-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCollection.php
199 lines (175 loc) · 4.09 KB
/
Collection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
namespace Mapado\RestClientSdk\Collection;
/**
* Class Collection
*
* @author Florent Clerc <[email protected]>
*/
class Collection implements
\IteratorAggregate,
\Serializable,
\Countable,
\ArrayAccess
{
/**
* The elements of the collection.
*
* @var array
*/
private $elements;
/**
* extra properties that are not the main list but linked data
* It can be "_embed" or "_links" for HAL response
* or "hydra:totalItems" for JSON-LD
* or anything you want to really ("foo" is OK for exemple)
*
* @var array
*/
private $extraProperties;
/**
* @param array $elements the data elements as an array
* @param array $extraProperties the extra properties
*/
public function __construct(
array $elements = [],
array $extraProperties = []
) {
$this->elements = $elements;
$this->extraProperties = $extraProperties;
}
/**
* @return array<string, mixed>
*/
public function __serialize(): array
{
return $this->elements;
}
/**
* {@inheritdoc}
*
* @param string $values
*/
public function __unserialize($values): void
{
$unserializedValues = unserialize($values);
if (is_array($unserializedValues)) {
$this->elements = $unserializedValues;
}
}
/**
* Returns inner elements collection.
*/
public function toArray(): array
{
return $this->elements;
}
/**
* {@inheritdoc}
*
* @deprecated `serialize` method is deprecated, `__serialize` is used instead. See https://php.watch/versions/8.1/serializable-deprecated
*/
public function serialize(): string
{
return serialize($this->__serialize());
}
/**
* @deprecated `unserialize` method is deprecated, `__unserialize` is used instead. See https://php.watch/versions/8.1/serializable-deprecated
*/
public function unserialize($data): void
{
$this->__unserialize($data);
}
/**
* {@inheritdoc}
*/
public function count(): int
{
return count($this->elements);
}
/**
* Returns element count in collection.
*/
public function getTotalItems(): int
{
return $this->count();
}
/**
* {@inheritdoc}
*
* @param mixed|null $offset
* @param mixed $value
*/
public function offsetSet($offset, $value): void
{
if (null === $offset) {
$this->elements[] = $value;
} else {
$this->elements[$offset] = $value;
}
}
/**
* {@inheritdoc}
*
* @param mixed|null $offset
*/
public function offsetExists($offset): bool
{
return isset($this->elements[$offset]);
}
/**
* {@inheritdoc}
*
* @param mixed|null $offset
*/
public function offsetUnset($offset): void
{
unset($this->elements[$offset]);
}
/**
* {@inheritdoc}
*
* @param mixed|null $offset
*
* @return mixed|null
*/
public function offsetGet($offset): mixed
{
return $this->elements[$offset] ?? null;
}
/**
* {@inheritdoc}
*/
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->elements);
}
/**
* getExtraProperties
*/
public function getExtraProperties(): array
{
return $this->extraProperties;
}
public function getStringExtraProperty(string $key): ?string
{
$value = $this->getExtraProperty($key);
return is_string($value) ? $value : null;
}
public function getIntExtraProperty(string $key): ?int
{
$value = $this->getExtraProperty($key);
return is_int($value) ? $value : null;
}
/**
* return the value of an extra property
*
* @return mixed
*/
public function getExtraProperty(string $key)
{
if (isset($this->extraProperties[$key])) {
return $this->extraProperties[$key];
}
}
}