protected static function RelationshipNormalizerValue::ensureUniqueResourceIdentifierObjects in JSON:API 8
Ensures each resource identifier object is unique.
The official JSON API JSON-Schema document requires that no two resource identifier objects are duplicated.
This adds an
arity;
member to each object's
meta;
member. The value of this member is an integer that is incremented by 1 (starting from 0) for each repeated resource identifier sharing a common
type;
and
id;
.
Parameters
array $resource_identifier_objects: A list of JSON API resource identifier objects.
Return value
array A set of JSON API resource identifier objects, with those having multiple occurrences getting [meta][arity].
See also
http://jsonapi.org/format/#document-resource-object-relationships
https://github.com/json-api/json-api/pull/1156#issuecomment-325377995
https://www.drupal.org/project/jsonapi/issues/2864680
1 call to RelationshipNormalizerValue::ensureUniqueResourceIdentifierObjects()
- RelationshipNormalizerValue::rasterizeValue in src/
Normalizer/ Value/ RelationshipNormalizerValue.php - Get the rasterized value.
File
- src/
Normalizer/ Value/ RelationshipNormalizerValue.php, line 110
Class
- RelationshipNormalizerValue
- Helps normalize relationships in compliance with the JSON API spec.
Namespace
Drupal\jsonapi\Normalizer\ValueCode
protected static function ensureUniqueResourceIdentifierObjects(array $resource_identifier_objects) {
if (count($resource_identifier_objects) <= 1) {
return $resource_identifier_objects;
}
// Count each repeated resource identifier and track their array indices.
$analysis = [];
foreach ($resource_identifier_objects as $index => $rio) {
$composite_key = $rio['type'] . ':' . $rio['id'];
$analysis[$composite_key]['count'] = isset($analysis[$composite_key]) ? $analysis[$composite_key]['count'] + 1 : 0;
// The index will later be used to assign an arity to repeated resource
// identifier objects. Doing this in two phases prevents adding an arity
// to objects which only occur once.
$analysis[$composite_key]['indices'][] = $index;
}
// Assign an arity to objects whose type + ID pair occurred more than once.
foreach ($analysis as $computed) {
if ($computed['count'] > 0) {
foreach ($computed['indices'] as $arity => $index) {
$resource_identifier_objects[$index]['meta']['arity'] = $arity;
}
}
}
return $resource_identifier_objects;
}