You are here

public static function ResourceIdentifier::toResourceIdentifiers in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/JsonApiResource/ResourceIdentifier.php \Drupal\jsonapi\JsonApiResource\ResourceIdentifier::toResourceIdentifiers()

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.

3 calls to ResourceIdentifier::toResourceIdentifiers()
EntityReferenceFieldNormalizer::normalize in core/modules/jsonapi/src/Normalizer/EntityReferenceFieldNormalizer.php
Normalizes an object into a set of arrays/scalars.
Relationship::createFromEntityReferenceField in core/modules/jsonapi/src/JsonApiResource/Relationship.php
Creates a new Relationship from an entity reference field.
ResourceIdentifier::toResourceIdentifiersWithArityRequired in core/modules/jsonapi/src/JsonApiResource/ResourceIdentifier.php
Creates an array of ResourceIdentifier objects with arity on every value.

File

core/modules/jsonapi/src/JsonApiResource/ResourceIdentifier.php, line 312

Class

ResourceIdentifier
Represents a JSON:API resource identifier object.

Namespace

Drupal\jsonapi\JsonApiResource

Code

public static function toResourceIdentifiers(EntityReferenceFieldItemListInterface $items) {
  $relationships = [];
  foreach ($items
    ->filterEmptyItems() 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);
    if ($relationship
      ->getResourceType()
      ->isInternal()) {
      continue;
    }

    // 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;
}