FieldNormalizerValue.php in JSON:API 8
File
src/Normalizer/Value/FieldNormalizerValue.php
View source
<?php
namespace Drupal\jsonapi\Normalizer\Value;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\jsonapi\Normalizer\CacheableDependencyTrait;
class FieldNormalizerValue implements FieldNormalizerValueInterface {
use CacheableDependencyTrait;
use CacheableDependenciesMergerTrait;
protected $values;
protected $includes;
protected $cardinality;
protected $propertyType;
public function __construct(AccessResultInterface $field_access_result, array $values, $cardinality, $property_type) {
assert($property_type === 'attributes' || $property_type === 'relationships');
$this
->setCacheability(static::mergeCacheableDependencies(array_merge([
$field_access_result,
], $values)));
$this->values = $values;
$this->includes = array_map(function ($value) {
if (!$value instanceof RelationshipItemNormalizerValue) {
return NULL;
}
return $value
->getInclude();
}, $values);
$this->includes = array_filter($this->includes);
$this->cardinality = $cardinality;
$this->propertyType = $property_type;
}
public function rasterizeValue() {
if (empty($this->values)) {
return NULL;
}
if ($this->cardinality == 1) {
assert(count($this->values) === 1);
return $this->values[0] instanceof FieldItemNormalizerValue ? $this->values[0]
->rasterizeValue() : NULL;
}
return array_map(function ($value) {
return $value instanceof FieldItemNormalizerValue ? $value
->rasterizeValue() : NULL;
}, $this->values);
}
public function rasterizeIncludes() {
return array_map(function ($include) {
return $include
->rasterizeValue();
}, $this->includes);
}
public function getIncludes() {
return $this->includes;
}
public function getPropertyType() {
return $this->propertyType;
}
public function getAllIncludes() {
$nested_includes = array_map(function ($include) {
return $include
->getIncludes();
}, $this
->getIncludes());
$includes = array_reduce(array_filter($nested_includes), function ($carry, $item) {
return array_merge($carry, $item);
}, $this
->getIncludes());
return array_values(array_reduce($includes, function ($unique_includes, $include) {
$rasterized_include = $include
->rasterizeValue();
$unique_includes[$rasterized_include['data']['type'] . ':' . $rasterized_include['data']['id']] = $include;
return $unique_includes;
}, []));
}
}