Skip to content

Commit 53945f5

Browse files
authored
Merge pull request #4: add Ulid identifier
2 parents c4976c5 + 7914a72 commit 53945f5

23 files changed

Lines changed: 1123 additions & 35 deletions

File tree

README.md

Lines changed: 142 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,165 @@ The package provides the ability to use `ramsey/identifier` as various Cycle ORM
99

1010
## Installation
1111

12-
> Note this package required PHP `8.2` or newer.
12+
> **Note:** Due to a dependency on `ramsey/identifier` this package requires PHP `8.2` or newer.
1313
1414
Install this package as a dependency using Composer.
1515

1616
```bash
1717
composer require cycle/entity-behavior-identifier
1818
```
1919

20-
## Example
20+
## Snowflake Examples
2121

22-
They are randomly-generated and do not contain any information about the time they are created or the machine that
23-
generated them.
22+
**Snowflake:** A distributed ID generation system developed by Twitter that produces 64-bit unique, sortable identifiers. Each ID encodes a timestamp, machine ID, and sequence number, enabling high-throughput, ordered ID creation suitable for large-scale distributed applications.
23+
24+
> **Note:** Support for Snowflake identifiers will arrive soon, stay tuned.
25+
26+
## ULID Examples
27+
28+
**ULID (Universally Unique Lexicographically Sortable Identifier):** A 128-bit identifier designed for high uniqueness and lexicographical sortability. It combines a timestamp component with random data, allowing for ordered IDs that can be generated rapidly and are human-readable, making it ideal for databases and distributed systems.
29+
30+
```php
31+
use Cycle\Annotated\Annotation\Column;
32+
use Cycle\Annotated\Annotation\Entity;
33+
use Cycle\ORM\Entity\Behavior\Identifier;
34+
use Ramsey\Identifier\Ulid;
35+
36+
#[Entity]
37+
#[Identifier\Ulid(field: 'id')]
38+
class User
39+
{
40+
#[Column(type: 'ulid', primary: true)]
41+
private Ulid $id;
42+
}
43+
```
44+
45+
## UUID Examples
46+
47+
**UUID Version 1 (Time-based):** Generated using the current timestamp and the MAC address of the computer, ensuring unique identification based on time and hardware.
48+
49+
```php
50+
use Cycle\Annotated\Annotation\Column;
51+
use Cycle\Annotated\Annotation\Entity;
52+
use Cycle\ORM\Entity\Behavior\Identifier;
53+
use Ramsey\Identifier\Uuid;
54+
55+
#[Entity]
56+
#[Identifier\Uuid1(field: 'id')]
57+
class User
58+
{
59+
#[Column(type: 'uuid', primary: true)]
60+
private Uuid $id;
61+
}
62+
```
63+
64+
**UUID Version 2 (DCE Security):** Similar to version 1 but includes a local identifier such as a user ID or group ID, primarily used in DCE security contexts.
65+
66+
```php
67+
use Cycle\Annotated\Annotation\Column;
68+
use Cycle\Annotated\Annotation\Entity;
69+
use Cycle\ORM\Entity\Behavior\Identifier;
70+
use Ramsey\Identifier\Uuid;
71+
72+
#[Entity]
73+
#[Identifier\Uuid2(field: 'id')]
74+
class User
75+
{
76+
#[Column(type: 'uuid', primary: true)]
77+
private Uuid $id;
78+
}
79+
```
80+
81+
**UUID Version 3 (Name-based, MD5):** Created by hashing a namespace identifier and name using MD5, resulting in a deterministic UUID based on input data.
82+
83+
```php
84+
use Cycle\Annotated\Annotation\Column;
85+
use Cycle\Annotated\Annotation\Entity;
86+
use Cycle\ORM\Entity\Behavior\Identifier;
87+
use Ramsey\Identifier\Uuid;
88+
89+
#[Entity]
90+
#[Identifier\Uuid3(
91+
field: 'id',
92+
namespace: '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
93+
name: 'example.com',
94+
)]
95+
class User
96+
{
97+
#[Column(type: 'uuid', primary: true)]
98+
private Uuid $id;
99+
}
100+
```
101+
102+
**UUID Version 4 (Random):** Generated entirely from random or pseudo-random numbers, offering high unpredictability and uniqueness.
103+
104+
```php
105+
use Cycle\Annotated\Annotation\Column;
106+
use Cycle\Annotated\Annotation\Entity;
107+
use Cycle\ORM\Entity\Behavior\Identifier;
108+
use Ramsey\Identifier\Uuid;
109+
110+
#[Entity]
111+
#[Identifier\Uuid4(field: 'id')]
112+
class User
113+
{
114+
#[Column(type: 'uuid', primary: true)]
115+
private Uuid $id;
116+
}
117+
```
118+
119+
**UUID Version 5 (Name-based, SHA-1):** Similar to version 3 but uses SHA-1 hashing, providing a different deterministic UUID based on namespace and name.
120+
121+
```php
122+
use Cycle\Annotated\Annotation\Column;
123+
use Cycle\Annotated\Annotation\Entity;
124+
use Cycle\ORM\Entity\Behavior\Identifier;
125+
use Ramsey\Identifier\Uuid;
126+
127+
#[Entity]
128+
#[Identifier\Uuid5(
129+
field: 'id',
130+
namespace: '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
131+
name: 'example.com',
132+
)]
133+
class User
134+
{
135+
#[Column(type: 'uuid', primary: true)]
136+
private Uuid $id;
137+
}
138+
```
139+
140+
**UUID Version 6 (Draft/Upcoming):** An experimental or proposed version focused on improving time-based UUIDs with more sortable properties (not yet widely adopted).
141+
142+
```php
143+
use Cycle\Annotated\Annotation\Column;
144+
use Cycle\Annotated\Annotation\Entity;
145+
use Cycle\ORM\Entity\Behavior\Identifier;
146+
use Ramsey\Identifier\Uuid;
147+
148+
#[Entity]
149+
#[Identifier\Uuid6(field: 'id')]
150+
class User
151+
{
152+
#[Column(type: 'uuid', primary: true)]
153+
private Uuid $id;
154+
}
155+
```
156+
157+
**UUID Version 7 (Draft/Upcoming):** A newer proposal designed to incorporate sortable features based on Unix timestamp, enhancing performance in database indexing.
24158

25159
```php
26160
use Cycle\Annotated\Annotation\Column;
27161
use Cycle\Annotated\Annotation\Entity;
28-
use Cycle\ORM\Entity\Behavior\Idetifier\Uuid4;
162+
use Cycle\ORM\Entity\Behavior\Identifier;
29163
use Ramsey\Identifier\Uuid;
30164

31165
#[Entity]
32-
#[Uuid4]
166+
#[Identifier\Uuid7(field: 'id')]
33167
class User
34168
{
35-
#[Column(field: 'uuid', type: 'uuid', primary: true)]
36-
private Uuid $uuid;
169+
#[Column(type: 'uuid', primary: true)]
170+
private Uuid $id;
37171
}
38172
```
39173

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@
4141
},
4242
"require": {
4343
"php": ">=8.2",
44-
"cycle/entity-behavior": "^1.3.1",
45-
"ramsey/identifier": "^0.1.0"
44+
"cycle/entity-behavior": "^1.6",
45+
"ramsey/identifier": "^0.1"
4646
},
4747
"require-dev": {
48-
"cycle/annotated": "^4.3.0",
49-
"doctrine/annotations": "^1.14.4 || ^2.0.2",
48+
"cycle/annotated": "^4.3",
49+
"doctrine/annotations": "^1.14 || ^2.0",
5050
"phpunit/phpunit": "^9.5",
51-
"spiral/code-style": "^2.2.0",
51+
"spiral/code-style": "^2.2",
5252
"spiral/tokenizer": "^2.14 || ^3.0",
5353
"vimeo/psalm": "^5.26 || ^6.6"
5454
},

composer.lock

Lines changed: 23 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Listener/Ulid.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Entity\Behavior\Identifier\Listener;
6+
7+
use Cycle\ORM\Entity\Behavior\Attribute\Listen;
8+
use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnCreate;
9+
use Ramsey\Identifier\Ulid\UlidFactory;
10+
11+
final class Ulid
12+
{
13+
public function __construct(
14+
private string $field = 'ulid',
15+
private bool $nullable = false,
16+
) {}
17+
18+
#[Listen(OnCreate::class)]
19+
public function __invoke(OnCreate $event): void
20+
{
21+
if ($this->nullable || isset($event->state->getData()[$this->field])) {
22+
return;
23+
}
24+
25+
$identifier = (new UlidFactory())->create();
26+
27+
$event->state->register($this->field, $identifier);
28+
}
29+
}

0 commit comments

Comments
 (0)