class FieldItemNormalizerValue in JSON:API 8
Helps normalize field items in compliance with the JSON API spec.
@internal
Hierarchy
- class \Drupal\jsonapi\Normalizer\Value\FieldItemNormalizerValue implements CacheableDependencyInterface uses CacheableDependencyTrait
Expanded class hierarchy of FieldItemNormalizerValue
6 files declare their use of FieldItemNormalizerValue
- FieldItemNormalizer.php in src/
Normalizer/ FieldItemNormalizer.php - FieldItemNormalizerValueTest.php in tests/
src/ Unit/ Normalizer/ Value/ FieldItemNormalizerValueTest.php - FieldNormalizer.php in src/
Normalizer/ FieldNormalizer.php - FieldNormalizerValueTest.php in tests/
src/ Unit/ Normalizer/ Value/ FieldNormalizerValueTest.php - HttpExceptionNormalizer.php in src/
Normalizer/ HttpExceptionNormalizer.php
File
- src/
Normalizer/ Value/ FieldItemNormalizerValue.php, line 13
Namespace
Drupal\jsonapi\Normalizer\ValueView source
class FieldItemNormalizerValue implements CacheableDependencyInterface {
use CacheableDependencyTrait;
/**
* Raw values.
*
* @var array
*/
protected $raw;
/**
* Instantiate a FieldItemNormalizerValue object.
*
* @param array $values
* The normalized result.
* @param \Drupal\Core\Cache\CacheableDependencyInterface $values_cacheability
* The cacheability of the normalized result. This cacheability is not part
* of $values because field items are normalized by Drupal core's
* serialization system, which was never designed with cacheability in mind.
* FieldItemNormalizer::normalize() must catch the out-of-band bubbled
* cacheability and then passes it to this value object.
*
* @see \Drupal\jsonapi\Normalizer\FieldItemNormalizer::normalize()
*/
public function __construct(array $values, CacheableDependencyInterface $values_cacheability) {
$this->raw = $values;
$this
->setCacheability($values_cacheability);
}
/**
* {@inheritdoc}
*/
public function rasterizeValue() {
// If there is only one property, then output it directly.
$value = count($this->raw) == 1 ? reset($this->raw) : $this->raw;
return $this
->rasterizeValueRecursive($value);
}
/**
* Rasterizes a value recursively.
*
* This is mainly for configuration entities where a field can be a tree of
* values to rasterize.
*
* @param mixed $value
* Either a scalar, an array or a rasterizable object.
*
* @return mixed
* The rasterized value.
*/
protected function rasterizeValueRecursive($value) {
if (!$value || is_scalar($value)) {
return $value;
}
if (is_array($value)) {
$output = [];
foreach ($value as $key => $item) {
$output[$key] = $this
->rasterizeValueRecursive($item);
}
return $output;
}
if ($value instanceof ValueExtractorInterface) {
return $value
->rasterizeValue();
}
// If the object can be turned into a string it's better than nothing.
if (method_exists($value, '__toString')) {
return $value
->__toString();
}
// We give up, since we do not know how to rasterize this.
return NULL;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableDependencyTrait:: |
protected | property | Cache contexts. | |
CacheableDependencyTrait:: |
protected | property | Cache max-age. | |
CacheableDependencyTrait:: |
protected | property | Cache tags. | |
CacheableDependencyTrait:: |
public | function | ||
CacheableDependencyTrait:: |
public | function | ||
CacheableDependencyTrait:: |
public | function | ||
CacheableDependencyTrait:: |
protected | function | Sets cacheability; useful for value object constructors. | |
FieldItemNormalizerValue:: |
protected | property | Raw values. | 1 |
FieldItemNormalizerValue:: |
public | function | 2 | |
FieldItemNormalizerValue:: |
protected | function | Rasterizes a value recursively. | |
FieldItemNormalizerValue:: |
public | function | Instantiate a FieldItemNormalizerValue object. | 2 |