1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of JSON-API. |
5
|
|
|
* |
6
|
|
|
* (c) Toby Zerner <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tobscure\JsonApi; |
13
|
|
|
|
14
|
|
|
class Collection implements ElementInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $resources = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Create a new collection instance. |
23
|
|
|
* |
24
|
|
|
* @param mixed $data |
25
|
|
|
* @param \Tobscure\JsonApi\SerializerInterface $serializer |
26
|
|
|
*/ |
27
|
18 |
|
public function __construct($data, SerializerInterface $serializer) |
28
|
|
|
{ |
29
|
18 |
|
$this->resources = $this->buildResources($data, $serializer); |
30
|
18 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Convert an array of raw data to Resource objects. |
34
|
|
|
* |
35
|
|
|
* @param mixed $data |
36
|
|
|
* @param SerializerInterface $serializer |
37
|
|
|
* |
38
|
|
|
* @return \Tobscure\JsonApi\Resource[] |
39
|
|
|
*/ |
40
|
18 |
|
protected function buildResources($data, SerializerInterface $serializer) |
41
|
|
|
{ |
42
|
18 |
|
$resources = []; |
43
|
|
|
|
44
|
18 |
|
foreach ($data as $resource) { |
45
|
12 |
|
if (! ($resource instanceof Resource)) { |
46
|
12 |
|
$resource = new Resource($resource, $serializer); |
47
|
12 |
|
} |
48
|
|
|
|
49
|
12 |
|
$resources[] = $resource; |
50
|
18 |
|
} |
51
|
|
|
|
52
|
18 |
|
return $resources; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
3 |
|
public function getResources() |
59
|
|
|
{ |
60
|
3 |
|
return $this->resources; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set the resources array. |
65
|
|
|
* |
66
|
|
|
* @param array $resources |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function setResources($resources) |
71
|
|
|
{ |
72
|
|
|
$this->resources = $resources; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Request a relationship to be included for all resources. |
77
|
|
|
* |
78
|
|
|
* @param string|array $relationships |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
9 |
|
public function with($relationships) |
83
|
|
|
{ |
84
|
9 |
|
foreach ($this->resources as $resource) { |
85
|
6 |
|
$resource->with($relationships); |
86
|
9 |
|
} |
87
|
|
|
|
88
|
9 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Request a relationship to be identified for all resources. |
93
|
|
|
* |
94
|
|
|
* @param string|array $relationships |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function identify($relationships) |
99
|
|
|
{ |
100
|
|
|
foreach ($this->resources as $resource) { |
101
|
|
|
$resource->identify($relationships); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Request a restricted set of fields. |
109
|
|
|
* |
110
|
|
|
* @param array|null $fields |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
9 |
|
public function fields($fields) |
115
|
|
|
{ |
116
|
9 |
|
foreach ($this->resources as $resource) { |
117
|
6 |
|
$resource->fields($fields); |
118
|
9 |
|
} |
119
|
|
|
|
120
|
9 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
3 |
|
public function toArray() |
127
|
|
|
{ |
128
|
|
|
return array_map(function (Resource $resource) { |
129
|
3 |
|
return $resource->toArray(); |
130
|
3 |
|
}, $this->resources); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function toIdentifier() |
137
|
|
|
{ |
138
|
12 |
|
return array_map(function (Resource $resource) { |
139
|
9 |
|
return $resource->toIdentifier(); |
140
|
12 |
|
}, $this->resources); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|