Skip to content

Commit b8b7f06

Browse files
README refinement
1 parent 6fd8d0a commit b8b7f06

File tree

1 file changed

+95
-106
lines changed

1 file changed

+95
-106
lines changed

README.md

Lines changed: 95 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,150 @@
11
# Object ID
22

3-
A feature-packed ObjectId implementation for PHP.
3+
A feature-packed, standalone **ObjectId** implementation for PHP.
44

5-
[![PHP Composer](https://github.com/slime-systems/php-object-id/actions/workflows/php.yml/badge.svg)](https://github.com/slime-systems/php-object-id/actions/workflows/php.yml)
6-
[![Packagist Version](https://img.shields.io/packagist/v/slime-systems/object-id)](https://packagist.org/packages/slime-systems/object-id)
5+
[![Latest Version](https://img.shields.io/packagist/v/slime-systems/object-id)](https://packagist.org/packages/slime-systems/object-id)
6+
[![Tests](https://github.com/slime-systems/php-object-id/actions/workflows/php.yml/badge.svg)](https://github.com/slime-systems/php-object-id/actions/workflows/php.yml)
77

88
---
99

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).
1111

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.
1713

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
2015

21-
### Creating ObjectIds
16+
Install via Composer:
2217

23-
#### Create a new ObjectId
18+
```bash
19+
composer require slime-systems/object-id
20+
```
2421

25-
Generate a new unique identifier
22+
## ⚡ Quick Start
2623

27-
~~~php
24+
```php
2825
use SlimeSystems\ObjectId;
2926

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();
3429

35-
#### From raw binary data
30+
echo $id;
31+
// Output: 507f1f77bcf86cd799439011
3632

37-
Accepts a binary string of exactly 12 bytes.
33+
// Get the generation time
34+
$timestamp = $id->toTime();
35+
// Returns a DateTime object
36+
```
3837

39-
~~~php
40-
$id = ObjectId::fromBinary($raw);
41-
~~~
38+
## 📖 Usage Guide
4239

43-
Invalid binary will throw `SlimeSystems\ObjectId\Exception\Invalid`.
40+
### Creating ObjectIds
4441

45-
#### From a hex string
42+
#### Generate a New ID
43+
Create a fresh, unique 12-byte identifier.
4644

47-
Accepts a 24-character hexadecimal string.
45+
```php
46+
$id = new ObjectId;
47+
```
4848

49-
~~~php
50-
$id = ObjectId::fromString($hex);
51-
~~~
49+
#### From Hex String
50+
Restore an ObjectId from its 24-character hexadecimal representation.
5251

53-
Invalid hex string will throw `SlimeSystems\ObjectId\Exception\Invalid`.
52+
```php
53+
$id = ObjectId::fromString('507f1f77bcf86cd799439011');
54+
```
5455

55-
#### From a timestamp
56+
#### From Binary Data
57+
Create from a raw 12-byte binary string (e.g., stored in a database).
5658

57-
You can generate an ObjectId from either a DateTime object or a Unix timestamp.
59+
```php
60+
$id = ObjectId::fromBinary($binaryData);
61+
```
5862

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.
6465

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'));
6669

67-
#### ObjectIds for time comparison
70+
// From a Unix timestamp
71+
$id = ObjectId::fromTime(1735689600);
72+
```
6873

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`:
7075

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+
```
7580

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.
7783

7884
### Conversions
7985

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
9787
$id = new ObjectId;
9888

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
10492

105-
`inspect()` returns a readable string containing the hex representation.
93+
// To Binary (12 bytes)
94+
$bin = $id->toBinary();
10695

107-
~~~php
108-
$id->inspect() // return "SlimeSystems\ObjectId(<hexadecimal representation>)"
109-
~~~
96+
// To DateTime
97+
$date = $id->toTime();
98+
```
11099

111-
### Comparison
100+
### Comparisons
112101

113-
#### Equality
102+
#### Equality Check
103+
Check if two ObjectIds represent the same value.
114104

115-
~~~php
116-
$id->equals($id); // true
117-
~~~
105+
```php
106+
if ($id1->equals($id2)) {
107+
// ...
108+
}
109+
```
118110

119-
Comparing to a non-ObjectId always returns `false`.
111+
#### Sorting
112+
Compare two IDs lexicographically (useful for sorting).
120113

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+
```
122120

123-
`compareTo()` works similarly to `strcmp()`:
121+
### Debugging
122+
Get a readable inspection string.
124123

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+
```
130128

131-
## `ObjectId` provides:
129+
## 🔌 Framework Integration
132130

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.
140133

141-
This makes it a flexible utility for systems that need compact, sortable, BSON-compatible identifiers.
134+
## 🧪 Testing
142135

143-
## Tests
136+
Run the test suite with:
144137

145-
~~~bash
138+
```bash
146139
composer run test
147-
~~~
140+
```
148141

149-
or if you have containerd:
142+
Or if you have containerd:
150143

151-
~~~bash
144+
```bash
152145
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+
```
158147

159-
## License
148+
## 📄 License
160149

161-
[BSD 2-Clause License](./LICENSE.md)
150+
This project is licensed under the [BSD 2-Clause License](./LICENSE.md).

0 commit comments

Comments
 (0)