1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Mapado\RestClientSdk\Collection; |
6
|
|
|
|
7
|
|
|
use ArrayIterator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Collection |
11
|
|
|
* |
12
|
|
|
* @author Florent Clerc <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Collection implements \IteratorAggregate, \Serializable, \Countable, \ArrayAccess |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The elements of the collection. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $elements; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* extra properties that are not the main list but linked data |
25
|
|
|
* It can be "_embed" or "_links" for HAL response |
26
|
|
|
* or "hydra:totalItems" for JSON-LD |
27
|
|
|
* or anything you want to really ("foo" is OK for exemple) |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $extraProperties; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $elements the data elements as an array |
35
|
|
|
* @param array $extraProperties the extra properties |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
array $elements = [], |
39
|
|
|
array $extraProperties = [] |
40
|
|
|
) { |
41
|
1 |
|
$this->elements = $elements; |
42
|
1 |
|
$this->extraProperties = $extraProperties; |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns inner elements collection. |
47
|
|
|
*/ |
48
|
|
|
public function toArray(): array |
49
|
|
|
{ |
50
|
1 |
|
return $this->elements; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function serialize() |
57
|
|
|
{ |
58
|
|
|
return serialize($this->elements); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function unserialize($values) |
65
|
|
|
{ |
66
|
|
|
$this->elements = unserialize($values); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function count() |
73
|
|
|
{ |
74
|
1 |
|
return count($this->elements); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns element count in collection. |
79
|
|
|
*/ |
80
|
|
|
public function getTotalItems(): int |
81
|
|
|
{ |
82
|
1 |
|
return $this->count(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
* |
88
|
|
|
* @param mixed|null $offset |
89
|
|
|
* @param mixed $value |
90
|
|
|
*/ |
91
|
|
|
public function offsetSet($offset, $value) |
92
|
|
|
{ |
93
|
|
|
if (null === $offset) { |
94
|
|
|
$this->elements[] = $value; |
95
|
|
|
} else { |
96
|
|
|
$this->elements[$offset] = $value; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
* |
103
|
|
|
* @param mixed|null $offset |
104
|
|
|
*/ |
105
|
|
|
public function offsetExists($offset) |
106
|
|
|
{ |
107
|
|
|
return isset($this->elements[$offset]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
* |
113
|
|
|
* @param mixed|null $offset |
114
|
|
|
*/ |
115
|
|
|
public function offsetUnset($offset) |
116
|
|
|
{ |
117
|
|
|
unset($this->elements[$offset]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
* |
123
|
|
|
* @param mixed|null $offset |
124
|
|
|
* |
125
|
|
|
* @return mixed|null |
126
|
|
|
*/ |
127
|
|
|
public function offsetGet($offset) |
128
|
|
|
{ |
129
|
|
|
return $this->elements[$offset] |
130
|
|
|
?? null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function getIterator(): ArrayIterator |
137
|
|
|
{ |
138
|
|
|
return new ArrayIterator($this->elements); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* getExtraProperties |
143
|
|
|
*/ |
144
|
|
|
public function getExtraProperties(): array |
145
|
|
|
{ |
146
|
1 |
|
return $this->extraProperties; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* return the value of an extra property |
151
|
|
|
* |
152
|
|
|
* @return mixed |
153
|
|
|
*/ |
154
|
|
|
public function getExtraProperty(string $key) |
155
|
|
|
{ |
156
|
1 |
|
if (isset($this->extraProperties[$key])) { |
157
|
1 |
|
return $this->extraProperties[$key]; |
158
|
|
|
} |
159
|
1 |
|
} |
160
|
|
|
} |
161
|
|
|
|