|
| 1 | +# `SlimeSystems\ObjectId` |
| 2 | + |
| 3 | +A feature-packed ObjectId implementation for PHP. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Usage Guide |
| 8 | + |
| 9 | +`SlimeSystems\ObjectId` is a BSON-compatible identifier similar to MongoDB's `ObjectId`. |
| 10 | +It provides a 12-byte binary id based on the specification, along with handy utilities. |
| 11 | + |
| 12 | +### Creating ObjectIds |
| 13 | + |
| 14 | +#### Create a new ObjectId |
| 15 | + |
| 16 | +When no data is provided, a new unique identifier is generated. |
| 17 | + |
| 18 | +~~~php |
| 19 | +use SlimeSystems\ObjectId; |
| 20 | + |
| 21 | +$id = new ObjectId; |
| 22 | + |
| 23 | +var_dump($id->toString()); // 24-char hex string |
| 24 | +~~~ |
| 25 | + |
| 26 | +#### From raw binary data |
| 27 | + |
| 28 | +Accepts a binary string of exactly 12 bytes. |
| 29 | + |
| 30 | +~~~php |
| 31 | +$id = ObjectId::fromBinary($raw); |
| 32 | +~~~ |
| 33 | + |
| 34 | +Invalid lengths (anything other than 12 bytes) will throw `SlimeSystems\ObjectId\Exception\Invalid`. |
| 35 | + |
| 36 | +#### From a hex string |
| 37 | + |
| 38 | +Accepts a 24-character hexadecimal string. |
| 39 | + |
| 40 | +~~~php |
| 41 | +$id = ObjectId::fromString($hex); |
| 42 | +~~~ |
| 43 | + |
| 44 | +Invalid hex or invalid lengths will throw `SlimeSystems\ObjectId\Exception\Invalid`. |
| 45 | + |
| 46 | +#### From a timestamp |
| 47 | + |
| 48 | +You can generate an ObjectId from either a DateTime object or a Unix timestamp. |
| 49 | + |
| 50 | +~~~php |
| 51 | +$id = ObjectId::fromTime($dateTime); |
| 52 | +// or |
| 53 | +$id = ObjectId::fromTime($timestamp); |
| 54 | +~~~ |
| 55 | + |
| 56 | +#### ObjectIds for time comparison |
| 57 | + |
| 58 | +If you need an ObjectId for time comparisons, you can add `unique: false` to zeroes the last 8 bytes out: |
| 59 | + |
| 60 | +~~~php |
| 61 | +$id = ObjectId::fromTime($time, unique: false); |
| 62 | +// Last 8 bytes of the hex string will be "0000000000000000" |
| 63 | +~~~ |
| 64 | + |
| 65 | +### Conversions |
| 66 | + |
| 67 | +#### Convert to string |
| 68 | + |
| 69 | +~~~php |
| 70 | +$id->toString() // return 24-digit hexadecimal string |
| 71 | +~~~ |
| 72 | + |
| 73 | +#### Convert to binary |
| 74 | + |
| 75 | +~~~php |
| 76 | +$id->toBinary() // return 12 byte binary string |
| 77 | +~~~ |
| 78 | + |
| 79 | +#### Extracting the time |
| 80 | + |
| 81 | +You can retrieve the timestamp embedded in the ObjectId: |
| 82 | + |
| 83 | +~~~php |
| 84 | +$id = new ObjectId; |
| 85 | + |
| 86 | +// Returns a DateTime object |
| 87 | +$time = $id->toTime(); |
| 88 | +~~~ |
| 89 | + |
| 90 | +#### Human-readable inspection |
| 91 | + |
| 92 | +`inspect()` returns a more detailed string containing the hex form and extra info. |
| 93 | + |
| 94 | +~~~php |
| 95 | +$id->inspect() // return "SlimeSystems\ObjectId(<hexadecimal representation>)" |
| 96 | +~~~ |
| 97 | + |
| 98 | +### Comparison |
| 99 | + |
| 100 | +#### Equality |
| 101 | + |
| 102 | +~~~php |
| 103 | +$id->equals($id); // true |
| 104 | +~~~ |
| 105 | + |
| 106 | +Comparing to a non-ObjectId always returns `false`. |
| 107 | + |
| 108 | +#### Lexicographic comparison |
| 109 | + |
| 110 | +`compareTo()` works similarly to `strcmp()`: |
| 111 | + |
| 112 | +~~~php |
| 113 | +$id1->compareTo($id2); // -1 (smaller) |
| 114 | +$id2->compareTo($id1); // 1 (larger) |
| 115 | +$id1->compareTo($id1); // 0 (equal) |
| 116 | +~~~ |
| 117 | + |
| 118 | +## `ObjectId` provides: |
| 119 | + |
| 120 | +* Unique ID generation |
| 121 | +* Creation from raw data, hex strings, or timestamps |
| 122 | +* Deterministic time-based IDs |
| 123 | +* String/binary conversion |
| 124 | +* Timestamp extraction |
| 125 | +* Comparison helpers (`equals()`, `compareTo()`) |
| 126 | +* Human-readable inspection |
| 127 | + |
| 128 | +This makes it a flexible utility for systems that need compact, sortable, BSON-compatible identifiers. |
| 129 | + |
| 130 | +## Tests |
| 131 | + |
| 132 | +~~~bash |
| 133 | +composer run test |
| 134 | +~~~ |
| 135 | + |
| 136 | +or if you have containerd: |
| 137 | + |
| 138 | +~~~bash |
| 139 | +make test |
| 140 | +~~~ |
| 141 | + |
| 142 | +## License |
| 143 | + |
| 144 | +[BSD 2-Clause License](./LICENSE.md) |
0 commit comments