|
2 | 2 | [](LICENSE)
|
3 | 3 | [](http://www.php.net)
|
4 | 4 |
|
5 |
| -Gember Serializer ([gember/event-sourcing](https://github.com/GemberPHP/event-sourcing)) implementation based on [symfony/serializer](https://github.com/symfony/serializer). |
| 5 | +[Gember Event Sourcing](https://github.com/GemberPHP/event-sourcing) Serializer adapter based on [symfony/serializer](https://github.com/symfony/serializer). |
| 6 | + |
| 7 | +> All external dependencies in Gember Event Sourcing are organized into separate packages, |
| 8 | +> making it easy to swap out a vendor adapter for another. |
| 9 | +
|
| 10 | +## Installation |
| 11 | +Install with Composer: |
| 12 | +```bash |
| 13 | +composer require gember/symfony-serializer |
| 14 | +``` |
| 15 | + |
| 16 | +## Configuration |
| 17 | +Bind this adapter to the `Serializer` interface in your service definitions. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +#### Vanilla PHP |
| 22 | +```php |
| 23 | +use Gember\SerializerSymfony\SymfonySerializer; |
| 24 | +use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 25 | +use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; |
| 26 | +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
| 27 | +use Symfony\Component\Serializer\Serializer; |
| 28 | + |
| 29 | +$serializer = new SymfonySerializer( |
| 30 | + new Serializer( |
| 31 | + [ |
| 32 | + new DateTimeNormalizer([ |
| 33 | + DateTimeNormalizer::FORMAT_KEY => 'Y-m-d\TH:i:s.uP', |
| 34 | + ]), |
| 35 | + new ObjectNormalizer(), |
| 36 | + ], |
| 37 | + [ |
| 38 | + new JsonEncoder(), |
| 39 | + ], |
| 40 | + ), |
| 41 | +); |
| 42 | +``` |
| 43 | + |
| 44 | +#### Symfony |
| 45 | +It is recommended to use the [Symfony bundle](https://github.com/GemberPHP/event-sourcing-symfony-bundle) to configure Gember Event Sourcing. |
| 46 | +With this bundle, the adapter is automatically set as the default for the Serializer. |
| 47 | + |
| 48 | +If you're not using the bundle, you can either add the serializer to the existing stack of serializers or bind it directly to the `Serializer` interface. |
| 49 | + |
| 50 | +Option 1: Add to the existing stack of serializers: |
| 51 | +```yaml |
| 52 | +Gember\SerializerSymfony\SymfonySerializer: |
| 53 | + arguments: |
| 54 | + - '@serializer' # or any other Symfony Serializer definition of your choice |
| 55 | + |
| 56 | +Gember\EventSourcing\Util\Serialization\Serializer\SerializableDomainEvent\SerializableDomainEventSerializer: ~ |
| 57 | + |
| 58 | +Gember\EventSourcing\Util\Serialization\Serializer\Serializer: |
| 59 | + class: Gember\EventSourcing\Util\Serialization\Serializer\Stacked\StackedSerializer |
| 60 | + arguments: |
| 61 | + - [ |
| 62 | + '@Gember\EventSourcing\Util\Serialization\Serializer\SerializableDomainEvent\SerializableDomainEventSerializer', |
| 63 | + '@Gember\SerializerSymfony\SymfonySerializer' # added to stack of serializers |
| 64 | + ] |
| 65 | +``` |
| 66 | + |
| 67 | +Option 2: Bind directly to `Serializer` interface: |
| 68 | +```yaml |
| 69 | +Gember\EventSourcing\Util\Serialization\Serializer\Serializer: |
| 70 | + class: Gember\SerializerSymfony\SymfonySerializer |
| 71 | + arguments: |
| 72 | + - '@serializer' # or any other Symfony Serializer definition of your choice |
| 73 | +``` |
0 commit comments