Skip to content

Commit 63943c7

Browse files
Merge pull request #5 from cycle/snowflake-identifier
Snowflake identifier
2 parents 53945f5 + 2d10cfc commit 63943c7

88 files changed

Lines changed: 4143 additions & 2068 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci-mssql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ jobs:
1414
phpunit:
1515
uses: cycle/gh-actions/.github/workflows/db-mssql.yml@master
1616
with:
17-
php: '["8.2","8.3","8.4"]'
17+
php: '["8.2","8.3","8.4","8.5"]'
1818

1919
...

.github/workflows/ci-mysql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ jobs:
1414
phpunit:
1515
uses: cycle/gh-actions/.github/workflows/db-mysql.yml@master
1616
with:
17-
php: '["8.2","8.3","8.4"]'
17+
php: '["8.2","8.3","8.4","8.5"]'
1818

1919
...

.github/workflows/ci-pgsql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ jobs:
1414
phpunit:
1515
uses: cycle/gh-actions/.github/workflows/db-pgsql.yml@master
1616
with:
17-
php: '["8.2","8.3","8.4"]'
17+
php: '["8.2","8.3","8.4","8.5"]'
1818

1919
...

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
runs-on: ubuntu-latest
1717
strategy:
1818
matrix:
19-
php-versions: ['8.2', '8.3', '8.4']
19+
php-versions: ['8.2', '8.3', '8.4', "8.5"]
2020
steps:
2121
- name: Install ODBC driver.
2222
run: |
@@ -68,7 +68,7 @@ jobs:
6868
runs-on: ubuntu-latest
6969
strategy:
7070
matrix:
71-
php-versions: ['8.2', '8.3', '8.4']
71+
php-versions: ['8.2', '8.3', '8.4', '8.5']
7272
steps:
7373
- name: 📦 Checkout
7474
uses: actions/checkout@v2

README.md

Lines changed: 127 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Cycle ORM Entity Behavior Identifier
2-
[![Latest Stable Version](https://poser.pugx.org/cycle/entity-behavior-Identifier/version)](https://packagist.org/packages/cycle/entity-behavior-identifier)
2+
[![Latest Stable Version](https://poser.pugx.org/cycle/entity-behavior-identifier/version)](https://packagist.org/packages/cycle/entity-behavior-identifier)
33
[![Build Status](https://github.com/cycle/entity-behavior-identifier/workflows/build/badge.svg)](https://github.com/cycle/entity-behavior-identifier/actions)
4-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/cycle/entity-behavior-identifier/badges/quality-score.png?b=1.x)](https://scrutinizer-ci.com/g/cycle/entity-behavior-identifier/?branch=1.x)
54
[![Codecov](https://codecov.io/gh/cycle/entity-behavior-identifier/graph/badge.svg)](https://codecov.io/gh/cycle/entity-behavior)
65
<a href="https://discord.gg/TFeEmCs"><img src="https://img.shields.io/badge/discord-chat-magenta.svg"></a>
76

@@ -17,15 +16,120 @@ Install this package as a dependency using Composer.
1716
composer require cycle/entity-behavior-identifier
1817
```
1918

20-
## Snowflake Examples
19+
## Usage
2120

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.
21+
The package provides various types of identifiers, for generating unique values or as alternatives to auto-increment
22+
IDs, helping ensure uniqueness and flexibility across an application.
2323

24-
> **Note:** Support for Snowflake identifiers will arrive soon, stay tuned.
24+
> **Note: ** Most identifiers encode metadata such as node ID and epoch offset. These values are typically
25+
> derived from the platform or system rather than defined within an entity. Each applicable listener class provides a
26+
> `setDefaults` method to allow these values to be set at an appropriate time within your application.
2527
26-
## ULID Examples
28+
For example:
2729

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.
30+
```php
31+
\Cycle\ORM\Entity\Behavior\Identifier\Listener\SnowflakeGeneric::setDefaults(0, 1_446_940_800_000);
32+
\Cycle\ORM\Entity\Behavior\Identifier\Listener\Uuid1::setDefaults('00000fffffff', 0xffff);
33+
```
34+
35+
36+
### Snowflake Examples
37+
38+
**Snowflake (Generic):** A flexible Snowflake implementation that generates globally unique, time-ordered 64-bit IDs
39+
without adhering to any specific platform’s conventions, suitable for general distributed systems.
40+
41+
```php
42+
use Cycle\Annotated\Annotation\Column;
43+
use Cycle\Annotated\Annotation\Entity;
44+
use Cycle\ORM\Entity\Behavior\Identifier;
45+
use Ramsey\Identifier\Snowflake;
46+
47+
#[Entity]
48+
#[Identifier\SnowflakeGeneric(field: 'id')]
49+
class User
50+
{
51+
#[Column(type: 'id', primary: true)]
52+
public Snowflake $id;
53+
}
54+
```
55+
56+
**Snowflake (Discord):** Implements Discord’s Snowflake format, generating 64-bit IDs that encode a timestamp,
57+
worker ID, and sequence number. Useful when interoperating with Discord’s API or matching its ID structure.
58+
59+
```php
60+
use Cycle\Annotated\Annotation\Column;
61+
use Cycle\Annotated\Annotation\Entity;
62+
use Cycle\ORM\Entity\Behavior\Identifier;
63+
use Ramsey\Identifier\Snowflake;
64+
65+
#[Entity]
66+
#[Identifier\SnowflakeDiscord(field: 'id')]
67+
class User
68+
{
69+
#[Column(type: 'id', primary: true)]
70+
public Snowflake $id;
71+
}
72+
```
73+
74+
**Snowflake (Instagram):** Follows Instagram’s Snowflake structure to produce unique, sortable 64-bit IDs suitable for
75+
applications that need compatibility with Instagram-style ID sequences.
76+
77+
```php
78+
use Cycle\Annotated\Annotation\Column;
79+
use Cycle\Annotated\Annotation\Entity;
80+
use Cycle\ORM\Entity\Behavior\Identifier;
81+
use Ramsey\Identifier\Snowflake;
82+
83+
#[Entity]
84+
#[Identifier\SnowflakeInstagram(field: 'id')]
85+
class User
86+
{
87+
#[Column(type: 'id', primary: true)]
88+
public Snowflake $id;
89+
}
90+
```
91+
92+
**Snowflake (Mastodon):** Generates IDs compatible with Mastodon’s distributed Snowflake system, encoding time and node
93+
information to ensure uniqueness across instances.
94+
95+
```php
96+
use Cycle\Annotated\Annotation\Column;
97+
use Cycle\Annotated\Annotation\Entity;
98+
use Cycle\ORM\Entity\Behavior\Identifier;
99+
use Ramsey\Identifier\Snowflake;
100+
101+
#[Entity]
102+
#[Identifier\SnowflakeMastodon(field: 'id')]
103+
class User
104+
{
105+
#[Column(type: 'id', primary: true)]
106+
public Snowflake $id;
107+
}
108+
```
109+
110+
**Snowflake (Twitter):** Produces 64-bit IDs in the format used by Twitter, encoding timestamp, machine ID, and sequence
111+
number for globally unique, time-sortable identifiers. Ideal for high-throughput distributed systems.
112+
113+
```php
114+
use Cycle\Annotated\Annotation\Column;
115+
use Cycle\Annotated\Annotation\Entity;
116+
use Cycle\ORM\Entity\Behavior\Identifier;
117+
use Ramsey\Identifier\Snowflake;
118+
119+
#[Entity]
120+
#[Identifier\SnowflakeTwitter(field: 'id')]
121+
class User
122+
{
123+
#[Column(type: 'id', primary: true)]
124+
public Snowflake $id;
125+
}
126+
```
127+
128+
### ULID Examples
129+
130+
**ULID (Universally Unique Lexicographically Sortable Identifier):** A 128-bit identifier designed for high uniqueness
131+
and lexicographical sortability. It combines a timestamp component with random data, allowing for ordered IDs that can
132+
be generated rapidly and are human-readable, making it ideal for databases and distributed systems.
29133

30134
```php
31135
use Cycle\Annotated\Annotation\Column;
@@ -42,9 +146,10 @@ class User
42146
}
43147
```
44148

45-
## UUID Examples
149+
### UUID Examples
46150

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.
151+
**UUID Version 1 (Time-based):** Generated using the current timestamp and the MAC address of the computer, ensuring
152+
unique identification based on time and hardware.
48153

49154
```php
50155
use Cycle\Annotated\Annotation\Column;
@@ -61,7 +166,8 @@ class User
61166
}
62167
```
63168

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.
169+
**UUID Version 2 (DCE Security):** Similar to version 1 but includes a local identifier such as a user ID or group ID,
170+
primarily used in DCE security contexts.
65171

66172
```php
67173
use Cycle\Annotated\Annotation\Column;
@@ -78,7 +184,8 @@ class User
78184
}
79185
```
80186

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.
187+
**UUID Version 3 (Name-based, MD5):** Created by hashing a namespace identifier and name using MD5, resulting in a
188+
deterministic UUID based on input data.
82189

83190
```php
84191
use Cycle\Annotated\Annotation\Column;
@@ -99,7 +206,8 @@ class User
99206
}
100207
```
101208

102-
**UUID Version 4 (Random):** Generated entirely from random or pseudo-random numbers, offering high unpredictability and uniqueness.
209+
**UUID Version 4 (Random):** Generated entirely from random or pseudo-random numbers, offering high unpredictability
210+
and uniqueness.
103211

104212
```php
105213
use Cycle\Annotated\Annotation\Column;
@@ -116,7 +224,8 @@ class User
116224
}
117225
```
118226

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.
227+
**UUID Version 5 (Name-based, SHA-1):** Similar to version 3 but uses SHA-1 hashing, providing a different deterministic
228+
UUID based on namespace and name.
120229

121230
```php
122231
use Cycle\Annotated\Annotation\Column;
@@ -137,7 +246,8 @@ class User
137246
}
138247
```
139248

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).
249+
**UUID Version 6 (Draft/Upcoming):** An experimental or proposed version focused on improving time-based UUIDs with more
250+
sortable properties (not yet widely adopted).
141251

142252
```php
143253
use Cycle\Annotated\Annotation\Column;
@@ -154,7 +264,8 @@ class User
154264
}
155265
```
156266

157-
**UUID Version 7 (Draft/Upcoming):** A newer proposal designed to incorporate sortable features based on Unix timestamp, enhancing performance in database indexing.
267+
**UUID Version 7 (Draft/Upcoming):** A newer proposal designed to incorporate sortable features based on Unix timestamp,
268+
enhancing performance in database indexing.
158269

159270
```php
160271
use Cycle\Annotated\Annotation\Column;
@@ -171,7 +282,7 @@ class User
171282
}
172283
```
173284

174-
You can find more information about Entity behavior UUID [here](https://cycle-orm.dev/docs/entity-behaviors-identifier).
285+
Read more about identifier generation in [Entity Behaviors: Identifiers](https://cycle-orm.dev/docs/entity-behaviors/identifiers.md).
175286

176287
## License:
177288

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"require": {
4343
"php": ">=8.2",
44-
"cycle/entity-behavior": "^1.6",
44+
"cycle/entity-behavior": "^1.7",
4545
"ramsey/identifier": "^0.1"
4646
},
4747
"require-dev": {

0 commit comments

Comments
 (0)