annotate core/modules/jsonapi/src/JsonApiResource/ResourceIdentifier.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
rev   line source
Chris@5 1 <?php
Chris@5 2
Chris@5 3 namespace Drupal\jsonapi\JsonApiResource;
Chris@5 4
Chris@5 5 use Drupal\Core\Entity\EntityInterface;
Chris@5 6 use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
Chris@5 7 use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
Chris@5 8 use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
Chris@5 9 use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper;
Chris@5 10 use Drupal\jsonapi\ResourceType\ResourceType;
Chris@5 11
Chris@5 12 /**
Chris@5 13 * Represents a JSON:API resource identifier object.
Chris@5 14 *
Chris@5 15 * The official JSON:API JSON-Schema document requires that no two resource
Chris@5 16 * identifier objects are duplicates, however Drupal allows multiple entity
Chris@5 17 * reference items to the same entity. Here, these are termed "parallel"
Chris@5 18 * relationships (as in "parallel edges" of a graph).
Chris@5 19 *
Chris@5 20 * This class adds a concept of an @code arity @endcode member under each its
Chris@5 21 * @code meta @endcode object. The value of this member is an integer that is
Chris@5 22 * incremented by 1 (starting from 0) for each repeated resource identifier
Chris@5 23 * sharing a common @code type @endcode and @code id @endcode.
Chris@5 24 *
Chris@5 25 * There are a number of helper methods to process the logic of dealing with
Chris@5 26 * resource identifies with and without arity.
Chris@5 27 *
Chris@5 28 * @internal JSON:API maintains no PHP API. The API is the HTTP API. This class
Chris@5 29 * may change at any time and could break any dependencies on it.
Chris@5 30 *
Chris@5 31 * @see https://www.drupal.org/project/jsonapi/issues/3032787
Chris@5 32 * @see jsonapi.api.php
Chris@5 33 *
Chris@5 34 * @see http://jsonapi.org/format/#document-resource-object-relationships
Chris@5 35 * @see https://github.com/json-api/json-api/pull/1156#issuecomment-325377995
Chris@5 36 * @see https://www.drupal.org/project/jsonapi/issues/2864680
Chris@5 37 */
Chris@5 38 class ResourceIdentifier implements ResourceIdentifierInterface {
Chris@5 39
Chris@5 40 const ARITY_KEY = 'arity';
Chris@5 41
Chris@5 42 /**
Chris@5 43 * The JSON:API resource type name.
Chris@5 44 *
Chris@5 45 * @var string
Chris@5 46 */
Chris@5 47 protected $resourceTypeName;
Chris@5 48
Chris@5 49 /**
Chris@5 50 * The JSON:API resource type.
Chris@5 51 *
Chris@5 52 * @var \Drupal\jsonapi\ResourceType\ResourceType
Chris@5 53 */
Chris@5 54 protected $resourceType;
Chris@5 55
Chris@5 56 /**
Chris@5 57 * The resource ID.
Chris@5 58 *
Chris@5 59 * @var string
Chris@5 60 */
Chris@5 61 protected $id;
Chris@5 62
Chris@5 63 /**
Chris@5 64 * The relationship's metadata.
Chris@5 65 *
Chris@5 66 * @var array
Chris@5 67 */
Chris@5 68 protected $meta;
Chris@5 69
Chris@5 70 /**
Chris@5 71 * ResourceIdentifier constructor.
Chris@5 72 *
Chris@5 73 * @param \Drupal\jsonapi\ResourceType\ResourceType|string $resource_type
Chris@5 74 * The JSON:API resource type or a JSON:API resource type name.
Chris@5 75 * @param string $id
Chris@5 76 * The resource ID.
Chris@5 77 * @param array $meta
Chris@5 78 * Any metadata for the ResourceIdentifier.
Chris@5 79 */
Chris@5 80 public function __construct($resource_type, $id, array $meta = []) {
Chris@5 81 assert(is_string($resource_type) || $resource_type instanceof ResourceType);
Chris@5 82 assert(!isset($meta[static::ARITY_KEY]) || is_int($meta[static::ARITY_KEY]) && $meta[static::ARITY_KEY] >= 0);
Chris@5 83 $this->resourceTypeName = is_string($resource_type) ? $resource_type : $resource_type->getTypeName();
Chris@5 84 $this->id = $id;
Chris@5 85 $this->meta = $meta;
Chris@5 86 if (!is_string($resource_type)) {
Chris@5 87 $this->resourceType = $resource_type;
Chris@5 88 }
Chris@5 89 }
Chris@5 90
Chris@5 91 /**
Chris@5 92 * Gets the ResourceIdentifier's JSON:API resource type name.
Chris@5 93 *
Chris@5 94 * @return string
Chris@5 95 * The JSON:API resource type name.
Chris@5 96 */
Chris@5 97 public function getTypeName() {
Chris@5 98 return $this->resourceTypeName;
Chris@5 99 }
Chris@5 100
Chris@5 101 /**
Chris@5 102 * {@inheritdoc}
Chris@5 103 */
Chris@5 104 public function getResourceType() {
Chris@5 105 if (!isset($this->resourceType)) {
Chris@5 106 /* @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository */
Chris@5 107 $resource_type_repository = \Drupal::service('jsonapi.resource_type.repository');
Chris@5 108 $this->resourceType = $resource_type_repository->getByTypeName($this->getTypeName());
Chris@5 109 }
Chris@5 110 return $this->resourceType;
Chris@5 111 }
Chris@5 112
Chris@5 113 /**
Chris@5 114 * Gets the ResourceIdentifier's ID.
Chris@5 115 *
Chris@5 116 * @return string
Chris@5 117 * The ID.
Chris@5 118 */
Chris@5 119 public function getId() {
Chris@5 120 return $this->id;
Chris@5 121 }
Chris@5 122
Chris@5 123 /**
Chris@5 124 * Whether this ResourceIdentifier has an arity.
Chris@5 125 *
Chris@5 126 * @return int
Chris@5 127 * TRUE if the ResourceIdentifier has an arity, FALSE otherwise.
Chris@5 128 */
Chris@5 129 public function hasArity() {
Chris@5 130 return isset($this->meta[static::ARITY_KEY]);
Chris@5 131 }
Chris@5 132
Chris@5 133 /**
Chris@5 134 * Gets the ResourceIdentifier's arity.
Chris@5 135 *
Chris@5 136 * One must check self::hasArity() before calling this method.
Chris@5 137 *
Chris@5 138 * @return int
Chris@5 139 * The arity.
Chris@5 140 */
Chris@5 141 public function getArity() {
Chris@5 142 assert($this->hasArity());
Chris@5 143 return $this->meta[static::ARITY_KEY];
Chris@5 144 }
Chris@5 145
Chris@5 146 /**
Chris@5 147 * Returns a copy of the given ResourceIdentifier with the given arity.
Chris@5 148 *
Chris@5 149 * @param int $arity
Chris@5 150 * The new arity; must be a non-negative integer.
Chris@5 151 *
Chris@5 152 * @return static
Chris@5 153 * A newly created ResourceIdentifier with the given arity, otherwise
Chris@5 154 * the same.
Chris@5 155 */
Chris@5 156 public function withArity($arity) {
Chris@5 157 return new static($this->getResourceType(), $this->getId(), [static::ARITY_KEY => $arity] + $this->getMeta());
Chris@5 158 }
Chris@5 159
Chris@5 160 /**
Chris@5 161 * Gets the resource identifier objects metadata.
Chris@5 162 *
Chris@5 163 * @return array
Chris@5 164 * The metadata.
Chris@5 165 */
Chris@5 166 public function getMeta() {
Chris@5 167 return $this->meta;
Chris@5 168 }
Chris@5 169
Chris@5 170 /**
Chris@5 171 * Determines if two ResourceIdentifiers are the same.
Chris@5 172 *
Chris@5 173 * This method does not consider parallel relationships with different arity
Chris@5 174 * values to be duplicates. For that, use the isParallel() method.
Chris@5 175 *
Chris@5 176 * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifier $a
Chris@5 177 * The first ResourceIdentifier object.
Chris@5 178 * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifier $b
Chris@5 179 * The second ResourceIdentifier object.
Chris@5 180 *
Chris@5 181 * @return bool
Chris@5 182 * TRUE if both relationships reference the same resource and do not have
Chris@5 183 * two distinct arity's, FALSE otherwise.
Chris@5 184 *
Chris@5 185 * For example, if $a and $b both reference the same resource identifier,
Chris@5 186 * they can only be distinct if they *both* have an arity and those values
Chris@5 187 * are not the same. If $a or $b does not have an arity, they will be
Chris@5 188 * considered duplicates.
Chris@5 189 */
Chris@5 190 public static function isDuplicate(ResourceIdentifier $a, ResourceIdentifier $b) {
Chris@5 191 return static::compare($a, $b) === 0;
Chris@5 192 }
Chris@5 193
Chris@5 194 /**
Chris@5 195 * Determines if two ResourceIdentifiers identify the same resource object.
Chris@5 196 *
Chris@5 197 * This method does not consider arity.
Chris@5 198 *
Chris@5 199 * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifier $a
Chris@5 200 * The first ResourceIdentifier object.
Chris@5 201 * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifier $b
Chris@5 202 * The second ResourceIdentifier object.
Chris@5 203 *
Chris@5 204 * @return bool
Chris@5 205 * TRUE if both relationships reference the same resource, even when they
Chris@5 206 * have differing arity values, FALSE otherwise.
Chris@5 207 */
Chris@5 208 public static function isParallel(ResourceIdentifier $a, ResourceIdentifier $b) {
Chris@5 209 return static::compare($a->withArity(0), $b->withArity(0)) === 0;
Chris@5 210 }
Chris@5 211
Chris@5 212 /**
Chris@5 213 * Compares ResourceIdentifier objects.
Chris@5 214 *
Chris@5 215 * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifier $a
Chris@5 216 * The first ResourceIdentifier object.
Chris@5 217 * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifier $b
Chris@5 218 * The second ResourceIdentifier object.
Chris@5 219 *
Chris@5 220 * @return int
Chris@5 221 * Returns 0 if $a and $b are duplicate ResourceIdentifiers. If $a and $b
Chris@5 222 * identify the same resource but have distinct arity values, then the
Chris@5 223 * return value will be arity $a minus arity $b. -1 otherwise.
Chris@5 224 */
Chris@5 225 public static function compare(ResourceIdentifier $a, ResourceIdentifier $b) {
Chris@5 226 $result = strcmp(sprintf('%s:%s', $a->getTypeName(), $a->getId()), sprintf('%s:%s', $b->getTypeName(), $b->getId()));
Chris@5 227 // If type and ID do not match, return their ordering.
Chris@5 228 if ($result !== 0) {
Chris@5 229 return $result;
Chris@5 230 }
Chris@5 231 // If both $a and $b have an arity, then return the order by arity.
Chris@5 232 // Otherwise, they are considered equal.
Chris@5 233 return $a->hasArity() && $b->hasArity()
Chris@5 234 ? $a->getArity() - $b->getArity()
Chris@5 235 : 0;
Chris@5 236 }
Chris@5 237
Chris@5 238 /**
Chris@5 239 * Deduplicates an array of ResourceIdentifier objects.
Chris@5 240 *
Chris@5 241 * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifier[] $resource_identifiers
Chris@5 242 * The list of ResourceIdentifiers to deduplicate.
Chris@5 243 *
Chris@5 244 * @return \Drupal\jsonapi\JsonApiResource\ResourceIdentifier[]
Chris@5 245 * A deduplicated array of ResourceIdentifier objects.
Chris@5 246 *
Chris@5 247 * @see self::isDuplicate()
Chris@5 248 */
Chris@5 249 public static function deduplicate(array $resource_identifiers) {
Chris@5 250 return array_reduce(array_slice($resource_identifiers, 1), function ($deduplicated, $current) {
Chris@5 251 assert($current instanceof static);
Chris@5 252 return array_merge($deduplicated, array_reduce($deduplicated, function ($duplicate, $previous) use ($current) {
Chris@5 253 return $duplicate ?: static::isDuplicate($previous, $current);
Chris@5 254 }, FALSE) ? [] : [$current]);
Chris@5 255 }, array_slice($resource_identifiers, 0, 1));
Chris@5 256 }
Chris@5 257
Chris@5 258 /**
Chris@5 259 * Determines if an array of ResourceIdentifier objects is duplicate free.
Chris@5 260 *
Chris@5 261 * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifier[] $resource_identifiers
Chris@5 262 * The list of ResourceIdentifiers to assess.
Chris@5 263 *
Chris@5 264 * @return bool
Chris@5 265 * Whether all the given resource identifiers are unique.
Chris@5 266 */
Chris@5 267 public static function areResourceIdentifiersUnique(array $resource_identifiers) {
Chris@5 268 return count($resource_identifiers) === count(static::deduplicate($resource_identifiers));
Chris@5 269 }
Chris@5 270
Chris@5 271 /**
Chris@5 272 * Creates a ResourceIdentifier object.
Chris@5 273 *
Chris@5 274 * @param \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item
Chris@5 275 * The entity reference field item from which to create the relationship.
Chris@5 276 * @param int $arity
Chris@5 277 * (optional) The arity of the relationship.
Chris@5 278 *
Chris@5 279 * @return self
Chris@5 280 * A new ResourceIdentifier object.
Chris@5 281 */
Chris@5 282 public static function toResourceIdentifier(EntityReferenceItem $item, $arity = NULL) {
Chris@5 283 $property_name = static::getDataReferencePropertyName($item);
Chris@5 284 $target = $item->get($property_name)->getValue();
Chris@5 285 if ($target === NULL) {
Chris@5 286 return static::getVirtualOrMissingResourceIdentifier($item);
Chris@5 287 }
Chris@5 288 assert($target instanceof EntityInterface);
Chris@5 289 /* @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository */
Chris@5 290 $resource_type_repository = \Drupal::service('jsonapi.resource_type.repository');
Chris@5 291 $resource_type = $resource_type_repository->get($target->getEntityTypeId(), $target->bundle());
Chris@5 292 // Remove unwanted properties from the meta value, usually 'entity'
Chris@5 293 // and 'target_id'.
Chris@5 294 $properties = TypedDataInternalPropertiesHelper::getNonInternalProperties($item);
Chris@5 295 $meta = array_diff_key($properties, array_flip([$property_name, $item->getDataDefinition()->getMainPropertyName()]));
Chris@5 296 if (!is_null($arity)) {
Chris@5 297 $meta[static::ARITY_KEY] = $arity;
Chris@5 298 }
Chris@5 299 return new static($resource_type, $target->uuid(), $meta);
Chris@5 300 }
Chris@5 301
Chris@5 302 /**
Chris@5 303 * Creates an array of ResourceIdentifier objects.
Chris@5 304 *
Chris@5 305 * @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items
Chris@5 306 * The entity reference field items from which to create the relationship
Chris@5 307 * array.
Chris@5 308 *
Chris@5 309 * @return self[]
Chris@5 310 * An array of new ResourceIdentifier objects with appropriate arity values.
Chris@5 311 */
Chris@5 312 public static function toResourceIdentifiers(EntityReferenceFieldItemListInterface $items) {
Chris@5 313 $relationships = [];
Chris@5 314 foreach ($items as $item) {
Chris@5 315 // Create a ResourceIdentifier from the field item. This will make it
Chris@5 316 // comparable with all previous field items. Here, it is assumed that the
Chris@5 317 // resource identifier is unique so it has no arity. If a parallel
Chris@5 318 // relationship is encountered, it will be assigned later.
Chris@5 319 $relationship = static::toResourceIdentifier($item);
Chris@5 320 // Now, iterate over the previously seen resource identifiers in reverse
Chris@5 321 // order. Reverse order is important so that when a parallel relationship
Chris@5 322 // is encountered, it will have the highest arity value so the current
Chris@5 323 // relationship's arity value can simply be incremented by one.
Chris@5 324 /* @var self $existing */
Chris@5 325 foreach (array_reverse($relationships, TRUE) as $index => $existing) {
Chris@5 326 $is_parallel = static::isParallel($existing, $relationship);
Chris@5 327 if ($is_parallel) {
Chris@5 328 // A parallel relationship has been found. If the previous
Chris@5 329 // relationship does not have an arity, it must now be assigned an
Chris@5 330 // arity of 0.
Chris@5 331 if (!$existing->hasArity()) {
Chris@5 332 $relationships[$index] = $existing->withArity(0);
Chris@5 333 }
Chris@5 334 // Since the new ResourceIdentifier is parallel, it must have an arity
Chris@5 335 // assigned to it that is the arity of the last parallel
Chris@5 336 // relationship's arity + 1.
Chris@5 337 $relationship = $relationship->withArity($relationships[$index]->getArity() + 1);
Chris@5 338 break;
Chris@5 339 }
Chris@5 340 }
Chris@5 341 // Finally, append the relationship to the list of ResourceIdentifiers.
Chris@5 342 $relationships[] = $relationship;
Chris@5 343 }
Chris@5 344 return $relationships;
Chris@5 345 }
Chris@5 346
Chris@5 347 /**
Chris@5 348 * Creates an array of ResourceIdentifier objects with arity on every value.
Chris@5 349 *
Chris@5 350 * @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items
Chris@5 351 * The entity reference field items from which to create the relationship
Chris@5 352 * array.
Chris@5 353 *
Chris@5 354 * @return self[]
Chris@5 355 * An array of new ResourceIdentifier objects with appropriate arity values.
Chris@5 356 * Unlike self::toResourceIdentifiers(), this method does not omit arity
Chris@5 357 * when an identifier is not parallel to any other identifier.
Chris@5 358 */
Chris@5 359 public static function toResourceIdentifiersWithArityRequired(EntityReferenceFieldItemListInterface $items) {
Chris@5 360 return array_map(function (ResourceIdentifier $identifier) {
Chris@5 361 return $identifier->hasArity() ? $identifier : $identifier->withArity(0);
Chris@5 362 }, static::toResourceIdentifiers($items));
Chris@5 363 }
Chris@5 364
Chris@5 365 /**
Chris@5 366 * Creates a ResourceIdentifier object.
Chris@5 367 *
Chris@5 368 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@5 369 * The entity from which to create the resource identifier.
Chris@5 370 *
Chris@5 371 * @return self
Chris@5 372 * A new ResourceIdentifier object.
Chris@5 373 */
Chris@5 374 public static function fromEntity(EntityInterface $entity) {
Chris@5 375 /* @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository */
Chris@5 376 $resource_type_repository = \Drupal::service('jsonapi.resource_type.repository');
Chris@5 377 $resource_type = $resource_type_repository->get($entity->getEntityTypeId(), $entity->bundle());
Chris@5 378 return new static($resource_type, $entity->uuid());
Chris@5 379 }
Chris@5 380
Chris@5 381 /**
Chris@5 382 * Helper method to determine which field item property contains an entity.
Chris@5 383 *
Chris@5 384 * @param \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item
Chris@5 385 * The entity reference item for which to determine the entity property
Chris@5 386 * name.
Chris@5 387 *
Chris@5 388 * @return string
Chris@5 389 * The property name which has an entity as its value.
Chris@5 390 */
Chris@5 391 protected static function getDataReferencePropertyName(EntityReferenceItem $item) {
Chris@5 392 foreach ($item->getDataDefinition()->getPropertyDefinitions() as $property_name => $property_definition) {
Chris@5 393 if ($property_definition instanceof DataReferenceDefinitionInterface) {
Chris@5 394 return $property_name;
Chris@5 395 }
Chris@5 396 }
Chris@5 397 }
Chris@5 398
Chris@5 399 /**
Chris@5 400 * Creates a ResourceIdentifier for a NULL or FALSE entity reference item.
Chris@5 401 *
Chris@5 402 * @param \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item
Chris@5 403 * The entity reference field item.
Chris@5 404 *
Chris@5 405 * @return self
Chris@5 406 * A new ResourceIdentifier object.
Chris@5 407 */
Chris@5 408 protected static function getVirtualOrMissingResourceIdentifier(EntityReferenceItem $item) {
Chris@5 409 $resource_type_repository = \Drupal::service('jsonapi.resource_type.repository');
Chris@5 410 $property_name = static::getDataReferencePropertyName($item);
Chris@5 411 $value = $item->get($property_name)->getValue();
Chris@5 412 assert($value === NULL);
Chris@5 413 $field = $item->getParent();
Chris@5 414 assert($field instanceof EntityReferenceFieldItemListInterface);
Chris@5 415 $host_entity = $field->getEntity();
Chris@5 416 assert($host_entity instanceof EntityInterface);
Chris@5 417 $resource_type = $resource_type_repository->get($host_entity->getEntityTypeId(), $host_entity->bundle());
Chris@5 418 assert($resource_type instanceof ResourceType);
Chris@5 419 $relatable_resource_types = $resource_type->getRelatableResourceTypesByField($field->getName());
Chris@5 420 $get_metadata = function ($type) {
Chris@5 421 return [
Chris@5 422 'links' => [
Chris@5 423 'help' => [
Chris@5 424 'href' => "https://www.drupal.org/docs/8/modules/json-api/core-concepts#$type",
Chris@5 425 'meta' => [
Chris@5 426 'about' => "Usage and meaning of the '$type' resource identifier.",
Chris@5 427 ],
Chris@5 428 ],
Chris@5 429 ],
Chris@5 430 ];
Chris@5 431 };
Chris@5 432 $resource_type = reset($relatable_resource_types);
Chris@5 433 // A non-empty entity reference field that refers to a non-existent entity
Chris@5 434 // is not a data integrity problem. For example, Term entities' "parent"
Chris@5 435 // entity reference field uses target_id zero to refer to the non-existent
Chris@5 436 // "<root>" term. And references to entities that no longer exist are not
Chris@5 437 // cleaned up by Drupal; hence we map it to a "missing" resource.
Chris@5 438 if ($field->getFieldDefinition()->getSetting('target_type') === 'taxonomy_term' && $item->get('target_id')->getCastedValue() === 0) {
Chris@5 439 if (count($relatable_resource_types) !== 1) {
Chris@5 440 throw new \RuntimeException('Relationships to virtual resources are possible only if a single resource type is relatable.');
Chris@5 441 }
Chris@5 442 return new static($resource_type, 'virtual', $get_metadata('virtual'));
Chris@5 443 }
Chris@5 444 else {
Chris@5 445 // In case of a dangling reference, it is impossible to determine which
Chris@5 446 // resource type it used to reference, because that requires knowing the
Chris@5 447 // referenced bundle, which Drupal does not store.
Chris@5 448 // If we can reliably determine the resource type of the dangling
Chris@5 449 // reference, use it; otherwise conjure a fake resource type out of thin
Chris@5 450 // air, one that indicates we don't know the bundle.
Chris@5 451 $resource_type = count($relatable_resource_types) > 1
Chris@5 452 ? new ResourceType('?', '?', '')
Chris@5 453 : reset($relatable_resource_types);
Chris@5 454 return new static($resource_type, 'missing', $get_metadata('missing'));
Chris@5 455 }
Chris@5 456 }
Chris@5 457
Chris@5 458 }