Version 1
[yaffs-website] / web / core / modules / hal / src / Normalizer / NormalizerBase.php
1 <?php
2
3 namespace Drupal\hal\Normalizer;
4
5 use Drupal\serialization\Normalizer\NormalizerBase as SerializationNormalizerBase;
6 use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
7
8 /**
9  * Base class for Normalizers.
10  */
11 abstract class NormalizerBase extends SerializationNormalizerBase implements DenormalizerInterface {
12
13   /**
14    * The formats that the Normalizer can handle.
15    *
16    * @var array
17    */
18   protected $formats = ['hal_json'];
19
20   /**
21    * {@inheritdoc}
22    */
23   public function supportsNormalization($data, $format = NULL) {
24     return in_array($format, $this->formats) && parent::supportsNormalization($data, $format);
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public function supportsDenormalization($data, $type, $format = NULL) {
31     if (in_array($format, $this->formats) && (class_exists($this->supportedInterfaceOrClass) || interface_exists($this->supportedInterfaceOrClass))) {
32       $target = new \ReflectionClass($type);
33       $supported = new \ReflectionClass($this->supportedInterfaceOrClass);
34       if ($supported->isInterface()) {
35         return $target->implementsInterface($this->supportedInterfaceOrClass);
36       }
37       else {
38         return ($target->getName() == $this->supportedInterfaceOrClass || $target->isSubclassOf($this->supportedInterfaceOrClass));
39       }
40     }
41
42     return FALSE;
43   }
44
45 }