slime-systems/object-id
Composer 安装命令:
composer require slime-systems/object-id
包简介
A feature-packed, BSON-compatible ObjectId implementation for PHP, similar to MongoDB's ObjectId, providing unique ID generation, conversions, and comparison utilities.
README 文档
README
A feature-packed, standalone ObjectId implementation for PHP.
SlimeSystems\ObjectId provides a BSON-compatible identifier generator and utility class, fully compliant with the MongoDB ObjectId specification.
It is lightweight, requires no external extensions (like mongodb), and is perfect for generating sortable, unique identifiers in any PHP application.
🚀 Installation
Install via Composer:
composer require slime-systems/object-id
⚡ Quick Start
use SlimeSystems\ObjectId; // Generate a new unique ID $id = new ObjectId(); echo $id; // Output: 507f1f77bcf86cd799439011 // Get the generation time $timestamp = $id->toTime(); // Returns a DateTime object
📖 Usage Guide
Creating ObjectIds
Generate a New ID
Create a fresh, unique 12-byte identifier.
$id = new ObjectId;
From Hex String
Restore an ObjectId from its 24-character hexadecimal representation.
$id = ObjectId::fromString('507f1f77bcf86cd799439011');
From Binary Data
Create from a raw 12-byte binary string (e.g., stored in a database).
$id = ObjectId::fromBinary($binaryData);
From Timestamp
Generate an ID based on a specific time. useful for time-based sorting or filtering.
// From a DateTime object $id = ObjectId::fromTime(new DateTime('2025-01-01')); // From a Unix timestamp $id = ObjectId::fromTime(1735689600);
Note: By default, fromTime generates a unique ID (randomizing the remaining bytes). If you need a "zeroed" ID for range queries (e.g., "find all IDs created after X"), pass unique: false:
$startId = ObjectId::fromTime($timestamp, unique: false); // Last 8 bytes will be 0000000000000000
Invalid input to the ObjectId::from... series will throw SlimeSystems\ObjectId\Exception\Invalid.
They are safe for handling untrusted variables, assuming that this exception is the expected behavior.
Conversions
$id = new ObjectId; // To Hex String (24 chars) $hex = $id->toString(); // or simply: (string) $id // To Binary (12 bytes) $bin = $id->toBinary(); // To DateTime $date = $id->toTime();
Comparisons
Equality Check
Check if two ObjectIds represent the same value.
if ($id1->equals($id2)) { // ... }
Sorting
Compare two IDs lexicographically (useful for sorting).
$result = $id1->compareTo($id2); // -1 if $id1 < $id2 // 0 if $id1 == $id2 // 1 if $id1 > $id2
Debugging
Get a readable inspection string.
echo $id->inspect(); // SlimeSystems\ObjectId(507f1f77bcf86cd799439011)
🔌 Framework Integration
Laravel / Eloquent
Using Laravel? Check out SlimeSystems\EloquentObjectId for seamless integration with Eloquent models.
🧪 Testing
Run the test suite with:
composer run test
Or if you have containerd:
make test
📄 License
This project is licensed under the BSD 2-Clause License.
slime-systems/object-id 适用场景与选型建议
slime-systems/object-id 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 slime-systems/object-id 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 slime-systems/object-id 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2025-11-20