|
1
|
|
|
<?php |
|
2
|
|
|
namespace Akkroo; |
|
3
|
|
|
|
|
4
|
|
|
use ArrayObject; |
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use LogicException; |
|
7
|
|
|
use JsonSerializable; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Manage a collection of resources |
|
11
|
|
|
* |
|
12
|
|
|
* The collection behaves like an array of objects, it can be filtered and serialized to JSON |
|
13
|
|
|
*/ |
|
14
|
|
|
class Collection extends ArrayObject implements JsonSerializable |
|
15
|
|
|
{ |
|
16
|
|
|
protected $itemType = null; |
|
17
|
|
|
|
|
18
|
|
|
protected $writeable = true; |
|
19
|
|
|
|
|
20
|
|
|
protected $requestID = null; |
|
21
|
|
|
|
|
22
|
|
|
protected $meta = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Constructor |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $data Array of resources |
|
28
|
|
|
* @param string $itemType Acceptable resource class |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
11 |
|
public function __construct(array $data = [], string $itemType = Resource::class) |
|
32
|
|
|
{ |
|
33
|
11 |
|
$this->itemType = $itemType; |
|
34
|
11 |
|
array_map(function ($item) { |
|
35
|
11 |
|
if (!empty($item)) { |
|
36
|
11 |
|
if (is_array($item)) { |
|
37
|
10 |
|
$this->append(new $this->itemType($item)); |
|
38
|
10 |
|
return $item; |
|
39
|
|
|
} |
|
40
|
2 |
|
if (is_a($item, $this->itemType)) { |
|
41
|
1 |
|
$this->append($item); |
|
42
|
1 |
|
return $item; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
1 |
|
throw new InvalidArgumentException('Invalid item data: array expected'); |
|
46
|
11 |
|
}, $data); |
|
47
|
10 |
|
$this->writeable = false; |
|
48
|
10 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Sets the value at the specified index to newval |
|
52
|
|
|
* |
|
53
|
|
|
* Wraps parent method to account for read-only collections |
|
54
|
|
|
* |
|
55
|
|
|
* @param mixed $index The index being set |
|
56
|
|
|
* @param mixed $newval The new value for the index |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
10 |
|
public function offsetSet($index, $newval) |
|
60
|
|
|
{ |
|
61
|
10 |
|
if (!$this->writeable) { |
|
62
|
1 |
|
throw new LogicException('Cannot add elements to a read-only collection'); |
|
63
|
|
|
} |
|
64
|
10 |
|
parent::offsetSet($index, $newval); |
|
65
|
10 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Unsets the value at the specified index |
|
69
|
|
|
* |
|
70
|
|
|
* Wraps parent method to account for read-only collections |
|
71
|
|
|
* Unlike offsetSet we don't need to check for witeable here, |
|
72
|
|
|
* because every collection is read-only once created and |
|
73
|
|
|
* cannot be made writeable |
|
74
|
|
|
* |
|
75
|
|
|
* @param mixed $index The index being unset |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function offsetUnset($index) |
|
79
|
|
|
{ |
|
80
|
1 |
|
throw new LogicException('Cannot remove elements from a read-only collection'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Link a collection to an API request |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $value ID provided by HTTP client |
|
87
|
|
|
* @return \Akkroo\Collection |
|
88
|
|
|
*/ |
|
89
|
4 |
|
public function withRequestID(string $value) |
|
90
|
|
|
{ |
|
91
|
4 |
|
$this->requestID = $value; |
|
92
|
4 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Embed pagination metadata |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $data Associative array of metadata |
|
99
|
|
|
* @return \Akkroo\Collection |
|
100
|
|
|
*/ |
|
101
|
5 |
|
public function withMeta(array $data) |
|
102
|
|
|
{ |
|
103
|
5 |
|
$this->meta = $data; |
|
104
|
5 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Fetch pagination metadata |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $key Specific key to retrieve |
|
111
|
|
|
* @return string|array |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function getMeta(string $key = null) |
|
114
|
|
|
{ |
|
115
|
1 |
|
if (!empty($key)) { |
|
116
|
1 |
|
if (isset($this->meta[$key])) { |
|
117
|
1 |
|
return $this->meta[$key]; |
|
118
|
|
|
} |
|
119
|
1 |
|
return null; |
|
120
|
|
|
} |
|
121
|
1 |
|
return $this->meta; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Customize JSON encode output |
|
126
|
|
|
* |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function jsonSerialize() |
|
130
|
|
|
{ |
|
131
|
1 |
|
return $this->getArrayCopy(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Filters elements of an array using a callback function |
|
136
|
|
|
* |
|
137
|
|
|
* @param callable $callback The callback function to use |
|
138
|
|
|
* @return \Akkroo\Collection The filtered collection |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function filter(callable $callback) |
|
141
|
|
|
{ |
|
142
|
1 |
|
return new self(array_filter($this->getArrayCopy(), $callback), $this->itemType); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|