|
| 1 | +[//]: # ( [](https://github.com/pixelfederation/doctrine-generic-types-bundle/actions?query=workflow%3ACI)) |
| 2 | +[//]: # ( [](https://codecov.io/gh/pixelfederation/doctrine-generic-types-bundle)) |
| 3 | +[//]: # ([](https://packagist.org/packages/pixelfederation/doctrine-generic-types-bundle)) |
| 4 | +[//]: # ([](https://packagist.org/packages/pixelfederation/doctrine-generic-types-bundle)) |
| 5 | + |
| 6 | +# PixelFederation DoctrineGenericTypesBundle |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +install via Composer: |
| 11 | + |
| 12 | +```bash |
| 13 | +composer require pixelfederation/doctrine-generic-types-bundle |
| 14 | +``` |
| 15 | + |
| 16 | +register bundle in `config/bundles.php` (if you don't use Symfony Flex) |
| 17 | + |
| 18 | +```php |
| 19 | +return [ |
| 20 | + // ... |
| 21 | + PixelFederation\DoctrineGenericTypesBundle\PixelFederationDoctrineGenericTypesBundle::class => ['all' => true], |
| 22 | +]; |
| 23 | +``` |
| 24 | + |
| 25 | +bundle configuration: |
| 26 | + |
| 27 | +```yaml |
| 28 | +# config/packages/pixel_federation_doctrine_generic_types.yaml |
| 29 | +pixel_federation_doctrine_generic_types: |
| 30 | + generic_types: |
| 31 | + PixelFederation\DoctrineGenericTypesBundle\Value\BooleanValue: PixelFederation\DoctrineGenericTypesBundle\Doctrine\Type\BooleanValueType |
| 32 | + PixelFederation\DoctrineGenericTypesBundle\Value\FloatValue: PixelFederation\DoctrineGenericTypesBundle\Doctrine\Type\FloatValueType |
| 33 | + PixelFederation\DoctrineGenericTypesBundle\Value\IntegerValue: PixelFederation\DoctrineGenericTypesBundle\Doctrine\Type\IntegerValueType |
| 34 | + PixelFederation\DoctrineGenericTypesBundle\Value\StringValue: PixelFederation\DoctrineGenericTypesBundle\Doctrine\Type\StringValueType |
| 35 | + # https://github.com/ramsey/uuid integration |
| 36 | + PixelFederation\DoctrineGenericTypesBundle\Bridge\RamseyUuid\Value\UuidValue: PixelFederation\DoctrineGenericTypesBundle\Bridge\RamseyUuid\Doctrine\Type\UuidValueType |
| 37 | + # Directories where to find your Value Objects |
| 38 | + directories: |
| 39 | + - ./src/App/Value |
| 40 | + - ./src/App/OtherValue |
| 41 | +``` |
| 42 | +
|
| 43 | +## Usage |
| 44 | +
|
| 45 | +Crete your Value Object: |
| 46 | +
|
| 47 | +```php |
| 48 | +<?php |
| 49 | + |
| 50 | +declare(strict_types=1); |
| 51 | + |
| 52 | +namespace App\Value; |
| 53 | + |
| 54 | +use PixelFederation\DoctrineGenericTypesBundle\Value\StringValue; |
| 55 | + |
| 56 | +final class FirstName extends StringValue |
| 57 | +{ |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +Use it in your Doctrine entity: |
| 62 | + |
| 63 | +```php |
| 64 | +<?php |
| 65 | + |
| 66 | +declare(strict_types=1); |
| 67 | + |
| 68 | +namespace App\Entity; |
| 69 | + |
| 70 | +use Doctrine\ORM\Mapping as ORM; |
| 71 | +use App\Value\FirstName; |
| 72 | + |
| 73 | +#[ORM\Entity] |
| 74 | +#[ORM\Table(name: 'person')] |
| 75 | +class Person |
| 76 | +{ |
| 77 | + public function __construct( |
| 78 | + #[ORM\Column(type: FirstName::class)] |
| 79 | + public FirstName $firstName, |
| 80 | + ) { |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +doctrine will handle persisting and retrieving your Value Object automatically. |
| 86 | + |
| 87 | +## How to create custom Generic Types |
| 88 | + |
| 89 | +Create abstract Value class extending `PixelFederation\DoctrineGenericTypesBundle\Value\Value` or `PixelFederation\DoctrineGenericTypesBundle\Value\BaseValue`: |
| 90 | + |
| 91 | +```php |
| 92 | +<?php |
| 93 | + |
| 94 | +declare(strict_types=1); |
| 95 | + |
| 96 | +namespace App\CustomValue; |
| 97 | + |
| 98 | +use PixelFederation\DoctrineGenericTypesBundle\Value\Value; |
| 99 | + |
| 100 | +abstract class MoneyValue implements Value |
| 101 | +{ |
| 102 | + public function __construct( |
| 103 | + public readonly float $value, |
| 104 | + public readonly string $currency, |
| 105 | + ) { |
| 106 | + // value object validation logic |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +Create Doctrine Type class extending `PixelFederation\DoctrineGenericTypesBundle\Doctrine\Type\GenericType`: |
| 112 | + |
| 113 | +```php |
| 114 | +<?php |
| 115 | + |
| 116 | +declare(strict_types=1); |
| 117 | + |
| 118 | +namespace App\Doctrine\Type; |
| 119 | + |
| 120 | +use App\CustomValue\MoneyValue; |
| 121 | +use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 122 | +use Doctrine\DBAL\Types\ConversionException; |
| 123 | +use Doctrine\DBAL\Types\JsonType; |
| 124 | +use Doctrine\DBAL\Types\Type; |
| 125 | +use InvalidArgumentException; |
| 126 | +use JsonException; |
| 127 | +use Override; |
| 128 | +use PixelFederation\DoctrineGenericTypesBundle\Doctrine\Type\GenericType; |
| 129 | + |
| 130 | +final class MoneyValueType extends JsonType implements GenericType |
| 131 | +{ |
| 132 | + /** |
| 133 | + * @var class-string<MoneyValue> |
| 134 | + */ |
| 135 | + protected string $class; |
| 136 | + |
| 137 | + public static function createForValue(string $class): Type |
| 138 | + { |
| 139 | + if (!is_a($class, MoneyValue::class, true)) { |
| 140 | + throw new InvalidArgumentException(sprintf( |
| 141 | + 'Doctrine Type %s must handle class %s. Got %s', |
| 142 | + self::class, |
| 143 | + MoneyValue::class, |
| 144 | + $class, |
| 145 | + )); |
| 146 | + } |
| 147 | + |
| 148 | + $self = new self(); |
| 149 | + $self->class = $class; |
| 150 | + |
| 151 | + return $self; |
| 152 | + } |
| 153 | + |
| 154 | + #[Override] |
| 155 | + public function getName(): string |
| 156 | + { |
| 157 | + return $this->class; |
| 158 | + } |
| 159 | + |
| 160 | + #[Override] |
| 161 | + public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string |
| 162 | + { |
| 163 | + if ($value === null) { |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + $class = $this->class; |
| 168 | + if (!$value instanceof $class) { |
| 169 | + throw ConversionException::conversionFailedInvalidType( |
| 170 | + $value, |
| 171 | + $this->getName(), |
| 172 | + ['null', $class], |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + try { |
| 177 | + return json_encode(['value' => $value->value, 'currency' => $value->currency], JSON_THROW_ON_ERROR); |
| 178 | + } catch (JsonException $e) { |
| 179 | + throw ConversionException::conversionFailedSerialization($value, 'json', $e->getMessage()); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @return object<MoneyValue>|null |
| 185 | + */ |
| 186 | + #[Override] |
| 187 | + public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed |
| 188 | + { |
| 189 | + if ($value === null) { |
| 190 | + return null; |
| 191 | + } |
| 192 | + |
| 193 | + if (!is_string($value)) { |
| 194 | + throw ConversionException::conversionFailedFormat($value, $this->getName(), 'json'); |
| 195 | + } |
| 196 | + |
| 197 | + try { |
| 198 | + $data = json_decode($value, true, 512, JSON_THROW_ON_ERROR); |
| 199 | + assert(is_array($data)); |
| 200 | + } catch (JsonException $e) { |
| 201 | + throw ConversionException::conversionFailedUnserialization($value, $e->getMessage()); |
| 202 | + } |
| 203 | + |
| 204 | + $dataValue = $data['value'] ?? null; |
| 205 | + $dataCurrency = $data['currency'] ?? null; |
| 206 | + if (!is_float($dataValue) || !is_string($dataCurrency)) { |
| 207 | + throw ConversionException::conversionFailedFormat( |
| 208 | + $value, |
| 209 | + $this->getName(), |
| 210 | + '{"value": float, "currency": string}', |
| 211 | + ); |
| 212 | + } |
| 213 | + |
| 214 | + return new ($this->class)($dataValue, $dataCurrency); |
| 215 | + } |
| 216 | + |
| 217 | + #[Override] |
| 218 | + public function requiresSQLCommentHint(AbstractPlatform $platform): bool |
| 219 | + { |
| 220 | + return false; |
| 221 | + } |
| 222 | +} |
| 223 | +``` |
| 224 | + |
| 225 | +Register your custom Generic Type in the bundle configuration: |
| 226 | + |
| 227 | +```yaml |
| 228 | +# config/packages/pixel_federation_doctrine_generic_types.yaml |
| 229 | +pixel_federation_doctrine_generic_types: |
| 230 | + generic_types: |
| 231 | + # ... |
| 232 | + App\CustomValue\MoneyValue: App\Doctrine\Type\MoneyValueType |
| 233 | + directories: |
| 234 | + # ... |
| 235 | + - ./src/App/CustomValue |
| 236 | +``` |
0 commit comments