|
| 1 | +.. index:: |
| 2 | + single: Serializer; Custom context builders |
| 3 | + |
| 4 | +How to Create your Custom Context Builder |
| 5 | +========================================= |
| 6 | + |
| 7 | +The :doc:`Serializer Component </components/serializer>` uses Normalizers |
| 8 | +and Encoders to transform any data to any data-structure (e.g. JSON). |
| 9 | +That serialization process could be configured thanks to a |
| 10 | +:ref:`serialization context <serializer-context>`, which can be built thanks to |
| 11 | +:ref:`context builders <component-serializer-context-builders>`. |
| 12 | + |
| 13 | +Each built-in normalizer/encoder has its related context builder. |
| 14 | +But, as an example, you may want to use custom context values |
| 15 | +for your :doc:`custom normalizers </serializer/custom_normalizer>` |
| 16 | +and create a custom context builder related to them. |
| 17 | + |
| 18 | +Creating a new context builder |
| 19 | +------------------------------ |
| 20 | + |
| 21 | +Let's imagine that you want to handle date denormalization differently if they |
| 22 | +are coming from a legacy system, by converting them to ``null`` if the serialized |
| 23 | +value is ``0000-00-00``. To do that you'll first have to create your normalizer:: |
| 24 | + |
| 25 | + // src/Serializer/ZeroDateTimeDenormalizer.php |
| 26 | + namespace App\Serializer; |
| 27 | + |
| 28 | + use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
| 29 | + use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
| 30 | + use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 31 | + |
| 32 | + final class ZeroDateTimeDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface |
| 33 | + { |
| 34 | + use DenormalizerAwareTrait; |
| 35 | + |
| 36 | + public function denormalize($data, string $type, string $format = null, array $context = []) |
| 37 | + { |
| 38 | + if ('0000-00-00' === $data) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + unset($context['zero_datetime_to_null']); |
| 43 | + |
| 44 | + return $this->denormalizer->denormalize($data, $type, $format, $context); |
| 45 | + } |
| 46 | + |
| 47 | + public function supportsDenormalization($data, string $type, string $format = null, array $context = []) |
| 48 | + { |
| 49 | + return true === ($context['zero_datetime_to_null'] ?? false) |
| 50 | + && is_a($type, \DateTimeInterface::class, true); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | +You'll therefore be able to cast zero-ish dates to ``null`` during denormalization:: |
| 55 | + |
| 56 | + $legacyData = '{"updatedAt": "0000-00-00"}'; |
| 57 | + |
| 58 | + $serializer->deserialize($legacyData, MyModel::class, 'json', ['zero_datetime_to_null' => true]); |
| 59 | + |
| 60 | +Then, if you don't want other developers to have to remind the precise ``zero_date_to_null`` context key, |
| 61 | +you can create a dedicated context builder:: |
| 62 | + |
| 63 | + // src/Serializer/LegacyContextBuilder |
| 64 | + namespace App\Serializer; |
| 65 | + |
| 66 | + use Symfony\Component\Serializer\Context\ContextBuilderTrait; |
| 67 | + |
| 68 | + final class LegacyContextBuilder |
| 69 | + { |
| 70 | + use ContextBuilderTrait; |
| 71 | + |
| 72 | + public function withLegacyDates(bool $legacy): static |
| 73 | + { |
| 74 | + return $this->with('zero_datetime_to_null', $legacy); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | +And finally use it to build the serialization context:: |
| 79 | + |
| 80 | + $legacyData = '{"updatedAt": "0000-00-00"}'; |
| 81 | + |
| 82 | + $context = (new LegacyContextBuilder()) |
| 83 | + ->withLegacyDates(true) |
| 84 | + ->toArray(); |
| 85 | + |
| 86 | + $serializer->deserialize($legacyData, MyModel::class, 'json', $context); |
0 commit comments