oittaa/uuid
Composer 安装命令:
composer require oittaa/uuid
包简介
A small PHP class for generating RFC 9562 universally unique identifiers (UUID) from version 3 to version 8.
关键字:
README 文档
README
uuid-php
A small PHP class for generating RFC 9562 universally unique identifiers (UUID) from version 3 to version 8.
If all you want is a unique ID, you should call uuid4().
Implementations SHOULD utilize UUIDv7 instead of UUIDv1 and UUIDv6 if possible.
If you're regularly generating more than thousand UUIDs per second, you might want to use uuid8() instead of uuid7(). This implementation of uuid8() sacrifices some entropy compared to uuid7(), but offers 100 nanosecond granularity while being otherwise compatible.
Minimal UUID v4 implementation
Credits go to this answer on Stackoverflow for this minimal RFC 9562 compliant solution.
<?php function uuid4() { $data = random_bytes(16); $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); } echo uuid4();
Minimal UUID v7 implementation
<?php function uuid7() { static $last_timestamp = 0; $unixts_ms = intval(microtime(true) * 1000); if ($last_timestamp >= $unixts_ms) { $unixts_ms = $last_timestamp + 1; } $last_timestamp = $unixts_ms; $data = random_bytes(10); $data[0] = chr((ord($data[0]) & 0x0f) | 0x70); // set version $data[2] = chr((ord($data[2]) & 0x3f) | 0x80); // set variant return vsprintf( '%s%s-%s-%s-%s-%s%s%s', str_split( str_pad(dechex($unixts_ms), 12, '0', \STR_PAD_LEFT) . bin2hex($data), 4 ) ); } echo uuid7();
Installation
If you need comparison tools or sortable identifiers like in versions 6, 7, and 8, you might find this small and fast package useful. It doesn't require any other dependencies.
composer require oittaa/uuid
Usage
How to generate
<?php require 'vendor/autoload.php'; use UUID\UUID; // Generate a version 3 (name-based and hashed with MD5) UUID // Namespaces: NAMESPACE_DNS, NAMESPACE_URL, NAMESPACE_OID, NAMESPACE_X500 $uuid3 = UUID::uuid3(UUID::NAMESPACE_DNS, 'php.net'); echo $uuid3 . "\n"; // 11a38b9a-b3da-360f-9353-a5a725514269 // Generate a version 4 (random) UUID $uuid4 = UUID::uuid4(); echo $uuid4 . "\n"; // e.g. 2140a926-4a47-465c-b622-4571ad9bb378 // Generate a version 5 (name-based and hashed with SHA1) UUID $uuid5 = UUID::uuid5(UUID::NAMESPACE_DNS, 'php.net'); echo $uuid5 . "\n"; // c4a760a8-dbcf-5254-a0d9-6a4474bd1b62 // Generate a version 6 (lexicographically sortable) UUID $uuid6_first = UUID::uuid6(); echo $uuid6_first . "\n"; // e.g. 1ec9414c-232a-6b00-b3c8-9e6bdeced846 $uuid6_second = UUID::uuid6(); var_dump($uuid6_first < $uuid6_second); // bool(true) // Generate a version 7 (lexicographically sortable) UUID $uuid7_first = UUID::uuid7(); echo $uuid7_first . "\n"; // e.g. 017f22e2-79b0-7cc3-98c4-dc0c0c07398f $uuid7_second = UUID::uuid7(); var_dump($uuid7_first < $uuid7_second); // bool(true) // Generate a version 8 (lexicographically sortable) UUID $uuid8_first = UUID::uuid8(); echo $uuid8_first . "\n"; // e.g. 017f22e2-79b0-8cc3-98c4-dc0c0c07398f $uuid8_second = UUID::uuid8(); var_dump($uuid8_first < $uuid8_second); // bool(true)
Tools
<?php require 'vendor/autoload.php'; use UUID\UUID; // Test if a given string is a valid UUID $isvalid = UUID::isValid('11a38b9a-b3da-360f-9353-a5a725514269'); var_dump($isvalid); // bool(true) // The string standard representation of the UUID. $tostring = UUID::toString('{C4A760A8-DBCF-5254-A0D9-6A4474BD1B62}'); var_dump($tostring); // string(36) "c4a760a8-dbcf-5254-a0d9-6a4474bd1b62" // Test if two UUIDs are equal $equals1 = UUID::equals( 'c4a760a8-dbcf-5254-a0d9-6a4474bd1b62', '2140a926-4a47-465c-b622-4571ad9bb378' ); var_dump($equals1); // bool(false) $equals2 = UUID::equals( 'c4a760a8-dbcf-5254-a0d9-6a4474bd1b62', 'C4A760A8-DBCF-5254-A0D9-6A4474BD1B62' ); var_dump($equals2); // bool(true) $equals3 = UUID::equals( 'urn:uuid:c4a760a8-dbcf-5254-a0d9-6a4474bd1b62', '{C4A760A8-DBCF-5254-A0D9-6A4474BD1B62}' ); var_dump($equals3); // bool(true) // UUID comparison. Returns < 0 if uuid1 is less than uuid2; // > 0 if uuid1 is greater than uuid2, and 0 if they are equal. $cmp1 = UUID::cmp( '11a38b9a-b3da-360f-9353-a5a725514269', '2140a926-4a47-465c-b622-4571ad9bb378' ); var_dump($cmp1 < 0); // bool(true) $cmp2 = UUID::cmp( 'c4a760a8-dbcf-5254-a0d9-6a4474bd1b62', '2140a926-4a47-465c-b622-4571ad9bb378' ); var_dump($cmp2 > 0); // bool(true) $cmp3 = UUID::cmp( 'urn:uuid:c4a760a8-dbcf-5254-a0d9-6a4474bd1b62', '{C4A760A8-DBCF-5254-A0D9-6A4474BD1B62}' ); var_dump($cmp3 === 0); // bool(true) // Extract Unix time from versions 6, 7, and 8 as a string. $uuid6_time = UUID::getTime('1ec9414c-232a-6b00-b3c8-9e6bdeced846'); var_dump($uuid6_time); // string(18) "1645557742.0000000" $uuid7_time = UUID::getTime('017f22e2-79b0-7cc3-98c4-dc0c0c07398f'); var_dump($uuid7_time); // string(18) "1645557742.0000000" $uuid8_time = UUID::getTime('017f22e2-79b0-8cc3-98c4-dc0c0c07398f'); var_dump($uuid8_time); // string(18) "1645557742.0007977" // Extract the UUID version. $uuid_version = UUID::getVersion('2140a926-4a47-465c-b622-4571ad9bb378'); var_dump($uuid_version); // int(4)
UUIDv6 Field and Bit Layout
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| time_high |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| time_mid | time_low_and_version |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-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+-+-+-+-+-+-+-+-+-+
| rand |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
unix_ts_ms: 48 bit big-endian unsigned number of Unix epoch timestamp with millisecond level of precisionver: The 4 bit UUIDv8 version (1000)subsec: 12 bits allocated to sub-second precision valuesvar: 2 bit UUID variant (10)sub: 2 bits allocated to sub-second precision valuesrand: The remaining 60 bits are filled with pseudo-random data
14 bits dedicated to sub-second precision provide 100 nanosecond resolution. The unix_ts_ms and subsec fields guarantee the order of UUIDs generated within the same timestamp by monotonically incrementing the timer.
oittaa/uuid 适用场景与选型建议
oittaa/uuid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 325.82k 次下载、GitHub Stars 达 54, 最近一次更新时间为 2021 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「uuid」 「identifier」 「guid」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 oittaa/uuid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 oittaa/uuid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 oittaa/uuid 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Paramconverter, Normalizer and Form Type for Ramsey Uuid
A PHP library for generating and working with identifiers, including UUIDs, ULIDs, and Snowflakes
DrUUID RFC 4122 library for PHP
Trait to generate uuid when creating models
Provides Doctrine types for mediagone/small-uid package.
Simply create Eloquent Models & database tables with UUID/GUID primary keys.
统计信息
- 总下载量: 325.82k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 54
- 点击次数: 34
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-04