|
1 | 1 | # Object ID |
2 | 2 |
|
3 | | -A feature-packed ObjectId implementation for PHP. |
| 3 | +A feature-packed, standalone **ObjectId** implementation for PHP. |
4 | 4 |
|
5 | | -[](https://github.com/slime-systems/php-object-id/actions/workflows/php.yml) |
6 | | -[](https://packagist.org/packages/slime-systems/object-id) |
| 5 | +[](https://packagist.org/packages/slime-systems/object-id) |
| 6 | +[](https://github.com/slime-systems/php-object-id/actions/workflows/php.yml) |
7 | 7 |
|
8 | 8 | --- |
9 | 9 |
|
10 | | -## Installation |
| 10 | +**SlimeSystems\ObjectId** provides a BSON-compatible identifier generator and utility class, fully compliant with the [MongoDB ObjectId specification](https://www.mongodb.com/docs/manual/reference/bson-types/#objectid). |
11 | 11 |
|
12 | | -~~~bash |
13 | | -composer require slime-systems/object-id |
14 | | -~~~ |
15 | | - |
16 | | -## Usage Guide |
| 12 | +It is lightweight, requires no external extensions (like `mongodb`), and is perfect for generating sortable, unique identifiers in any PHP application. |
17 | 13 |
|
18 | | -`SlimeSystems\ObjectId` is a BSON-compatible identifier similar to MongoDB's `ObjectId`. |
19 | | -It provides a 12-byte binary id based on [the specification](https://www.mongodb.com/docs/manual/reference/bson-types/#objectid), along with handy utilities. |
| 14 | +## 🚀 Installation |
20 | 15 |
|
21 | | -### Creating ObjectIds |
| 16 | +Install via Composer: |
22 | 17 |
|
23 | | -#### Create a new ObjectId |
| 18 | +```bash |
| 19 | +composer require slime-systems/object-id |
| 20 | +``` |
24 | 21 |
|
25 | | -Generate a new unique identifier |
| 22 | +## ⚡ Quick Start |
26 | 23 |
|
27 | | -~~~php |
| 24 | +```php |
28 | 25 | use SlimeSystems\ObjectId; |
29 | 26 |
|
30 | | -$id = new ObjectId; |
31 | | - |
32 | | -var_dump($id->toString()); // 24-char hex string |
33 | | -~~~ |
| 27 | +// Generate a new unique ID |
| 28 | +$id = new ObjectId(); |
34 | 29 |
|
35 | | -#### From raw binary data |
| 30 | +echo $id; |
| 31 | +// Output: 507f1f77bcf86cd799439011 |
36 | 32 |
|
37 | | -Accepts a binary string of exactly 12 bytes. |
| 33 | +// Get the generation time |
| 34 | +$timestamp = $id->toTime(); |
| 35 | +// Returns a DateTime object |
| 36 | +``` |
38 | 37 |
|
39 | | -~~~php |
40 | | -$id = ObjectId::fromBinary($raw); |
41 | | -~~~ |
| 38 | +## 📖 Usage Guide |
42 | 39 |
|
43 | | -Invalid binary will throw `SlimeSystems\ObjectId\Exception\Invalid`. |
| 40 | +### Creating ObjectIds |
44 | 41 |
|
45 | | -#### From a hex string |
| 42 | +#### Generate a New ID |
| 43 | +Create a fresh, unique 12-byte identifier. |
46 | 44 |
|
47 | | -Accepts a 24-character hexadecimal string. |
| 45 | +```php |
| 46 | +$id = new ObjectId; |
| 47 | +``` |
48 | 48 |
|
49 | | -~~~php |
50 | | -$id = ObjectId::fromString($hex); |
51 | | -~~~ |
| 49 | +#### From Hex String |
| 50 | +Restore an ObjectId from its 24-character hexadecimal representation. |
52 | 51 |
|
53 | | -Invalid hex string will throw `SlimeSystems\ObjectId\Exception\Invalid`. |
| 52 | +```php |
| 53 | +$id = ObjectId::fromString('507f1f77bcf86cd799439011'); |
| 54 | +``` |
54 | 55 |
|
55 | | -#### From a timestamp |
| 56 | +#### From Binary Data |
| 57 | +Create from a raw 12-byte binary string (e.g., stored in a database). |
56 | 58 |
|
57 | | -You can generate an ObjectId from either a DateTime object or a Unix timestamp. |
| 59 | +```php |
| 60 | +$id = ObjectId::fromBinary($binaryData); |
| 61 | +``` |
58 | 62 |
|
59 | | -~~~php |
60 | | -$id = ObjectId::fromTime($dateTime); |
61 | | -// or |
62 | | -$id = ObjectId::fromTime($timestamp); |
63 | | -~~~ |
| 63 | +#### From Timestamp |
| 64 | +Generate an ID based on a specific time. useful for time-based sorting or filtering. |
64 | 65 |
|
65 | | -Invalid time will throw `SlimeSystems\ObjectId\Exception\Invalid`. |
| 66 | +```php |
| 67 | +// From a DateTime object |
| 68 | +$id = ObjectId::fromTime(new DateTime('2025-01-01')); |
66 | 69 |
|
67 | | -#### ObjectIds for time comparison |
| 70 | +// From a Unix timestamp |
| 71 | +$id = ObjectId::fromTime(1735689600); |
| 72 | +``` |
68 | 73 |
|
69 | | -If you need an ObjectId for time comparisons, you can add `unique: false` to zeroes the last 8 bytes out: |
| 74 | +**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`: |
70 | 75 |
|
71 | | -~~~php |
72 | | -$id = ObjectId::fromTime($time, unique: false); |
73 | | -// Last 8 bytes of the hex string will be "0000000000000000" |
74 | | -~~~ |
| 76 | +```php |
| 77 | +$startId = ObjectId::fromTime($timestamp, unique: false); |
| 78 | +// Last 8 bytes will be 0000000000000000 |
| 79 | +``` |
75 | 80 |
|
76 | | -This also do not increase the internal counter for generating a unique identifier. |
| 81 | +Invalid input to the `ObjectId::from...` series will throw `SlimeSystems\ObjectId\Exception\Invalid`. |
| 82 | +They are safe for handling untrusted variables, assuming that this exception is the expected behavior. |
77 | 83 |
|
78 | 84 | ### Conversions |
79 | 85 |
|
80 | | -#### Convert to string |
81 | | - |
82 | | -~~~php |
83 | | -$id->toString() // return 24-digit hexadecimal string |
84 | | -~~~ |
85 | | - |
86 | | -#### Convert to binary |
87 | | - |
88 | | -~~~php |
89 | | -$id->toBinary() // return 12-byte binary string |
90 | | -~~~ |
91 | | - |
92 | | -#### Extracting the time |
93 | | - |
94 | | -You can retrieve the timestamp embedded in the ObjectId: |
95 | | - |
96 | | -~~~php |
| 86 | +```php |
97 | 87 | $id = new ObjectId; |
98 | 88 |
|
99 | | -// Returns a DateTime object |
100 | | -$time = $id->toTime(); |
101 | | -~~~ |
102 | | - |
103 | | -#### Human-readable inspection |
| 89 | +// To Hex String (24 chars) |
| 90 | +$hex = $id->toString(); |
| 91 | +// or simply: (string) $id |
104 | 92 |
|
105 | | -`inspect()` returns a readable string containing the hex representation. |
| 93 | +// To Binary (12 bytes) |
| 94 | +$bin = $id->toBinary(); |
106 | 95 |
|
107 | | -~~~php |
108 | | -$id->inspect() // return "SlimeSystems\ObjectId(<hexadecimal representation>)" |
109 | | -~~~ |
| 96 | +// To DateTime |
| 97 | +$date = $id->toTime(); |
| 98 | +``` |
110 | 99 |
|
111 | | -### Comparison |
| 100 | +### Comparisons |
112 | 101 |
|
113 | | -#### Equality |
| 102 | +#### Equality Check |
| 103 | +Check if two ObjectIds represent the same value. |
114 | 104 |
|
115 | | -~~~php |
116 | | -$id->equals($id); // true |
117 | | -~~~ |
| 105 | +```php |
| 106 | +if ($id1->equals($id2)) { |
| 107 | + // ... |
| 108 | +} |
| 109 | +``` |
118 | 110 |
|
119 | | -Comparing to a non-ObjectId always returns `false`. |
| 111 | +#### Sorting |
| 112 | +Compare two IDs lexicographically (useful for sorting). |
120 | 113 |
|
121 | | -#### Lexicographic comparison |
| 114 | +```php |
| 115 | +$result = $id1->compareTo($id2); |
| 116 | +// -1 if $id1 < $id2 |
| 117 | +// 0 if $id1 == $id2 |
| 118 | +// 1 if $id1 > $id2 |
| 119 | +``` |
122 | 120 |
|
123 | | -`compareTo()` works similarly to `strcmp()`: |
| 121 | +### Debugging |
| 122 | +Get a readable inspection string. |
124 | 123 |
|
125 | | -~~~php |
126 | | -$id1->compareTo($id2); // -1 (smaller) |
127 | | -$id2->compareTo($id1); // 1 (larger) |
128 | | -$id1->compareTo($id1); // 0 (equal) |
129 | | -~~~ |
| 124 | +```php |
| 125 | +echo $id->inspect(); |
| 126 | +// SlimeSystems\ObjectId(507f1f77bcf86cd799439011) |
| 127 | +``` |
130 | 128 |
|
131 | | -## `ObjectId` provides: |
| 129 | +## 🔌 Framework Integration |
132 | 130 |
|
133 | | -* Unique ID generation |
134 | | -* Creation from raw data, hex strings, or timestamps |
135 | | -* Deterministic time-based IDs |
136 | | -* String/binary conversion |
137 | | -* Timestamp extraction |
138 | | -* Comparison helpers (`equals()`, `compareTo()`) |
139 | | -* Human-readable inspection |
| 131 | +### Laravel / Eloquent |
| 132 | +Using Laravel? Check out **[SlimeSystems\EloquentObjectId](https://github.com/slime-systems/eloquent-object-id)** for seamless integration with Eloquent models. |
140 | 133 |
|
141 | | -This makes it a flexible utility for systems that need compact, sortable, BSON-compatible identifiers. |
| 134 | +## 🧪 Testing |
142 | 135 |
|
143 | | -## Tests |
| 136 | +Run the test suite with: |
144 | 137 |
|
145 | | -~~~bash |
| 138 | +```bash |
146 | 139 | composer run test |
147 | | -~~~ |
| 140 | +``` |
148 | 141 |
|
149 | | -or if you have containerd: |
| 142 | +Or if you have containerd: |
150 | 143 |
|
151 | | -~~~bash |
| 144 | +```bash |
152 | 145 | make test |
153 | | -~~~ |
154 | | - |
155 | | -## Headup |
156 | | - |
157 | | -If you are using Laravel or Eloquent, you may interested in [SlimeSystems\EloquentObjectId](https://github.com/slime-systems/eloquent-object-id). |
| 146 | +``` |
158 | 147 |
|
159 | | -## License |
| 148 | +## 📄 License |
160 | 149 |
|
161 | | -[BSD 2-Clause License](./LICENSE.md) |
| 150 | +This project is licensed under the [BSD 2-Clause License](./LICENSE.md). |
0 commit comments