1 | <?php |
||
2 | /** |
||
3 | * Resource Collection Helper Class |
||
4 | */ |
||
5 | |||
6 | namespace Twigger\UnionCloud\API; |
||
7 | |||
8 | |||
9 | use Twigger\UnionCloud\API\Exception\Resource\ResourceNotFoundException; |
||
10 | |||
11 | /** |
||
12 | * Contains a wrapper for an array, providing helpful functions. |
||
13 | * |
||
14 | * Designed with classes implementing IResource in mind |
||
15 | * |
||
16 | * Class ResourceCollection |
||
17 | * |
||
18 | * @package Twigger\UnionCloud\API\Core |
||
19 | */ |
||
20 | class ResourceCollection implements \IteratorAggregate |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var array $resources The plain array |
||
25 | */ |
||
26 | private $resources = []; |
||
27 | |||
28 | /** |
||
29 | * ResourceCollection constructor. |
||
30 | */ |
||
31 | public function __construct() |
||
32 | { |
||
33 | |||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Add an element to the array |
||
38 | * |
||
39 | * @param mixed Resource to add |
||
40 | * |
||
41 | * @return void |
||
42 | */ |
||
43 | public function addResource($resource) |
||
44 | { |
||
45 | $this->resources[] = $resource; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Add multiple resources to the collection |
||
50 | * |
||
51 | * @param mixed[] $resources Arrays of resources to add to the collection |
||
52 | */ |
||
53 | public function addResources($resources) |
||
54 | { |
||
55 | $this->resources = array_merge($this->resources, $resources); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Return the plain array as an array |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | public function toArray() |
||
64 | { |
||
65 | return $this->resources; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Return a JSON representation of the resource collection |
||
70 | * |
||
71 | * @return string|false |
||
72 | */ |
||
73 | public function toJson() |
||
74 | { |
||
75 | return json_encode($this->resources); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Return the first resource in the collection |
||
80 | * |
||
81 | * @return mixed |
||
82 | * |
||
83 | * @throws ResourceNotFoundException |
||
84 | */ |
||
85 | public function first() |
||
86 | { |
||
87 | if (!isset($this->resources[0])) |
||
88 | { |
||
89 | throw new ResourceNotFoundException('No resources were found.', 404); |
||
90 | } |
||
91 | return $this->resources[0]; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Create an array populated by the key in each of the models |
||
96 | * |
||
97 | * @param string $key |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | public function pluck($key) |
||
102 | { |
||
103 | $values = array(); |
||
104 | |||
105 | foreach($this->resources as $resource) |
||
106 | { |
||
107 | if(($value = $resource->$key) !== false) |
||
108 | { |
||
109 | $values[] = $value; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | return $values; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Allow iteration over the elements in the collection |
||
118 | * |
||
119 | * @return \Traversable|ArrayIterator |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
120 | */ |
||
121 | public function getIterator() { |
||
122 | return new \ArrayIterator( $this->resources ); |
||
123 | } |
||
124 | |||
125 | } |