class CacheableNormalization
Same name in other branches
- 9 core/modules/jsonapi/src/Normalizer/Value/CacheableNormalization.php \Drupal\jsonapi\Normalizer\Value\CacheableNormalization
- 8.9.x core/modules/jsonapi/src/Normalizer/Value/CacheableNormalization.php \Drupal\jsonapi\Normalizer\Value\CacheableNormalization
- 11.x core/modules/jsonapi/src/Normalizer/Value/CacheableNormalization.php \Drupal\jsonapi\Normalizer\Value\CacheableNormalization
Use to store normalized data and its cacheability.
@internal JSON:API maintains no PHP API since its API is the HTTP API. This class may change at any time and this will break any dependencies on it.
Hierarchy
- class \Drupal\jsonapi\Normalizer\Value\TemporaryArrayObjectThrowingExceptions extends \Drupal\jsonapi\Normalizer\Value\ArrayObject
- class \Drupal\jsonapi\Normalizer\Value\CacheableNormalization extends \Drupal\jsonapi\Normalizer\Value\TemporaryArrayObjectThrowingExceptions implements \Drupal\Core\Cache\CacheableDependencyInterface uses \Drupal\Core\Cache\CacheableDependencyTrait
Expanded class hierarchy of CacheableNormalization
See also
https://www.drupal.org/project/drupal/issues/3032787
18 files declare their use of CacheableNormalization
- DataNormalizer.php in core/
modules/ jsonapi/ src/ Normalizer/ DataNormalizer.php - EntityReferenceFieldNormalizer.php in core/
modules/ jsonapi/ src/ Normalizer/ EntityReferenceFieldNormalizer.php - FieldItemNormalizer.php in core/
modules/ jsonapi/ src/ Normalizer/ FieldItemNormalizer.php - FieldItemNormalizerTest.php in core/
modules/ jsonapi/ tests/ src/ Kernel/ Normalizer/ FieldItemNormalizerTest.php - FieldNormalizer.php in core/
modules/ jsonapi/ src/ Normalizer/ FieldNormalizer.php
File
-
core/
modules/ jsonapi/ src/ Normalizer/ Value/ CacheableNormalization.php, line 19
Namespace
Drupal\jsonapi\Normalizer\ValueView source
class CacheableNormalization extends TemporaryArrayObjectThrowingExceptions implements CacheableDependencyInterface {
use CacheableDependencyTrait;
/**
* A normalized value.
*
* @var mixed
*/
protected $normalization;
/**
* CacheableNormalization constructor.
*
* @param \Drupal\Core\Cache\CacheableDependencyInterface $cacheability
* The cacheability metadata for the normalized data.
* @param array|string|int|float|bool|null $normalization
* The normalized data. This value must not contain any
* CacheableNormalizations.
*/
public function __construct(CacheableDependencyInterface $cacheability, $normalization) {
assert(is_array($normalization) && static::hasNoNestedInstances($normalization) || is_string($normalization) || is_int($normalization) || is_float($normalization) || is_bool($normalization) || is_null($normalization));
$this->normalization = $normalization;
$this->setCacheability($cacheability);
}
/**
* Creates a CacheableNormalization instance without any special cacheability.
*
* @param array|string|int|float|bool|null $normalization
* The normalized data. This value must not contain any
* CacheableNormalizations.
*
* @return static
* The CacheableNormalization.
*/
public static function permanent($normalization) {
return new static(new CacheableMetadata(), $normalization);
}
/**
* Gets the decorated normalization.
*
* @return array|string|int|float|bool|null
* The normalization.
*/
public function getNormalization() {
return $this->normalization;
}
/**
* Converts the object to a CacheableOmission if the normalization is empty.
*
* @return self|\Drupal\jsonapi\Normalizer\Value\CacheableOmission
* A CacheableOmission if the normalization is considered empty, self
* otherwise.
*/
public function omitIfEmpty() {
return empty($this->normalization) ? new CacheableOmission($this) : $this;
}
/**
* Gets a new CacheableNormalization with an additional dependency.
*
* @param \Drupal\Core\Cache\CacheableDependencyInterface $dependency
* The new cacheable dependency.
*
* @return static
* A new object based on the current value with an additional cacheable
* dependency.
*/
public function withCacheableDependency(CacheableDependencyInterface $dependency) {
return new static(CacheableMetadata::createFromObject($this)->addCacheableDependency($dependency), $this->normalization);
}
/**
* Collects an array of CacheableNormalizations into a single instance.
*
* @param \Drupal\jsonapi\Normalizer\Value\CacheableNormalization[] $cacheable_normalizations
* An array of CacheableNormalizations.
*
* @return static
* A new CacheableNormalization. Each input value's cacheability will be
* merged into the return value's cacheability. The return value's
* normalization will be an array of the input's normalizations. This method
* does *not* behave like array_merge() or NestedArray::mergeDeep().
*/
public static function aggregate(array $cacheable_normalizations) {
assert(Inspector::assertAllObjects($cacheable_normalizations, CacheableNormalization::class));
return new static(array_reduce($cacheable_normalizations, function (CacheableMetadata $merged, CacheableNormalization $item) {
return $merged->addCacheableDependency($item);
}, new CacheableMetadata()), array_reduce(array_keys($cacheable_normalizations), function ($merged, $key) use ($cacheable_normalizations) {
if (!$cacheable_normalizations[$key] instanceof CacheableOmission) {
$merged[$key] = $cacheable_normalizations[$key]->getNormalization();
}
return $merged;
}, []));
}
/**
* Ensures that no nested values are instances of this class.
*
* @param array|\Traversable $array
* The traversable object which may contain instance of this object.
*
* @return bool
* Whether the given object or its children have CacheableNormalizations in
* them.
*/
protected static function hasNoNestedInstances($array) {
foreach ($array as $value) {
if (is_iterable($value) && !static::hasNoNestedInstances($value) || $value instanceof static) {
return FALSE;
}
}
return TRUE;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
CacheableDependencyTrait::$cacheContexts | protected | property | Cache contexts. | |
CacheableDependencyTrait::$cacheMaxAge | protected | property | Cache max-age. | |
CacheableDependencyTrait::$cacheTags | protected | property | Cache tags. | |
CacheableDependencyTrait::getCacheContexts | public | function | 4 | |
CacheableDependencyTrait::getCacheMaxAge | public | function | 4 | |
CacheableDependencyTrait::getCacheTags | public | function | 4 | |
CacheableDependencyTrait::setCacheability | protected | function | Sets cacheability; useful for value object constructors. | |
CacheableNormalization::$normalization | protected | property | A normalized value. | |
CacheableNormalization::aggregate | public static | function | Collects an array of CacheableNormalizations into a single instance. | |
CacheableNormalization::getNormalization | public | function | Gets the decorated normalization. | 1 |
CacheableNormalization::hasNoNestedInstances | protected static | function | Ensures that no nested values are instances of this class. | |
CacheableNormalization::omitIfEmpty | public | function | Converts the object to a CacheableOmission if the normalization is empty. | |
CacheableNormalization::permanent | public static | function | Creates a CacheableNormalization instance without any special cacheability. | 1 |
CacheableNormalization::withCacheableDependency | public | function | Gets a new CacheableNormalization with an additional dependency. | |
CacheableNormalization::__construct | public | function | CacheableNormalization constructor. | 1 |
TemporaryArrayObjectThrowingExceptions::append | public | function | Append a value to the ArrayObject. | |
TemporaryArrayObjectThrowingExceptions::asort | public | function | Sort the ArrayObject. | |
TemporaryArrayObjectThrowingExceptions::count | public | function | Count the ArrayObject. | |
TemporaryArrayObjectThrowingExceptions::exchangeArray | public | function | Exchange the current array with another array or object. | |
TemporaryArrayObjectThrowingExceptions::getArrayCopy | public | function | Exports the \ArrayObject to an array. | |
TemporaryArrayObjectThrowingExceptions::getFlags | public | function | Gets the behavior flags of the \ArrayObject. | |
TemporaryArrayObjectThrowingExceptions::getIterator | public | function | Create a new iterator from an ArrayObject instance. | |
TemporaryArrayObjectThrowingExceptions::getIteratorClass | public | function | Gets the class name of the array iterator that is used by \ArrayObject::getIterator(). | |
TemporaryArrayObjectThrowingExceptions::ksort | public | function | Sort the entries by key. | |
TemporaryArrayObjectThrowingExceptions::natcasesort | public | function | Sort an array using a case insensitive "natural order" algorithm. | |
TemporaryArrayObjectThrowingExceptions::natsort | public | function | Sort entries using a "natural order" algorithm. | |
TemporaryArrayObjectThrowingExceptions::offsetExists | public | function | Returns whether the requested index exists. | |
TemporaryArrayObjectThrowingExceptions::offsetGet | public | function | Returns the value at the specified index. | |
TemporaryArrayObjectThrowingExceptions::offsetSet | public | function | Sets the value at the specified index to new value. | |
TemporaryArrayObjectThrowingExceptions::offsetUnset | public | function | Unsets the value at the specified index. | |
TemporaryArrayObjectThrowingExceptions::setFlags | public | function | Sets the behavior flags for the \ArrayObject. | |
TemporaryArrayObjectThrowingExceptions::setIteratorClass | public | function | Sets the iterator classname for the \ArrayObject. | |
TemporaryArrayObjectThrowingExceptions::uasort | public | function | Sort the entries with a user-defined comparison function. | |
TemporaryArrayObjectThrowingExceptions::uksort | public | function | Sort the entries by keys using a user-defined comparison function. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.