elfsundae / laravel-hashid-uuid
缩短 Laravel Hashid 的 UUID 编码。
1.1.0
2022-02-19 19:36 UTC
Requires
- php: ~7.1|~8.0
- elfsundae/laravel-hashid: ~1.0
- ramsey/uuid: ~3.6|~4.0
Requires (Dev)
- mockery/mockery: ~1.0
- phpunit/phpunit: ~5.7|~6.0|~7.0|~8.0|~9.0
README
这是 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', ], // ... ],
用法
encode($data)
接受一个Ramsey\Uuid\Uuid
实例或 UUID 字符串。decode($data)
返回一个Ramsey\Uuid\Uuid
实例。
示例
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 许可。