-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to use Symfony UID for UUID generation
- Loading branch information
Showing
9 changed files
with
363 additions
and
8 deletions.
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,56 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Row\Entry\Type; | ||
|
||
use Flow\ETL\Exception\InvalidArgumentException; | ||
use Flow\ETL\Exception\RuntimeException; | ||
|
||
final class Uuid | ||
{ | ||
/** | ||
* This regexp is a port of the Uuid library, | ||
* which is copyright Ben Ramsey, @see https://github.com/ramsey/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 string $value; | ||
|
||
/** | ||
* @throws InvalidArgumentException|RuntimeException | ||
*/ | ||
public function __construct(string|\Ramsey\Uuid\UuidInterface|\Symfony\Component\Uid\Uuid $value) | ||
{ | ||
if (\is_string($value)) { | ||
try { | ||
if (\class_exists(\Ramsey\Uuid\UuidInterface::class)) { | ||
$this->value = (string) \Ramsey\Uuid\Uuid::fromString($value); | ||
} elseif (\class_exists(\Symfony\Component\Uid\Uuid::class)) { | ||
$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->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 $this->value; | ||
} | ||
} |
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,96 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Row\Entry; | ||
|
||
use Flow\ETL\Exception\InvalidArgumentException; | ||
use Flow\ETL\Row\Entry; | ||
use Flow\ETL\Row\Reference; | ||
use Flow\ETL\Row\Schema\Definition; | ||
|
||
/** | ||
* @implements Entry<Entry\Type\Uuid, array{name: string, value: string}> | ||
*/ | ||
final class UuidEntry implements \Stringable, Entry | ||
{ | ||
use EntryRef; | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function __construct(private readonly string $name, private readonly Entry\Type\Uuid $value) | ||
{ | ||
if ('' === $name) { | ||
throw InvalidArgumentException::because('Entry name cannot be empty'); | ||
} | ||
} | ||
|
||
public static function from(string $name, string $value) : self | ||
{ | ||
return new self($name, Entry\Type\Uuid::fromString($value)); | ||
} | ||
|
||
public function __serialize() : array | ||
{ | ||
return ['name' => $this->name, 'value' => $this->value->toString()]; | ||
} | ||
|
||
public function __toString() : string | ||
{ | ||
return $this->toString(); | ||
} | ||
|
||
public function __unserialize(array $data) : void | ||
{ | ||
$this->name = $data['name']; | ||
$this->value = new Entry\Type\Uuid($data['value']); | ||
} | ||
|
||
public function definition() : Definition | ||
{ | ||
return Definition::uuid($this->name); | ||
} | ||
|
||
public function is(string|Reference $name) : bool | ||
{ | ||
if ($name instanceof Reference) { | ||
return $this->name === $name->name(); | ||
} | ||
|
||
return $this->name === $name; | ||
} | ||
|
||
public function isEqual(Entry $entry) : bool | ||
{ | ||
return $this->is($entry->name()) && $entry instanceof self && $this->value()->isEqual($entry->value()); | ||
} | ||
|
||
public function map(callable $mapper) : Entry | ||
{ | ||
return new self($this->name, $mapper($this->value)); | ||
} | ||
|
||
public function name() : string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function rename(string $name) : Entry | ||
{ | ||
return new self($name, $this->value); | ||
} | ||
|
||
public function toString() : string | ||
{ | ||
return $this->value->toString(); | ||
} | ||
|
||
public function value() : Entry\Type\Uuid | ||
{ | ||
return $this->value; | ||
} | ||
} |
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
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
Oops, something went wrong.