diff --git a/src/Normalizer/DeterministicNormalizerInterface.php b/src/Normalizer/DeterministicNormalizerInterface.php index f40ca7e..a1a816c 100644 --- a/src/Normalizer/DeterministicNormalizerInterface.php +++ b/src/Normalizer/DeterministicNormalizerInterface.php @@ -11,4 +11,20 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface; * for *any* instance of a particular interface or class if it is also valid for * the given format, regardless of the state of the $data or $context arguments. */ -interface DeterministicNormalizerInterface extends NormalizerInterface {} +interface DeterministicNormalizerInterface extends NormalizerInterface { + + /** + * The interface or class that this Normalizer supports. + * + * @param string|object $type + * An interface or class name or an object. + * @param string $format + * Format the normalization result will be encoded as. + * + * @return bool + * Whether this normalizer supports *any* instance of the given interface or + * class or of the given object's class. + */ + public function supportsNormalizationForInterfaceOrClass($type, $format); + +} diff --git a/src/Normalizer/NormalizerBase.php b/src/Normalizer/NormalizerBase.php index b5a642c..7caae87 100644 --- a/src/Normalizer/NormalizerBase.php +++ b/src/Normalizer/NormalizerBase.php @@ -16,6 +16,16 @@ abstract class NormalizerBase extends SerializationNormalizerBase implements Det */ protected $format = 'api_json'; + /** + * {@inheritdoc} + */ + public function supportsNormalizationForInterfaceOrClass($type, $format) { + $supported = (array) $this->supportedInterfaceOrClass; + return $this->checkFormat($format) && (bool) array_filter($supported, function ($name) use ($type) { + return is_subclass_of($type, $name); + }); + } + /** * {@inheritdoc} */ diff --git a/src/Serializer/Serializer.php b/src/Serializer/Serializer.php index 5437522..59a0688 100644 --- a/src/Serializer/Serializer.php +++ b/src/Serializer/Serializer.php @@ -96,6 +96,7 @@ final class Serializer extends SymfonySerializer { } $normalizer = $this->copiedGetNormalizer($data, $format, $context); if ($type && $normalizer instanceof DeterministicNormalizerInterface) { + assert($normalizer->supportsNormalizationForInterfaceOrClass($type, $format)); $this->deterministicNormalizers[$type][$format] = $normalizer; } return $normalizer;