3 namespace Drupal\serialization\Normalizer;
5 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
6 use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;
9 * Base class for Normalizers.
11 abstract class NormalizerBase extends SerializerAwareNormalizer implements NormalizerInterface {
14 * The interface or class that this Normalizer supports.
18 protected $supportedInterfaceOrClass;
23 public function supportsNormalization($data, $format = NULL) {
24 // If we aren't dealing with an object or the format is not supported return
26 if (!is_object($data) || !$this->checkFormat($format)) {
30 $supported = (array) $this->supportedInterfaceOrClass;
32 return (bool) array_filter($supported, function($name) use ($data) {
33 return $data instanceof $name;
38 * Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization()
40 * This class doesn't implement DenormalizerInterface, but most of its child
41 * classes do, so this method is implemented at this level to reduce code
44 public function supportsDenormalization($data, $type, $format = NULL) {
45 // If the format is not supported return now.
46 if (!$this->checkFormat($format)) {
50 $supported = (array) $this->supportedInterfaceOrClass;
52 $subclass_check = function($name) use ($type) {
53 return (class_exists($name) || interface_exists($name)) && is_subclass_of($type, $name, TRUE);
56 return in_array($type, $supported) || array_filter($supported, $subclass_check);
60 * Checks if the provided format is supported by this normalizer.
62 * @param string $format
63 * The format to check.
66 * TRUE if the format is supported, FALSE otherwise. If no format is
67 * specified this will return TRUE.
69 protected function checkFormat($format = NULL) {
70 if (!isset($format) || !isset($this->format)) {
74 return in_array($format, (array) $this->format);