Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| CurrencyType | |
100.00% |
13 / 13 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| getSQLDeclaration | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| convertToPHPValue | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| convertToDatabaseValue | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Shared\Infrastructure\Persistence\Doctrine\Type; |
| 6 | |
| 7 | use App\Shared\Domain\ValueObject\Currency; |
| 8 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 9 | use Doctrine\DBAL\Types\Type; |
| 10 | |
| 11 | final class CurrencyType extends Type |
| 12 | { |
| 13 | public const NAME = 'currency'; |
| 14 | |
| 15 | public function getSQLDeclaration(array $column, AbstractPlatform $platform): string |
| 16 | { |
| 17 | $column['length'] = 3; |
| 18 | $column['fixed'] = true; |
| 19 | |
| 20 | return $platform->getStringTypeDeclarationSQL($column); |
| 21 | } |
| 22 | |
| 23 | public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Currency |
| 24 | { |
| 25 | if ($value === null) { |
| 26 | return null; |
| 27 | } |
| 28 | assert(is_string($value)); |
| 29 | |
| 30 | return new Currency($value); |
| 31 | } |
| 32 | |
| 33 | public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string |
| 34 | { |
| 35 | if ($value === null) { |
| 36 | return null; |
| 37 | } |
| 38 | if ($value instanceof Currency) { |
| 39 | return $value->code(); |
| 40 | } |
| 41 | assert(is_string($value)); |
| 42 | |
| 43 | return $value; |
| 44 | } |
| 45 | } |