-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add version 7 UUID support * Bump ramsey/uuid to 4.5
- Loading branch information
Showing
4 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\ORM\Entity\Behavior\Uuid\Listener; | ||
|
||
use Cycle\ORM\Entity\Behavior\Attribute\Listen; | ||
use Cycle\ORM\Entity\Behavior\Event\Mapper\Command\OnCreate; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
final class Uuid7 | ||
{ | ||
public function __construct( | ||
private string $field = 'uuid' | ||
) { | ||
} | ||
|
||
#[Listen(OnCreate::class)] | ||
public function __invoke(OnCreate $event): void | ||
{ | ||
if (!isset($event->state->getData()[$this->field])) { | ||
$event->state->register($this->field, Uuid::uuid7()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\ORM\Entity\Behavior\Uuid; | ||
|
||
use Cycle\ORM\Entity\Behavior\Uuid\Listener\Uuid7 as Listener; | ||
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; | ||
use Doctrine\Common\Annotations\Annotation\Target; | ||
use JetBrains\PhpStorm\ArrayShape; | ||
|
||
/** | ||
* Uses a version 7 (Unix Epoch Time) UUID | ||
* | ||
* @Annotation | ||
* @NamedArgumentConstructor() | ||
* @Target({"CLASS"}) | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor] | ||
final class Uuid7 extends Uuid | ||
{ | ||
/** | ||
* @param non-empty-string $field Uuid property name | ||
* @param non-empty-string|null $column Uuid column name | ||
* | ||
* @see \Ramsey\Uuid\UuidFactoryInterface::uuid7() | ||
*/ | ||
public function __construct( | ||
string $field = 'uuid', | ||
?string $column = null | ||
) { | ||
$this->field = $field; | ||
$this->column = $column; | ||
} | ||
|
||
protected function getListenerClass(): string | ||
{ | ||
return Listener::class; | ||
} | ||
|
||
#[ArrayShape(['field' => 'string'])] | ||
protected function getListenerArgs(): array | ||
{ | ||
return [ | ||
'field' => $this->field | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters