Skip to content

Commit

Permalink
Simplify Uuid type class
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Sep 30, 2023
1 parent c7c21a6 commit 1227d1c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/DSL/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ final public static function structure(string $name, RowEntry ...$entries) : Row
/**
* @return RowEntry\UuidEntry
*/
final public static function uuid(string $name, Uuid|string $value) : RowEntry
final public static function uuid(string $name, string $value) : RowEntry
{
return new RowEntry\UuidEntry($name, $value);
return new RowEntry\UuidEntry($name, Uuid::fromString($value));
}

/**
Expand Down
17 changes: 12 additions & 5 deletions src/core/etl/src/Flow/ETL/Row/Entry/Type/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class Uuid
*/
public const UUID_REGEXP = '/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/ms';

private readonly \Ramsey\Uuid\UuidInterface|\Symfony\Component\Uid\Uuid $value;
private readonly string $value;

/**
* @throws InvalidArgumentException|RuntimeException
Expand All @@ -23,27 +23,34 @@ public function __construct(string|\Ramsey\Uuid\UuidInterface|\Symfony\Component
if (\is_string($value)) {
try {
if (\class_exists(\Ramsey\Uuid\UuidInterface::class)) {
$this->value = \Ramsey\Uuid\Uuid::fromString($value);
$this->value = (string) \Ramsey\Uuid\Uuid::fromString($value);
} elseif (\class_exists(\Symfony\Component\Uid\Uuid::class)) {
$this->value = \Symfony\Component\Uid\Uuid::fromString($value);
$this->value = \Symfony\Component\Uid\Uuid::fromString($value)->toRfc4122();
} else {
throw new RuntimeException("\Ramsey\Uuid\Uuid nor \Symfony\Component\Uid\Uuid class not found, please add 'ramsey/uuid' or 'symfony/uid' as a dependency to the project first.");
}
} catch (\InvalidArgumentException $e) {
throw new InvalidArgumentException("Invalid UUID: '{$value}'", 0, $e);
}
} elseif ($value instanceof \Ramsey\Uuid\UuidInterface) {
$this->value = $value->toString();
} else {
$this->value = $value;
$this->value = $value->toRfc4122();
}
}

public static function fromString(string $value) : self
{
return new self($value);
}

public function isEqual(self $type) : bool
{
return $this->toString() === $type->toString();
}

public function toString() : string
{
return (string) $this->value;
return $this->value;
}
}
12 changes: 4 additions & 8 deletions src/core/etl/src/Flow/ETL/Row/Entry/UuidEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,18 @@ final class UuidEntry implements \Stringable, Entry
/**
* @throws InvalidArgumentException|RuntimeException
*/
public function __construct(private readonly string $name, string|Entry\Type\Uuid $value)
public function __construct(private readonly string $name, Entry\Type\Uuid $value)
{
if ('' === $name) {
throw InvalidArgumentException::because('Entry name cannot be empty');
}

if (\is_string($value)) {
$this->value = new Entry\Type\Uuid($value);
} else {
$this->value = $value;
}
$this->value = $value;
}

public static function from(string $name, string $value) : self
{
return new self($name, $value);
return new self($name, Entry\Type\Uuid::fromString($value));
}

public function __serialize() : array
Expand Down Expand Up @@ -90,7 +86,7 @@ public function name() : string
*/
public function rename(string $name) : Entry
{
return new self($name, $this->value->toString());
return new self($name, $this->value);
}

public function toString() : string
Expand Down
5 changes: 3 additions & 2 deletions src/core/etl/src/Flow/ETL/Row/Factory/NativeEntryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Row;
use Flow\ETL\Row\Entry;
use Flow\ETL\Row\Entry\Type\Uuid;
use Flow\ETL\Row\Entry\TypedCollection\Type;
use Flow\ETL\Row\EntryFactory;
use Flow\ETL\Row\Schema;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function create(string $entryName, mixed $value) : Entry
}

if ($this->isUuid($value)) {
return new Row\Entry\UuidEntry($entryName, $value);
return new Row\Entry\UuidEntry($entryName, Uuid::fromString($value));
}

if ($this->isXML($value)) {
Expand Down Expand Up @@ -204,7 +205,7 @@ private function fromDefinition(Schema\Definition $definition, mixed $value) : E
}

if ($type === Entry\UuidEntry::class && (\is_string($value) || $value instanceof Entry\Type\Uuid)) {
return EntryDSL::uuid($definition->entry()->name(), $value);
return EntryDSL::uuid($definition->entry()->name(), \is_string($value) ? $value : $value->toString());
}

if ($type === Entry\ObjectEntry::class && \is_object($value)) {
Expand Down
25 changes: 13 additions & 12 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/UuidEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Flow\ETL\Tests\Unit\Row\Entry;

use Flow\ETL\Row\Entry\Type\Uuid;
use Flow\ETL\Row\Entry\UuidEntry;
use PHPUnit\Framework\TestCase;

Expand All @@ -13,23 +14,23 @@ public static function is_equal_data_provider() : \Generator
{
yield 'equal names and values' => [
true,
new UuidEntry('name', '00000000-0000-0000-0000-000000000000'),
new UuidEntry('name', '00000000-0000-0000-0000-000000000000'),
new UuidEntry('name', Uuid::fromString('00000000-0000-0000-0000-000000000000')),
new UuidEntry('name', Uuid::fromString('00000000-0000-0000-0000-000000000000')),
];
yield 'different names and values' => [
false,
new UuidEntry('name', '00000000-0000-0000-0000-000000000000'),
new UuidEntry('different_name', '11111111-1111-1111-1111-111111111111'),
new UuidEntry('name', Uuid::fromString('00000000-0000-0000-0000-000000000000')),
new UuidEntry('different_name', Uuid::fromString('11111111-1111-1111-1111-111111111111')),
];
yield 'equal names and different values' => [
false,
new UuidEntry('name', '00000000-0000-0000-0000-000000000000'),
new UuidEntry('name', '11111111-1111-1111-1111-111111111111'),
new UuidEntry('name', Uuid::fromString('00000000-0000-0000-0000-000000000000')),
new UuidEntry('name', Uuid::fromString('11111111-1111-1111-1111-111111111111')),
];
yield 'different names characters and equal values' => [
false,
new UuidEntry('NAME', '00000000-0000-0000-0000-000000000000'),
new UuidEntry('name', '00000000-0000-0000-0000-000000000000'),
new UuidEntry('NAME', Uuid::fromString('00000000-0000-0000-0000-000000000000')),
new UuidEntry('name', Uuid::fromString('00000000-0000-0000-0000-000000000000')),
];
}

Expand Down Expand Up @@ -68,7 +69,7 @@ public function test_is_equal(bool $equals, UuidEntry $entry, UuidEntry $nextEnt

public function test_map() : void
{
$entry = new UuidEntry('entry-name', '00000000-0000-0000-0000-000000000000');
$entry = new UuidEntry('entry-name', Uuid::fromString('00000000-0000-0000-0000-000000000000'));

$this->assertEquals(
$entry,
Expand All @@ -87,16 +88,16 @@ public function test_prevents_from_creating_entry_with_empty_entry_name() : void
{
$this->expectExceptionMessage('Entry name cannot be empty');

new UuidEntry('', '00000000-0000-0000-0000-000000000000');
new UuidEntry('', Uuid::fromString('00000000-0000-0000-0000-000000000000'));
}

public function test_renames_entry() : void
{
$entry = new UuidEntry('entry-name', $uuid = '00000000-0000-0000-0000-000000000000');
$entry = new UuidEntry('entry-name', $uuid = Uuid::fromString('00000000-0000-0000-0000-000000000000'));
/** @var UuidEntry $newEntry */
$newEntry = $entry->rename('new-entry-name');

$this->assertEquals('new-entry-name', $newEntry->name());
$this->assertEquals($uuid, $newEntry->value()->toString());
$this->assertEquals($uuid->toString(), $newEntry->value()->toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Flow\ETL\DSL\Entry;
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Row\Entry\Type\Uuid;
use Flow\ETL\Row\Entry\TypedCollection\ObjectType;
use Flow\ETL\Row\Entry\TypedCollection\ScalarType;
use Flow\ETL\Row\Factory\NativeEntryFactory;
Expand Down Expand Up @@ -277,7 +276,7 @@ public function test_uuid_from_ramsey_uuid_library() : void
}

$this->assertEquals(
Entry::uuid('e', $uuid = new Uuid(\Ramsey\Uuid\Uuid::uuid4())),
Entry::uuid('e', $uuid = \Ramsey\Uuid\Uuid::uuid4()->toString()),
(new NativeEntryFactory())->create('e', $uuid)
);
}
Expand Down

0 comments on commit 1227d1c

Please sign in to comment.