1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Directus – <http://getdirectus.com> |
5
|
|
|
* |
6
|
|
|
* @link The canonical repository – <https://github.com/directus/directus> |
7
|
|
|
* @copyright Copyright 2006-2016 RANGER Studio, LLC – <http://rangerstudio.com> |
8
|
|
|
* @license GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Directus\SDK\Response; |
12
|
|
|
|
13
|
|
|
use Directus\Util\ArrayUtils; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Entry Collection |
17
|
|
|
* |
18
|
|
|
* @author Welling Guzmán <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class EntryCollection implements ResponseInterface, \IteratorAggregate, \ArrayAccess, \Countable |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $items = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $rawData = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $metadata = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* EntryCollection constructor. |
39
|
|
|
* |
40
|
|
|
* @param $data |
41
|
|
|
*/ |
42
|
8 |
|
public function __construct($data) |
43
|
|
|
{ |
44
|
8 |
|
$this->rawData = $data; |
45
|
8 |
|
$this->metadata = ArrayUtils::omit($data, 'rows'); |
46
|
|
|
|
47
|
8 |
|
$rows = isset($data['rows']) ? $data['rows'] : []; |
48
|
8 |
|
$items = []; |
49
|
8 |
|
foreach($rows as $row) { |
50
|
8 |
|
$items[] = new Entry($row); |
51
|
8 |
|
} |
52
|
|
|
|
53
|
8 |
|
$this->items = $items; |
54
|
8 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the response raw data |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
2 |
|
public function getRawData() |
62
|
|
|
{ |
63
|
2 |
|
return $this->rawData; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the response entries |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
2 |
|
public function getData() |
72
|
|
|
{ |
73
|
2 |
|
return $this->items; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the response metadata |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
2 |
|
public function getMetaData() |
82
|
|
|
{ |
83
|
2 |
|
return $this->metadata; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Create a new iterator based on this collection |
88
|
|
|
* |
89
|
|
|
* @return \ArrayIterator |
90
|
|
|
*/ |
91
|
2 |
|
public function getIterator() |
92
|
|
|
{ |
93
|
2 |
|
return new \ArrayIterator($this->items); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function offsetExists($offset) |
97
|
|
|
{ |
98
|
|
|
return array_key_exists($offset, $this->items); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function offsetGet($offset) |
102
|
|
|
{ |
103
|
|
|
return $this->items[$offset]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function offsetSet($offset, $value) |
107
|
|
|
{ |
108
|
|
|
throw new \BadMethodCallException('EntryCollection is read only'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function offsetUnset($offset) |
112
|
|
|
{ |
113
|
|
|
throw new \BadMethodCallException('EntryCollection is read only'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Gets the number of entries in this collection |
118
|
|
|
* |
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
2 |
|
public function count() |
122
|
|
|
{ |
123
|
2 |
|
return count($this->items); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Gets an object representation of this collection |
128
|
|
|
* |
129
|
|
|
* @return object |
130
|
|
|
*/ |
131
|
2 |
|
public function jsonSerialize() |
132
|
|
|
{ |
133
|
|
|
return (object) [ |
134
|
2 |
|
'metadata' => $this->metadata, |
135
|
2 |
|
'data' => $this->items |
136
|
2 |
|
]; |
137
|
|
|
} |
138
|
|
|
} |