Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/jsonapi/src/JsonApiResource/Data.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 |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\jsonapi\JsonApiResource; | |
4 | |
5 use Drupal\Component\Assertion\Inspector; | |
6 use Drupal\jsonapi\Exception\EntityAccessDeniedHttpException; | |
7 | |
8 /** | |
9 * Represents the `data` and `included` objects of a top-level object. | |
10 * | |
11 * @internal JSON:API maintains no PHP API. The API is the HTTP API. This class | |
12 * may change at any time and could break any dependencies on it. | |
13 * | |
14 * @see https://www.drupal.org/project/jsonapi/issues/3032787 | |
15 * @see jsonapi.api.php | |
16 */ | |
17 abstract class Data implements \IteratorAggregate, \Countable { | |
18 | |
19 /** | |
20 * Various representations of JSON:API objects. | |
21 * | |
22 * @var \Drupal\jsonapi\JsonApiResource\ResourceIdentifierInterface[] | |
23 */ | |
24 protected $data; | |
25 | |
26 /** | |
27 * The number of resources permitted in this collection. | |
28 * | |
29 * @var int | |
30 */ | |
31 protected $cardinality; | |
32 | |
33 /** | |
34 * Holds a boolean indicating if there is a next page. | |
35 * | |
36 * @var bool | |
37 */ | |
38 protected $hasNextPage; | |
39 | |
40 /** | |
41 * Holds the total count of entities. | |
42 * | |
43 * @var int | |
44 */ | |
45 protected $count; | |
46 | |
47 /** | |
48 * Instantiates a Data object. | |
49 * | |
50 * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifierInterface[] $data | |
51 * The resources or resource identifiers for the collection. | |
52 * @param int $cardinality | |
53 * The number of resources that this collection may contain. Related | |
54 * resource collections may handle both to-one or to-many relationships. A | |
55 * to-one relationship should have a cardinality of 1. Use -1 for unlimited | |
56 * cardinality. | |
57 */ | |
58 public function __construct(array $data, $cardinality = -1) { | |
59 assert(Inspector::assertAllObjects($data, ResourceIdentifierInterface::class)); | |
60 assert($cardinality >= -1 && $cardinality !== 0, 'Cardinality must be -1 for unlimited cardinality or a positive integer.'); | |
61 assert($cardinality === -1 || count($data) <= $cardinality, 'If cardinality is not unlimited, the number of given resources must not exceed the cardinality of the collection.'); | |
62 $this->data = array_values($data); | |
63 $this->cardinality = $cardinality; | |
64 } | |
65 | |
66 /** | |
67 * Returns an iterator for entities. | |
68 * | |
69 * @return \ArrayIterator | |
70 * An \ArrayIterator instance | |
71 */ | |
72 public function getIterator() { | |
73 return new \ArrayIterator($this->data); | |
74 } | |
75 | |
76 /** | |
77 * Returns the number of entities. | |
78 * | |
79 * @return int | |
80 * The number of parameters | |
81 */ | |
82 public function count() { | |
83 return count($this->data); | |
84 } | |
85 | |
86 /** | |
87 * {@inheritdoc} | |
88 */ | |
89 public function getTotalCount() { | |
90 return $this->count; | |
91 } | |
92 | |
93 /** | |
94 * {@inheritdoc} | |
95 */ | |
96 public function setTotalCount($count) { | |
97 $this->count = $count; | |
98 } | |
99 | |
100 /** | |
101 * Returns the collection as an array. | |
102 * | |
103 * @return \Drupal\Core\Entity\EntityInterface[] | |
104 * The array of entities. | |
105 */ | |
106 public function toArray() { | |
107 return $this->data; | |
108 } | |
109 | |
110 /** | |
111 * Checks if there is a next page in the collection. | |
112 * | |
113 * @return bool | |
114 * TRUE if the collection has a next page. | |
115 */ | |
116 public function hasNextPage() { | |
117 return (bool) $this->hasNextPage; | |
118 } | |
119 | |
120 /** | |
121 * Sets the has next page flag. | |
122 * | |
123 * Once the collection query has been executed and we build the entity | |
124 * collection, we now if there will be a next page with extra entities. | |
125 * | |
126 * @param bool $has_next_page | |
127 * TRUE if the collection has a next page. | |
128 */ | |
129 public function setHasNextPage($has_next_page) { | |
130 $this->hasNextPage = (bool) $has_next_page; | |
131 } | |
132 | |
133 /** | |
134 * Gets the cardinality of this collection. | |
135 * | |
136 * @return int | |
137 * The cardinality of the resource collection. -1 for unlimited cardinality. | |
138 */ | |
139 public function getCardinality() { | |
140 return $this->cardinality; | |
141 } | |
142 | |
143 /** | |
144 * Returns a new Data object containing the entities of $this and $other. | |
145 * | |
146 * @param \Drupal\jsonapi\JsonApiResource\Data $a | |
147 * A Data object object to be merged. | |
148 * @param \Drupal\jsonapi\JsonApiResource\Data $b | |
149 * A Data object to be merged. | |
150 * | |
151 * @return \Drupal\jsonapi\JsonApiResource\Data | |
152 * A new merged Data object. | |
153 */ | |
154 public static function merge(Data $a, Data $b) { | |
155 return new static(array_merge($a->toArray(), $b->toArray())); | |
156 } | |
157 | |
158 /** | |
159 * Returns a new, deduplicated Data object. | |
160 * | |
161 * @param \Drupal\jsonapi\JsonApiResource\Data $collection | |
162 * The Data object to deduplicate. | |
163 * | |
164 * @return static | |
165 * A new merged Data object. | |
166 */ | |
167 public static function deduplicate(Data $collection) { | |
168 $deduplicated = []; | |
169 foreach ($collection as $resource) { | |
170 $dedupe_key = $resource->getTypeName() . ':' . $resource->getId(); | |
171 if ($resource instanceof EntityAccessDeniedHttpException && ($error = $resource->getError()) && !is_null($error['relationship_field'])) { | |
172 $dedupe_key .= ':' . $error['relationship_field']; | |
173 } | |
174 $deduplicated[$dedupe_key] = $resource; | |
175 } | |
176 return new static(array_values($deduplicated)); | |
177 } | |
178 | |
179 } |