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
|
|
|
* @access private |
29
|
|
|
*/ |
30
|
|
|
private $extraProperties; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array $elements The data elements as an array. |
34
|
|
|
* @param array $extraProperties The extra properties. |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $elements = [], array $extraProperties = []) |
37
|
|
|
{ |
38
|
1 |
|
$this->elements = $elements; |
39
|
1 |
|
$this->extraProperties = $extraProperties; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns inner elements collection. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function toArray() |
48
|
|
|
{ |
49
|
1 |
|
return $this->elements; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function serialize() |
56
|
|
|
{ |
57
|
|
|
return serialize($this->elements); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function unserialize($values) |
64
|
|
|
{ |
65
|
|
|
$this->elements = unserialize($values); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function count() |
72
|
|
|
{ |
73
|
1 |
|
return count($this->elements); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns element count in collection. |
78
|
|
|
* |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public function getTotalItems() |
82
|
|
|
{ |
83
|
1 |
|
return $this->count(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function offsetSet($offset, $value) |
90
|
|
|
{ |
91
|
|
|
if (is_null($offset)) { |
92
|
|
|
$this->elements[] = $value; |
93
|
|
|
} else { |
94
|
|
|
$this->elements[$offset] = $value; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function offsetExists($offset) |
102
|
|
|
{ |
103
|
|
|
return isset($this->elements[$offset]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function offsetUnset($offset) |
110
|
|
|
{ |
111
|
|
|
unset($this->elements[$offset]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function offsetGet($offset) |
118
|
|
|
{ |
119
|
|
|
return isset($this->elements[$offset]) ? $this->elements[$offset] : null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function getIterator() |
126
|
|
|
{ |
127
|
|
|
return new ArrayIterator($this->elements); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* getExtraProperties |
132
|
|
|
* |
133
|
|
|
* @access public |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getExtraProperties() |
137
|
|
|
{ |
138
|
1 |
|
return $this->extraProperties; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* return the value of an extra property |
143
|
|
|
* |
144
|
|
|
* @param string $key |
145
|
|
|
* @access public |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
|
|
public function getExtraProperty($key) |
149
|
|
|
{ |
150
|
1 |
|
if (isset($this->extraProperties[$key])) { |
151
|
1 |
|
return $this->extraProperties[$key]; |
152
|
|
|
} |
153
|
1 |
|
} |
154
|
|
|
} |
155
|
|
|
|