Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / serializer / Normalizer / CustomNormalizer.php
index 688590ef02a101e8dc06d88cbb181a3ab2d86071..6a15d8da9027063bcd36d05331a34f61c5649f3d 100644 (file)
@@ -19,8 +19,11 @@ use Symfony\Component\Serializer\SerializerAwareTrait;
  */
 class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
 {
+    use ObjectToPopulateTrait;
     use SerializerAwareTrait;
 
+    private $cache = array();
+
     /**
      * {@inheritdoc}
      */
@@ -34,7 +37,7 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se
      */
     public function denormalize($data, $class, $format = null, array $context = array())
     {
-        $object = new $class();
+        $object = $this->extractObjectToPopulate($class, $context) ?: new $class();
         $object->denormalize($this->serializer, $data, $format, $context);
 
         return $object;
@@ -54,7 +57,7 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se
     }
 
     /**
-     * Checks if the given class implements the NormalizableInterface.
+     * Checks if the given class implements the DenormalizableInterface.
      *
      * @param mixed  $data   Data to denormalize from
      * @param string $type   The class to which the data should be denormalized
@@ -64,10 +67,14 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se
      */
     public function supportsDenormalization($data, $type, $format = null)
     {
+        if (isset($this->cache[$type])) {
+            return $this->cache[$type];
+        }
+
         if (!class_exists($type)) {
-            return false;
+            return $this->cache[$type] = false;
         }
 
-        return is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
+        return $this->cache[$type] = is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
     }
 }