deduplicate example


  public function resolve($data$include_parameter) {
    assert($data instanceof ResourceObject || $data instanceof ResourceObjectData);
    $data = $data instanceof ResourceObjectData ? $data : new ResourceObjectData([$data], 1);
    $include_tree = static::toIncludeTree($data$include_parameter);
    return IncludedData::deduplicate($this->resolveIncludeTree($include_tree$data));
  }

  /** * Receives a tree of include field names and resolves resources for it. * * This method takes a tree of relationship field names and JSON:API Data * object. For the top-level of the tree and for each entity in the * collection, it gets the target entity type and IDs for each relationship * field. The method then loads all of those targets and calls itself * recursively with the next level of the tree and those loaded resources. * * @param array $include_tree * The include paths, represented as a tree. * @param \Drupal\jsonapi\JsonApiResource\Data $data * The entity collection from which includes should be resolved. * @param \Drupal\jsonapi\JsonApiResource\Data|null $includes * (Internal use only) Any prior resolved includes. * * @return \Drupal\jsonapi\JsonApiResource\Data * A JSON:API Data of included items. * * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException * Thrown if an included entity type doesn't exist. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * Thrown if a storage handler couldn't be loaded. */
    /** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $field_list */
    $field_list = $entity->{$internal_relationship_field_name};
    /** @var \Drupal\field\Entity\FieldConfig $field_definition */
    $field_definition = $field_list->getFieldDefinition();
    $is_multiple = $field_definition->getFieldStorageDefinition()->isMultiple();
    if (!$is_multiple) {
      throw new ConflictHttpException(sprintf('You can only POST to to-many relationships. %s is a to-one relationship.', $related));
    }

    $original_resource_identifiers = ResourceIdentifier::toResourceIdentifiersWithArityRequired($field_list);
    $new_resource_identifiers = array_udiff(
      ResourceIdentifier::deduplicate(array_merge($original_resource_identifiers$resource_identifiers)),
      $original_resource_identifiers,
      [ResourceIdentifier::class, 'compare']
    );

    // There are no relationships that need to be added so we can exit early.     if (empty($new_resource_identifiers)) {
      $status = static::relationshipResponseRequiresBody($resource_identifiers$original_resource_identifiers) ? 200 : 204;
      return $this->getRelationship($resource_type$entity$related$request$status);
    }

    $main_property_name = $field_definition->getItemDefinition()->getMainPropertyName();
    
/** * Determines if an array of ResourceIdentifier objects is duplicate free. * * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifier[] $resource_identifiers * The list of ResourceIdentifiers to assess. * * @return bool * Whether all the given resource identifiers are unique. */
  public static function areResourceIdentifiersUnique(array $resource_identifiers) {
    return count($resource_identifiers) === count(static::deduplicate($resource_identifiers));
  }

  /** * Creates a ResourceIdentifier object. * * @param \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item * The entity reference field item from which to create the relationship. * @param int $arity * (optional) The arity of the relationship. * * @return self * A new ResourceIdentifier object. */
Home | Imprint | This part of the site doesn't use cookies.