Skip to content

Commit fb6df50

Browse files
Provides fixes after merged changes broke a lot of things
1 parent 61ec5ef commit fb6df50

File tree

12 files changed

+159
-64
lines changed

12 files changed

+159
-64
lines changed

src/Listener/SnowflakeDiscord.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,9 @@ protected function createValue(): DiscordSnowflake
6666

6767
/**
6868
* Get the current process ID.
69-
*
70-
* @return int<0, 281474976710655>
7169
*/
7270
private function getProcessId(): int
7371
{
74-
return self::$defaultProcessId ??= \getmypid();
72+
return self::$defaultProcessId === null ? \intval(\getmypid()) : self::$defaultProcessId;
7573
}
7674
}

src/Listener/Uuid3.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
use Cycle\ORM\Entity\Behavior\Identifier\Listener\BaseUuid as Base;
88
use Ramsey\Identifier\Uuid;
99
use Ramsey\Identifier\Uuid\NamespaceId;
10-
use Ramsey\Identifier\Uuid\UuidV3Factory;
10+
use Ramsey\Identifier\Uuid\UuidFactory;
1111

1212
/**
1313
* Generates UUIDv3 (name-based with MD5 hashing) identifiers for entities.
1414
*/
1515
final class Uuid3 extends Base
1616
{
17-
private UuidV3Factory $factory;
17+
private UuidFactory $factory;
1818

1919
/**
2020
* @param non-empty-string $field The name of the field to store the UUID
21-
* @param NamespaceId|BaseUuid|string $namespace The namespace UUID
21+
* @param NamespaceId|Uuid|string $namespace The namespace UUID
2222
* @param string $name The name to hash
2323
* @param bool $nullable Indicates whether the UUID can be null
2424
*/
@@ -28,13 +28,13 @@ public function __construct(
2828
private readonly string $name,
2929
bool $nullable = false,
3030
) {
31-
$this->factory = new UuidV3Factory();
31+
$this->factory = new UuidFactory();
3232
parent::__construct($field, $nullable);
3333
}
3434

3535
#[\Override]
3636
protected function createValue(): \Ramsey\Identifier\Uuid
3737
{
38-
return $this->factory->create($this->namespace, $this->name);
38+
return $this->factory->v3($this->namespace, $this->name);
3939
}
4040
}

src/Listener/Uuid5.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
use Cycle\ORM\Entity\Behavior\Identifier\Listener\BaseUuid as Base;
88
use Ramsey\Identifier\Uuid;
99
use Ramsey\Identifier\Uuid\NamespaceId;
10-
use Ramsey\Identifier\Uuid\UuidV5Factory;
10+
use Ramsey\Identifier\Uuid\UuidFactory;
1111

1212
/**
1313
* Generates UUIDv5 (name-based with SHA-1 hashing) identifiers for entities.
1414
*/
1515
final class Uuid5 extends Base
1616
{
17-
private UuidV5Factory $factory;
17+
private UuidFactory $factory;
1818

1919
/**
2020
* @param non-empty-string $field The name of the field to store the UUID
21-
* @param NamespaceId|BaseUuid|string $namespace The namespace UUID
21+
* @param NamespaceId|Uuid|string $namespace The namespace UUID
2222
* @param string $name The name to hash
2323
* @param bool $nullable Indicates whether the UUID can be null
2424
*/
@@ -28,13 +28,13 @@ public function __construct(
2828
private readonly string $name,
2929
bool $nullable = false,
3030
) {
31-
$this->factory = new UuidV5Factory();
31+
$this->factory = new UuidFactory();
3232
parent::__construct($field, $nullable);
3333
}
3434

3535
#[\Override]
3636
protected function createValue(): \Ramsey\Identifier\Uuid
3737
{
38-
return $this->factory->create($this->namespace, $this->name);
38+
return $this->factory->v5($this->namespace, $this->name);
3939
}
4040
}

src/Snowflake.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Cycle\ORM\Entity\Behavior\Identifier;
66

7+
use Cycle\Database\DatabaseInterface;
78
use Cycle\ORM\Entity\Behavior\Schema\BaseModifier;
89
use Cycle\ORM\Entity\Behavior\Schema\RegistryModifier;
910
use Cycle\ORM\Schema\GeneratedField;
1011
use Cycle\Schema\Registry;
11-
use Ramsey\Identifier\SnowflakeFactory;
1212

1313
abstract class Snowflake extends BaseModifier
1414
{
@@ -24,6 +24,7 @@ abstract class Snowflake extends BaseModifier
2424
public function compute(Registry $registry): void
2525
{
2626
$modifier = new RegistryModifier($registry, $this->role);
27+
/** @var non-empty-string column */
2728
$this->column = $modifier->findColumnName($this->field, $this->column);
2829
if (\is_string($this->column) && $this->column !== '') {
2930
$modifier->addSnowflakeColumn(
@@ -32,11 +33,9 @@ public function compute(Registry $registry): void
3233
$this->nullable ? null : GeneratedField::BEFORE_INSERT,
3334
)->nullable($this->nullable);
3435

35-
$factory = $this->snowflakeFactory();
36-
3736
$modifier->setTypecast(
3837
$registry->getEntity($this->role)->getFields()->get($this->field),
39-
[$factory, 'createFromInteger'],
38+
[static::class, 'fromInteger', $this->getTypecastArgs()],
4039
);
4140
}
4241
}
@@ -54,13 +53,20 @@ public function render(Registry $registry): void
5453
$this->nullable ? null : GeneratedField::BEFORE_INSERT,
5554
)->nullable($this->nullable);
5655

57-
$factory = $this->snowflakeFactory();
58-
5956
$modifier->setTypecast(
6057
$registry->getEntity($this->role)->getFields()->get($this->field),
61-
[$factory, 'createFromInteger'],
58+
[static::class, 'fromInteger', $this->getTypecastArgs()],
6259
);
6360
}
6461

65-
abstract protected function snowflakeFactory(): SnowflakeFactory;
62+
/**
63+
* @param int<0, max>|numeric-string $identifier
64+
*/
65+
abstract public static function fromInteger(
66+
int|string $identifier,
67+
DatabaseInterface $database,
68+
array $arguments,
69+
): \Ramsey\Identifier\Snowflake;
70+
71+
abstract protected function getTypecastArgs(): array;
6672
}

src/SnowflakeDiscord.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Cycle\ORM\Entity\Behavior\Identifier;
66

7+
use Cycle\Database\DatabaseInterface;
78
use Cycle\ORM\Entity\Behavior\Identifier\Snowflake as BaseSnowflake;
89
use Cycle\ORM\Entity\Behavior\Identifier\Listener\SnowflakeDiscord as Listener;
910
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
1011
use Ramsey\Identifier\Snowflake\DiscordSnowflakeFactory;
11-
use Ramsey\Identifier\SnowflakeFactory;
1212

1313
/**
1414
* A Snowflake identifier for use with the Discord voice, text, and streaming video platform
@@ -30,7 +30,7 @@ final class SnowflakeDiscord extends BaseSnowflake
3030
* @see \Ramsey\Identifier\Snowflake\DiscordSnowflakeFactory::create()
3131
*/
3232
public function __construct(
33-
string $field,
33+
string $field = 'snowflake',
3434
?string $column = null,
3535
private readonly ?int $workerId = null,
3636
private readonly ?int $processId = null,
@@ -41,6 +41,18 @@ public function __construct(
4141
$this->nullable = $nullable;
4242
}
4343

44+
#[\Override]
45+
public static function fromInteger(
46+
int|string $identifier,
47+
DatabaseInterface $database,
48+
array $arguments,
49+
): \Ramsey\Identifier\Snowflake {
50+
return (new DiscordSnowflakeFactory(
51+
$arguments['workerId'],
52+
$arguments['processId'],
53+
))->createFromInteger($identifier);
54+
}
55+
4456
#[\Override]
4557
protected function getListenerClass(): string
4658
{
@@ -67,8 +79,11 @@ protected function getListenerArgs(): array
6779
}
6880

6981
#[\Override]
70-
protected function snowflakeFactory(): SnowflakeFactory
82+
protected function getTypecastArgs(): array
7183
{
72-
return new DiscordSnowflakeFactory($this->workerId, $this->processId);
84+
return [
85+
'workerId' => $this->workerId,
86+
'processId' => $this->processId,
87+
];
7388
}
7489
}

src/SnowflakeGeneric.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace Cycle\ORM\Entity\Behavior\Identifier;
66

7+
use Cycle\Database\DatabaseInterface;
78
use Cycle\ORM\Entity\Behavior\Identifier\Snowflake as BaseSnowflake;
89
use Cycle\ORM\Entity\Behavior\Identifier\Listener\SnowflakeGeneric as Listener;
910
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
1011
use Ramsey\Identifier\Snowflake\Epoch;
1112
use Ramsey\Identifier\Snowflake\GenericSnowflakeFactory;
12-
use Ramsey\Identifier\SnowflakeFactory;
1313

1414
/**
1515
* A distributed ID generation system developed by Twitter that produces
@@ -32,7 +32,7 @@ final class SnowflakeGeneric extends BaseSnowflake
3232
* @see \Ramsey\Identifier\Snowflake\GenericSnowflakeFactory::create()
3333
*/
3434
public function __construct(
35-
string $field,
35+
string $field = 'snowflake',
3636
?string $column = null,
3737
private readonly ?int $node = null,
3838
private readonly Epoch|int|null $epochOffset = null,
@@ -43,6 +43,18 @@ public function __construct(
4343
$this->nullable = $nullable;
4444
}
4545

46+
#[\Override]
47+
public static function fromInteger(
48+
int|string $identifier,
49+
DatabaseInterface $database,
50+
array $arguments,
51+
): \Ramsey\Identifier\Snowflake {
52+
return (new GenericSnowflakeFactory(
53+
$arguments['node'],
54+
$arguments['epochOffset'],
55+
))->createFromInteger($identifier);
56+
}
57+
4658
#[\Override]
4759
protected function getListenerClass(): string
4860
{
@@ -69,8 +81,11 @@ protected function getListenerArgs(): array
6981
}
7082

7183
#[\Override]
72-
protected function snowflakeFactory(): SnowflakeFactory
84+
protected function getTypecastArgs(): array
7385
{
74-
return new GenericSnowflakeFactory($this->node, $this->epochOffset);
86+
return [
87+
'node' => $this->node,
88+
'epochOffset' => $this->epochOffset,
89+
];
7590
}
7691
}

src/SnowflakeInstagram.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Cycle\ORM\Entity\Behavior\Identifier;
66

7+
use Cycle\Database\DatabaseInterface;
78
use Cycle\ORM\Entity\Behavior\Identifier\Snowflake as BaseSnowflake;
89
use Cycle\ORM\Entity\Behavior\Identifier\Listener\SnowflakeInstagram as Listener;
910
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
1011
use Ramsey\Identifier\Snowflake\InstagramSnowflakeFactory;
11-
use Ramsey\Identifier\SnowflakeFactory;
1212

1313
/**
1414
* A Snowflake identifier for use with the Instagram photo and video sharing social media platform
@@ -29,7 +29,7 @@ final class SnowflakeInstagram extends BaseSnowflake
2929
* @see \Ramsey\Identifier\Snowflake\InstagramSnowflakeFactory::create()
3030
*/
3131
public function __construct(
32-
string $field,
32+
string $field = 'snowflake',
3333
?string $column = null,
3434
private readonly ?int $shardId = null,
3535
bool $nullable = false,
@@ -39,6 +39,17 @@ public function __construct(
3939
$this->nullable = $nullable;
4040
}
4141

42+
#[\Override]
43+
public static function fromInteger(
44+
int|string $identifier,
45+
DatabaseInterface $database,
46+
array $arguments,
47+
): \Ramsey\Identifier\Snowflake {
48+
return (new InstagramSnowflakeFactory(
49+
$arguments['shardId'],
50+
))->createFromInteger($identifier);
51+
}
52+
4253
#[\Override]
4354
protected function getListenerClass(): string
4455
{
@@ -63,8 +74,10 @@ protected function getListenerArgs(): array
6374
}
6475

6576
#[\Override]
66-
protected function snowflakeFactory(): SnowflakeFactory
77+
protected function getTypecastArgs(): array
6778
{
68-
return new InstagramSnowflakeFactory($this->shardId);
79+
return [
80+
'shardId' => $this->shardId,
81+
];
6982
}
7083
}

src/SnowflakeMastodon.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Cycle\ORM\Entity\Behavior\Identifier;
66

7+
use Cycle\Database\DatabaseInterface;
78
use Cycle\ORM\Entity\Behavior\Identifier\Snowflake as BaseSnowflake;
89
use Cycle\ORM\Entity\Behavior\Identifier\Listener\SnowflakeMastodon as Listener;
910
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
1011
use Ramsey\Identifier\Snowflake\MastodonSnowflakeFactory;
11-
use Ramsey\Identifier\SnowflakeFactory;
1212

1313
/**
1414
* A Snowflake identifier for use with the Mastodon open source platform for decentralized social networking
@@ -29,7 +29,7 @@ final class SnowflakeMastodon extends BaseSnowflake
2929
* @see \Ramsey\Identifier\Snowflake\MastodonSnowflakeFactory::create()
3030
*/
3131
public function __construct(
32-
string $field,
32+
string $field = 'snowflake',
3333
?string $column = null,
3434
private readonly ?string $tableName = null,
3535
bool $nullable = false,
@@ -39,6 +39,17 @@ public function __construct(
3939
$this->nullable = $nullable;
4040
}
4141

42+
#[\Override]
43+
public static function fromInteger(
44+
int|string $identifier,
45+
DatabaseInterface $database,
46+
array $arguments,
47+
): \Ramsey\Identifier\Snowflake {
48+
return (new MastodonSnowflakeFactory(
49+
$arguments['tableName'],
50+
))->createFromInteger($identifier);
51+
}
52+
4253
#[\Override]
4354
protected function getListenerClass(): string
4455
{
@@ -63,8 +74,10 @@ protected function getListenerArgs(): array
6374
}
6475

6576
#[\Override]
66-
protected function snowflakeFactory(): SnowflakeFactory
77+
protected function getTypecastArgs(): array
6778
{
68-
return new MastodonSnowflakeFactory($this->tableName);
79+
return [
80+
'tableName' => $this->tableName,
81+
];
6982
}
7083
}

0 commit comments

Comments
 (0)