elfsundae/laravel-hashid-uuid

缩短 Laravel Hashid 的 UUID 编码。

1.1.0 2022-02-19 19:36 UTC

This package is auto-updated.

Last update: 2024-09-20 03:05:15 UTC


README

Latest Version on Packagist Software License tests StyleCI SymfonyInsight Grade Quality Score Code Coverage Total Downloads

这是 Laravel Hashid 的一个插件包,它提供了一个 uuid hashid 驱动程序来缩短您的 UUID 编码。

安装

您可以使用 Composer 管理器安装此包

$ composer require elfsundae/laravel-hashid-uuid

对于 Lumen 或低于 v5.5 的 Laravel,您需要手动注册服务提供者

ElfSundae\Laravel\Hashid\UuidServiceProvider::class,

配置

  • 驱动程序名称: uuid
  • 配置
    • connection:编码和解码时使用的 hashid 连接名称。

示例

'connections' => [

    'uuid_base62' => [
        'driver' => 'uuid',
        'connection' => 'base62',
    ],

    'uuid_hashids' => [
        'driver' => 'uuid',
        'connection' => 'hashids_string',
    ],

    // ...
],

用法

示例

use Ramsey\Uuid\Uuid;

$string = 'cd79e206-c715-11e7-891d-8bf37117635e';
$uuid = Uuid::fromString($string);
$hex = $uuid->getHex();                 // "cd79e206c71511e7891d8bf37117635e"

// Encode using the original connections:
hashid_encode($uuid, 'base62');         // "1mUcj8TfpKB7vEBlRecZ4PADhnE1UbBg2L9n3PQOSFqzYZHwj"
hashid_encode($uuid, 'hashids_string'); // "Wr3xrA2WWEh4K1LBBV6vhXL592VVQoSKWnpQB5vkt9DkZxDp6Lsjz945vnRl"

// Encode using the "uuid" driver:
hashid_encode($uuid, 'uuid_base62');    // "6Fj7unqIaNKkq5zbJo1HJ8"
hashid_encode($string, 'uuid_base62');  // "6Fj7unqIaNKkq5zbJo1HJ8"
hashid_encode($hex, 'uuid_base62');     // "6Fj7unqIaNKkq5zbJo1HJ8"
hashid_encode($uuid, 'uuid_hashids');   // "kp2wZkXOBzSMNA3nPxNzSOZJ701"

// Decode
$decoded = hashid_decode('6Fj7unqIaNKkq5zbJo1HJ8', 'uuid_base62');
(string) $decoded;                               // "cd79e206-c715-11e7-891d-8bf37117635e"
$decoded->getDateTime()->format('Y-m-d H:i:s');  // "2017-11-11 19:23:31"

// Decoding failure
(string) hashid_decode('foobar', 'uuid_base62'); // "00000000-0000-0000-0000-000000000000"

测试

$ composer test

许可

此包是开源软件,许可协议为 MIT 许可