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