namespace Drupal\serialization\Normalizer;
-use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
-use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;
+use Drupal\Core\Cache\CacheableDependencyInterface;
+use Symfony\Component\Serializer\SerializerAwareInterface;
+use Symfony\Component\Serializer\SerializerAwareTrait;
/**
* Base class for Normalizers.
*/
-abstract class NormalizerBase extends SerializerAwareNormalizer implements NormalizerInterface {
+abstract class NormalizerBase implements SerializerAwareInterface, CacheableNormalizerInterface {
+
+ use SerializerAwareTrait;
/**
* The interface or class that this Normalizer supports.
*/
protected $supportedInterfaceOrClass;
+ /**
+ * List of formats which supports (de-)normalization.
+ *
+ * @var string|string[]
+ */
+ protected $format;
+
/**
* {@inheritdoc}
*/
$supported = (array) $this->supportedInterfaceOrClass;
- return (bool) array_filter($supported, function($name) use ($data) {
+ return (bool) array_filter($supported, function ($name) use ($data) {
return $data instanceof $name;
});
}
$supported = (array) $this->supportedInterfaceOrClass;
- $subclass_check = function($name) use ($type) {
+ $subclass_check = function ($name) use ($type) {
return (class_exists($name) || interface_exists($name)) && is_subclass_of($type, $name, TRUE);
};
return TRUE;
}
- return in_array($format, (array) $this->format);
+ return in_array($format, (array) $this->format, TRUE);
+ }
+
+ /**
+ * Adds cacheability if applicable.
+ *
+ * @param array $context
+ * Context options for the normalizer.
+ * @param $data
+ * The data that might have cacheability information.
+ */
+ protected function addCacheableDependency(array $context, $data) {
+ if ($data instanceof CacheableDependencyInterface && isset($context[static::SERIALIZATION_CONTEXT_CACHEABILITY])) {
+ $context[static::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($data);
+ }
}
}