Skip to content

Commit

Permalink
Add version 7 UUID support (#10)
Browse files Browse the repository at this point in the history
* Add version 7 UUID support

* Bump ramsey/uuid to 4.5
  • Loading branch information
sunxyw authored May 9, 2023
1 parent 72b113c commit 95b63ba
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"require": {
"php": ">=8.0",
"cycle/entity-behavior": "^1.0",
"ramsey/uuid": "^4.0"
"ramsey/uuid": "^4.5"
},
"require-dev": {
"cycle/annotated": "^3.0",
Expand Down
25 changes: 25 additions & 0 deletions src/Listener/Uuid7.php
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());
}
}
}
48 changes: 48 additions & 0 deletions src/Uuid7.php
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
];
}
}
16 changes: 16 additions & 0 deletions tests/Uuid/Functional/Driver/Common/Uuid/ListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Cycle\ORM\Entity\Behavior\Uuid\Tests\Functional\Driver\Common\Uuid;

use Cycle\ORM\Entity\Behavior\Uuid\Listener\Uuid7;
use Cycle\ORM\Entity\Behavior\Uuid\Tests\Fixtures\Uuid\User;
use Cycle\ORM\Entity\Behavior\Uuid\Tests\Functional\Driver\Common\BaseTest;
use Cycle\ORM\Entity\Behavior\Uuid\Tests\Traits\TableTrait;
Expand Down Expand Up @@ -159,6 +160,21 @@ public function testUuid6(): void
$this->assertIsString($data->uuid->toString());
}

public function testUuid7(): void
{
$this->withListeners(Uuid7::class);

$user = new User();
$this->save($user);

$select = new Select($this->orm->with(heap: new Heap()), User::class);
$data = $select->fetchOne();

$this->assertInstanceOf(UuidInterface::class, $data->uuid);
$this->assertSame(7, $data->uuid->getVersion());
$this->assertIsString($data->uuid->toString());
}

public function withListeners(array|string $listeners): void
{
$this->withSchema(new Schema([
Expand Down

0 comments on commit 95b63ba

Please sign in to comment.