ResourceIdentifier example


  public function __construct(CacheableDependencyInterface $cacheability, ResourceType $resource_type$id$revision_id, array $fields, LinkCollection $links, LanguageInterface $language = NULL) {
    assert(is_null($revision_id) || $resource_type->isVersionable());
    $this->setCacheability($cacheability);
    $this->resourceType = $resource_type;
    $this->resourceIdentifier = new ResourceIdentifier($resource_type$id);
    $this->versionIdentifier = $revision_id ? 'id:' . $revision_id : NULL;
    $this->fields = $fields;
    $this->links = $links->withContext($this);

    // If the specified language empty it falls back the same way as in the entity system     // @see \Drupal\Core\Entity\EntityBase::language()     $this->language = $language ?: new Language(['id' => LanguageInterface::LANGCODE_NOT_SPECIFIED]);
  }

  /** * Creates a new ResourceObject from an entity. * * @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type * The JSON:API resource type of the resource object. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity to be represented by this resource object. * @param \Drupal\jsonapi\JsonApiResource\LinkCollection $links * (optional) Any links for the resource object, if a `self` link is not * provided, one will be automatically added if the resource is locatable * and is not an internal entity. * * @return static * An instantiated resource object. */
$data = $this->massageRelationshipInput($data$is_multiple);
    $resource_identifiers = array_map(function D$value) use ($target_resource_type_names) {
      // Make sure that the provided type is compatible with the targeted       // resource.       if (!in_array($value['type']$target_resource_type_names)) {
        throw new BadRequestHttpException(sprintf(
          'The provided type (%s) does not match the destination resource types (%s).',
          $value['type'],
          implode(', ', $target_resource_type_names)
        ));
      }
      return new ResourceIdentifier($value['type']$value['id']$value['meta'] ?? []);
    }$data['data']);
    if (!ResourceIdentifier::areResourceIdentifiersUnique($resource_identifiers)) {
      throw new BadRequestHttpException('Duplicate relationships are not permitted. Use `meta.arity` to distinguish resource identifiers with matching `type` and `id` values.');
    }
    return $resource_identifiers;
  }

  /** * Validates and massages the relationship input depending on the cardinality. * * @param array $data * The input data from the body. * @param bool $is_multiple * Indicates if the relationship is to-many. * * @return array * The massaged data array. */
/** * Data provider for the denormalize test. * * @return array * The data for the test method. */
  public function denormalizeProvider() {
    return [
      [
        ['data' => [['type' => 'lorem--dummy_bundle', 'id' => '4e6cb61d-4f04-437f-99fe-42c002393658']]],
        'field_dummy',
        [new ResourceIdentifier('lorem--dummy_bundle', '4e6cb61d-4f04-437f-99fe-42c002393658')],
      ],
      [
        ['data' => []],
        'field_dummy',
        [],
      ],
      [
        ['data' => NULL],
        'field_dummy_single',
        [],
      ],
    ];
Home | Imprint | This part of the site doesn't use cookies.