Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| UuidType | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| getSQLDeclaration | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| convertToPHPValue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| convertToDatabaseValue | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| className | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Shared\Infrastructure\Persistence\Doctrine\Type; |
| 6 | |
| 7 | use App\Shared\Domain\ValueObject\Uuid; |
| 8 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 9 | use Doctrine\DBAL\Types\Type; |
| 10 | |
| 11 | /** |
| 12 | * Base Doctrine type for UUID value objects, stored as CHAR(36) (RFC 4122). |
| 13 | * One concrete subclass per typed id (UserId, TripId, ...). |
| 14 | * |
| 15 | * @template T of Uuid |
| 16 | */ |
| 17 | abstract class UuidType extends Type |
| 18 | { |
| 19 | public function getSQLDeclaration(array $column, AbstractPlatform $platform): string |
| 20 | { |
| 21 | $column['length'] = 36; |
| 22 | $column['fixed'] = true; |
| 23 | |
| 24 | return $platform->getStringTypeDeclarationSQL($column); |
| 25 | } |
| 26 | |
| 27 | public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Uuid |
| 28 | { |
| 29 | if ($value === null) { |
| 30 | return null; |
| 31 | } |
| 32 | assert(is_string($value)); |
| 33 | |
| 34 | $class = $this->className(); |
| 35 | |
| 36 | return new $class($value); |
| 37 | } |
| 38 | |
| 39 | public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string |
| 40 | { |
| 41 | if ($value === null) { |
| 42 | return null; |
| 43 | } |
| 44 | if ($value instanceof Uuid) { |
| 45 | return $value->value(); |
| 46 | } |
| 47 | assert(is_string($value)); |
| 48 | |
| 49 | return $value; |
| 50 | } |
| 51 | |
| 52 | /** @return class-string<T> */ |
| 53 | abstract protected function className(): string; |
| 54 | } |