public static function ResourceIdentifier::toResourceIdentifiers in JSON:API 8.2
Creates an array of ResourceIdentifier objects.
Parameters
\Drupal\Core\Field\EntityReferenceFieldItemListInterface $items: The entity reference field items from which to create the relationship array.
Return value
self[] An array of new ResourceIdentifier objects with appropriate arity values.
2 calls to ResourceIdentifier::toResourceIdentifiers()
- EntityReferenceFieldNormalizer::normalize in src/
Normalizer/ EntityReferenceFieldNormalizer.php - Normalizes an object into a set of arrays/scalars.
- ResourceIdentifier::toResourceIdentifiersWithArityRequired in src/
JsonApiResource/ ResourceIdentifier.php - Creates an array of ResourceIdentifier objects with arity on every value.
File
- src/
JsonApiResource/ ResourceIdentifier.php, line 312
Class
- ResourceIdentifier
- Represents a JSON:API resource identifier object.
Namespace
Drupal\jsonapi\JsonApiResourceCode
public static function toResourceIdentifiers(EntityReferenceFieldItemListInterface $items) {
$relationships = [];
foreach ($items as $item) {
// Create a ResourceIdentifier from the field item. This will make it
// comparable with all previous field items. Here, it is assumed that the
// resource identifier is unique so it has no arity. If a parallel
// relationship is encountered, it will be assigned later.
$relationship = static::toResourceIdentifier($item);
// Now, iterate over the previously seen resource identifiers in reverse
// order. Reverse order is important so that when a parallel relationship
// is encountered, it will have the highest arity value so the current
// relationship's arity value can simply be incremented by one.
/* @var self $existing */
foreach (array_reverse($relationships, TRUE) as $index => $existing) {
$is_parallel = static::isParallel($existing, $relationship);
if ($is_parallel) {
// A parallel relationship has been found. If the previous
// relationship does not have an arity, it must now be assigned an
// arity of 0.
if (!$existing
->hasArity()) {
$relationships[$index] = $existing
->withArity(0);
}
// Since the new ResourceIdentifier is parallel, it must have an arity
// assigned to it that is the arity of the last parallel
// relationship's arity + 1.
$relationship = $relationship
->withArity($relationships[$index]
->getArity() + 1);
break;
}
}
// Finally, append the relationship to the list of ResourceIdentifiers.
$relationships[] = $relationship;
}
return $relationships;
}