Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / serialization / src / Normalizer / NormalizerBase.php
1 <?php
2
3 namespace Drupal\serialization\Normalizer;
4
5 use Drupal\Core\Cache\CacheableDependencyInterface;
6 use Symfony\Component\Serializer\SerializerAwareInterface;
7 use Symfony\Component\Serializer\SerializerAwareTrait;
8
9 /**
10  * Base class for Normalizers.
11  */
12 abstract class NormalizerBase implements SerializerAwareInterface, CacheableNormalizerInterface {
13
14   use SerializerAwareTrait;
15
16   /**
17    * The interface or class that this Normalizer supports.
18    *
19    * @var string|array
20    */
21   protected $supportedInterfaceOrClass;
22
23   /**
24    * List of formats which supports (de-)normalization.
25    *
26    * @var string|string[]
27    */
28   protected $format;
29
30   /**
31    * {@inheritdoc}
32    */
33   public function supportsNormalization($data, $format = NULL) {
34     // If we aren't dealing with an object or the format is not supported return
35     // now.
36     if (!is_object($data) || !$this->checkFormat($format)) {
37       return FALSE;
38     }
39
40     $supported = (array) $this->supportedInterfaceOrClass;
41
42     return (bool) array_filter($supported, function ($name) use ($data) {
43       return $data instanceof $name;
44     });
45   }
46
47   /**
48    * Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization()
49    *
50    * This class doesn't implement DenormalizerInterface, but most of its child
51    * classes do, so this method is implemented at this level to reduce code
52    * duplication.
53    */
54   public function supportsDenormalization($data, $type, $format = NULL) {
55     // If the format is not supported return now.
56     if (!$this->checkFormat($format)) {
57       return FALSE;
58     }
59
60     $supported = (array) $this->supportedInterfaceOrClass;
61
62     $subclass_check = function ($name) use ($type) {
63       return (class_exists($name) || interface_exists($name)) && is_subclass_of($type, $name, TRUE);
64     };
65
66     return in_array($type, $supported) || array_filter($supported, $subclass_check);
67   }
68
69   /**
70    * Checks if the provided format is supported by this normalizer.
71    *
72    * @param string $format
73    *   The format to check.
74    *
75    * @return bool
76    *   TRUE if the format is supported, FALSE otherwise. If no format is
77    *   specified this will return TRUE.
78    */
79   protected function checkFormat($format = NULL) {
80     if (!isset($format) || !isset($this->format)) {
81       return TRUE;
82     }
83
84     return in_array($format, (array) $this->format, TRUE);
85   }
86
87   /**
88    * Adds cacheability if applicable.
89    *
90    * @param array $context
91    *   Context options for the normalizer.
92    * @param $data
93    *   The data that might have cacheability information.
94    */
95   protected function addCacheableDependency(array $context, $data) {
96     if ($data instanceof CacheableDependencyInterface && isset($context[static::SERIALIZATION_CONTEXT_CACHEABILITY])) {
97       $context[static::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($data);
98     }
99   }
100
101 }