krmgns/uuid
最新稳定版本:1.4.3
Composer 安装命令:
composer require krmgns/uuid
包简介
Random or sortable UUIDs with date/time prefixes.
关键字:
README 文档
README
While preserving version & variant fields of generated values, Uuid library provides three types of UUIDs with a simple and fast approach, and can be used where sortable UUIDs are needed.
The generate() method of;
Uuid\Uuidclass uses 16-length random bytes (UUID/v4).Uuid\DateUuidclass uses 12-length random bytes and 4-length bytes of UTC date as prefix, and generated values are sortable up to 8th hex character.Uuid\DateTimeUuidclass uses 10-length random bytes and 6-length bytes of UTC date/time as prefix, and generated values are sortable up to 12th hex character.
Besides these UUIDs are sortable, they can be used for some sort of jobs like folder exploration (say, where we are working with an image cropping service).
So, as a quick example, let's see it in action;
// File: ImageController.php use Uuid\{DateUuid, UuidError}; use Throwable; /** * Eg: cdn.foo.com/image/crop/0134b3ce-ce20-4917-a020-f0514e110834.jpg * @route /image/crop/:image */ public function cropAction(string $image) { // Eg: 0134b3ce-ce20-4917-a020-f0514e110834.jpg [$name, $extension] = explode('.', $image); try { // Since we've created an image file name with DateUuid, // here we're expecting the incoming $image to be valid. $uuid = new DateUuid($name); // Eg: 2023/11/12 $path = $uuid->getDate('/'); } catch (UuidError) { // Invalid DateUuid. throw new BadRequestError(); } catch (Throwable) { // Internal error. throw new InternalServerError(); } // Eg: /images/2023/11/12/0134b3ce-ce20-4917-a020-f0514e110834.jpg $image = sprintf('/images/%s/%s.%s', $path, $name, $extension); // No such file. if (!file_exists($image)) { throw new NotFoundError(); } // Else crop image & serve cropped image here... }
Installing
composer require krmgns/uuid
Notes / Reminding
· Besides all classes can take $value argument (#1) as type of string, Uuid\Uuid class can also take type of Uuid\Uuid, Uuid\DateUuid class can also take type of Uuid\DateUuid, Uuid\DateTimeUuid class can also take type of Uuid\DateTimeUuid, but it also can be skipped for auto-generation at the same time.
· Besides Uuid\Uuid is implementing Stringable interface, Uuid\DateUuid and Uuid\DateTimeUuid are subclasses of Uuid\Uuid class. So, while inheriting some useful methods (toString(), toHashString(), etc.), they also overrides some methods (isValid(), generate(), validate() etc.) alongside __constructor() methods.
· Since Uuid\DateTimeUuid uses an instant date/time stamp up to seconds (format: YmdHis), the best sortable UUIDs can only be generated with this class.
The Uuid\Uuid Class
Like the inheriting classes, when no $value (UUID value) given, Uuid\Uuid class will generate and assign its value by itself. Otherwise, given value will be assigned after it's checked in strict mode (modifier argument is $strict as true) whether it's a valid UUID value or not.
use Uuid\{Uuid, UuidError}; // Auto-generate. $uuid = new Uuid(); assert($uuid->value === $uuid->toString()); assert($uuid->value === (string) $uuid); assert($uuid->value == $uuid); // Stringable assert(true === $uuid->isEqual($uuid)); assert(true === $uuid->isEqual($uuid->value)); $uuid = new Uuid('26708ec6-ad78-4291-a449-9ee08cf50cfc'); assert(true === $uuid->isValid()); $uuid = new Uuid('invalid', strict: false); assert(false === $uuid->isValid()); try { new Uuid(null); } catch (UuidError $e) { assert("Invalid UUID value: null" === $e->getMessage()); } try { new Uuid('invalid'); } catch (UuidError $e) { assert("Invalid UUID value: 'invalid'" === $e->getMessage()); } // Given value. $uuid = new Uuid($value = '26708ec6-ad78-4291-a449-9ee08cf50cfc'); assert(true === $uuid->isEqual($uuid)); assert(true === $uuid->isEqual($uuid->value)); assert(true === $uuid->isEqual($value)); assert('26708ec6-ad78-4291-a449-9ee08cf50cfc' === $uuid->toString()); assert('26708ec6ad784291a4499ee08cf50cfc' === $uuid->toHashString()); // Null values. $uuid1 = new Uuid('00000000-0000-0000-0000-000000000000', strict: false); $uuid2 = new Uuid('00000000000000000000000000000000', strict: false); assert(false === $uuid1->isValid()); assert(false === $uuid2->isValid()); assert(true === $uuid1->isNull()); assert(true === $uuid2->isNullHash()); assert(Uuid::NULL === $uuid1->value); assert(Uuid::NULL_HASH === $uuid2->value);
Statics
// Generating. $uuid = Uuid::generate(); // Eg: fec3cfe2-d378-4181-8ba1-99c54bcfa63e // Validating. $valid = Uuid::validate($uuid); assert(true === $valid); assert(false === Uuid::validate('invalid')); assert(false === Uuid::validate('invalid', strict: false)); assert(false === Uuid::validate(Uuid::NULL)); assert(false === Uuid::validate(Uuid::NULL_HASH)); assert(true === Uuid::validate(Uuid::NULL, strict: false)); assert(true === Uuid::validate(Uuid::NULL_HASH, strict: false)); // Equal checking. assert(true === Uuid::equals($uuid, 'fec3cfe2-d378-4181-8ba1-99c54bcfa63e')); assert(false === Uuid::equals($uuid, 'invalid-uuid-input-value')); // DIY tools. $bins = random_bytes(16); // Add version/variant. $bins = Uuid::modify($bins); // Format as UUID format. $uuid = Uuid::format(bin2hex($bins));
See test/unit/UuidTest.php for more examples.
The Uuid\DateUuid Class
This class uses 12-length random bytes and 4-length bytes of UTC date as prefix. So, its date can be re-taken (eg: 20231212 or 2023-12-12 with $separator option) to use for any use case and it's usable for where sortable UUIDs are needed.
use Uuid\DateUuid; $date = explode('.', gmdate('Y.m.d')); $time = explode('.', gmdate('00:00:00')); // Getting date. $uuid = new DateUuid(); assert($date === $uuid->getDate()); assert(implode('/', $date) === $uuid->getDate(separator: '/')); $uuid = new DateUuid('d41d8cd98f00b204e9800998ecf8427e', strict: false); assert(null === $uuid->getDate()); // Getting date/time. $uuid = new DateUuid(); assert(implode('-', $date) === $uuid->getDateTime()->format('Y-m-d')); assert(implode(':', $time) === $uuid->getDateTime()->format('H:i:s')); $uuid = new DateUuid('d41d8cd98f00b204e9800998ecf8427e', strict: false); assert(null === $uuid->getDateTime());
Statics
// Generating. $uuid = DateUuid::generate(); // Eg: 0134b3ce-25fc-49f8-b9f9-61ed2784c7d1 // Parsing. $uuid1 = new DateUuid(); $uuid2 = new DateUuid('d41d8cd98f00b204e9800998ecf8427e', strict: false); assert(null !== DateUuid::parse($uuid1->value)); assert(null === DateUuid::parse($uuid2->value)); // Next year for falsity (eg: 20241212). $threshold = (gmdate('Y') + 1) . '1212'; assert(null === DateUuid::parse($uuid1->value, $threshold));
See test/unit/DateUuidTest.php for more examples.
The Uuid\DateTimeUuid Class
This class uses 10-length random bytes and 6-length bytes of UTC date/time as prefix. So, its date can be re-taken (eg: 20231212, 101122 or 2023-12-12, 10-11-22 with $separator option) to use for any use case and it's usable for where sortable UUIDs are needed.
use Uuid\DateTimeUuid; $date = explode('.', gmdate('Y.m.d')); $time = explode('.', gmdate('H.i.s')); // Getting date. $uuid = new DateTimeUuid(); assert($date === $uuid->getDate()); assert(implode('/', $date) === $uuid->getDate(separator: '/')); $uuid = new DateTimeUuid('d41d8cd98f00b204e9800998ecf8427e', strict: false); assert(null === $uuid->getDate()); // Getting time. $uuid = new DateTimeUuid(); assert($time === $uuid->getTime()); assert(implode(':', $time) === $uuid->getTime(separator: ':')); $uuid = new DateTimeUuid('d41d8cd98f00b204e9800998ecf8427e', strict: false); assert(null === $uuid->getTime()); // Getting date/time. $uuid = new DateTimeUuid(); assert(implode('-', $date) === $uuid->getDateTime()->format('Y-m-d')); assert(implode(':', $time) === $uuid->getDateTime()->format('H:i:s')); $uuid = new DateTimeUuid('d41d8cd98f00b204e9800998ecf8427e', strict: false); assert(null === $uuid->getDateTime());
Statics
// Generating. $uuid = DateTimeUuid::generate(); // Eg: 12666c9c-b0c6-4532-b8da-dbc660ff4170 // Parsing. $uuid1 = new DateTimeUuid(); $uuid2 = new DateTimeUuid('d41d8cd98f00b204e9800998ecf8427e', strict: false); assert(null !== DateTimeUuid::parse($uuid1->value)); assert(null === DateTimeUuid::parse($uuid2->value)); // Next year for falsity (eg: 20241212191919). $threshold = (gmdate('Y') + 1) . '1212191919'; assert(null === DateTimeUuid::parse($uuid1->value, $threshold));
See test/unit/DateTimeUuidTest.php for more examples.
krmgns/uuid 适用场景与选型建议
krmgns/uuid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 01 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「uuid」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 krmgns/uuid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 krmgns/uuid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 krmgns/uuid 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
DrUUID RFC 4122 library for PHP
Trait to generate uuid when creating models
SilverStripe module for adding an unique id (UUID) to a DataObject
A CakePHP plugin for using secure, non-incremental IDs for entities
Add a factory management for Laramore
Time Based UUID Generator
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2024-01-26