add context to serialize and deserialize

This commit is contained in:
andrey1s 2018-02-04 23:55:07 +03:00 committed by Nicolas Grekas
parent d7b98f9ef7
commit 6f8d5e263e
2 changed files with 4 additions and 3 deletions

View File

@ -10,6 +10,7 @@ CHANGELOG
* added getter for extra attributes in `ExtraAttributesException` * added getter for extra attributes in `ExtraAttributesException`
* improved `CsvEncoder` to handle variable nested structures * improved `CsvEncoder` to handle variable nested structures
* CSV headers can be passed to the `CsvEncoder` via the `csv_headers` serialization context variable * CSV headers can be passed to the `CsvEncoder` via the `csv_headers` serialization context variable
* added `$context` when checking for encoding, decoding and normalizing in `Serializer`
3.3.0 3.3.0
----- -----

View File

@ -108,11 +108,11 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
*/ */
final public function serialize($data, $format, array $context = array()) final public function serialize($data, $format, array $context = array())
{ {
if (!$this->supportsEncoding($format)) { if (!$this->supportsEncoding($format, $context)) {
throw new NotEncodableValueException(sprintf('Serialization for the format %s is not supported', $format)); throw new NotEncodableValueException(sprintf('Serialization for the format %s is not supported', $format));
} }
if ($this->encoder->needsNormalization($format)) { if ($this->encoder->needsNormalization($format, $context)) {
$data = $this->normalize($data, $format, $context); $data = $this->normalize($data, $format, $context);
} }
@ -124,7 +124,7 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
*/ */
final public function deserialize($data, $type, $format, array $context = array()) final public function deserialize($data, $type, $format, array $context = array())
{ {
if (!$this->supportsDecoding($format)) { if (!$this->supportsDecoding($format, $context)) {
throw new NotEncodableValueException(sprintf('Deserialization for the format %s is not supported', $format)); throw new NotEncodableValueException(sprintf('Deserialization for the format %s is not supported', $format));
} }