Skip to content

Commit

Permalink
Add UUID column support
Browse files Browse the repository at this point in the history
  • Loading branch information
compositephp committed Oct 1, 2023
1 parent a81ea52 commit 49f6e4d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
}
],
"require": {
"php": "^8.1"
"php": "^8.1",
"ramsey/uuid": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "^10.1",
Expand Down
2 changes: 2 additions & 0 deletions src/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Composite\Entity\Columns;
use Composite\Entity\Exceptions\EntityException;
use Ramsey\Uuid\UuidInterface;

class ColumnBuilder
{
Expand All @@ -14,6 +15,7 @@ class ColumnBuilder
'float' => Columns\FloatColumn::class,
'int' => Columns\IntegerColumn::class,
'string' => Columns\StringColumn::class,
UuidInterface::class => Columns\UuidColumn::class,
\stdClass::class => Columns\ObjectColumn::class,
\DateTime::class => Columns\DateTimeColumn::class,
\DateTimeImmutable::class => Columns\DateTimeColumn::class,
Expand Down
23 changes: 23 additions & 0 deletions src/Columns/UuidColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Composite\Entity\Columns;

use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

class UuidColumn extends AbstractColumn
{
public function cast(mixed $dbValue): UuidInterface
{
return Uuid::fromString((string)$dbValue);
}

/**
* @param UuidInterface $entityValue
* @return string
*/
public function uncast(mixed $entityValue): string
{
return $entityValue->toString();
}
}
83 changes: 83 additions & 0 deletions tests/Columns/UuidColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php declare(strict_types=1);

namespace Composite\Entity\Tests\Columns;

use Composite\Entity\AbstractEntity;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

final class UuidColumnTest extends \PHPUnit\Framework\TestCase
{
public static function cast_dataProvider(): array
{
return [
[
'value' => null,
'expected' => null,
],
[
'value' => '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
'expected' => Uuid::fromString('6ba7b810-9dad-11d1-80b4-00c04fd430c8'),
],
[
'value' => '550e8400-e29b-41d4-a716-446655440000',
'expected' => Uuid::fromString('550e8400-e29b-41d4-a716-446655440000'),
],
[
'value' => 'invalid_uuid',
'expected' => null,
],
];
}

/**
* @dataProvider cast_dataProvider
*/
public function test_cast(mixed $value, ?UuidInterface $expected): void
{
$class = new class extends AbstractEntity {
public function __construct(
public ?UuidInterface $column = null,
) {}
};
$entity = $class::fromArray(['column' => $value]);
$this->assertEquals($expected, $entity->column);
}

public static function uncast_dataProvider(): array
{
return [
[
'value' => null,
'expected' => null,
],
[
'value' => Uuid::fromString('6ba7b810-9dad-11d1-80b4-00c04fd430c8'),
'expected' => '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
],
[
'value' => Uuid::fromString('550e8400-e29b-41d4-a716-446655440000'),
'expected' => '550e8400-e29b-41d4-a716-446655440000',
],
];
}

/**
* @dataProvider uncast_dataProvider
*/
public function test_uncast(?UuidInterface $value, mixed $expected): void
{
$entity = new class($value) extends AbstractEntity {
public function __construct(
public ?UuidInterface $column,
) {}
};
$actual = $entity->toArray()['column'];
$this->assertEquals($expected, $actual);

$newEntity = $entity::fromArray(['column' => $actual]);
$newActual = $newEntity->toArray()['column'];
$this->assertEquals($entity->column, $newEntity->column);
$this->assertEquals($expected, $newActual);
}
}

0 comments on commit 49f6e4d

Please sign in to comment.