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
|
|
View Code Duplication |
class PolymorphicCollection 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\SerializerRegistryInterface $serializers |
26
|
|
|
*/ |
27
|
|
|
public function __construct($data, SerializerRegistryInterface $serializers) |
28
|
|
|
{ |
29
|
|
|
$this->resources = $this->buildResources($data, $serializers); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Convert an array of raw data to Resource objects. |
34
|
|
|
* |
35
|
|
|
* @param mixed $data |
36
|
|
|
* @param \Tobscure\JsonApi\SerializerRegistryInterface $serializers |
37
|
|
|
* @return \Tobscure\JsonApi\Resource[] |
38
|
|
|
*/ |
39
|
|
|
protected function buildResources($data, SerializerRegistryInterface $serializers) |
40
|
|
|
{ |
41
|
|
|
$resources = []; |
42
|
|
|
|
43
|
|
|
foreach ($data as $resource) { |
44
|
|
|
if (! ($resource instanceof Resource)) { |
45
|
|
|
$resource = new Resource($resource, $serializers->getFromSerializable($resource)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$resources[] = $resource; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $resources; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function getResources() |
58
|
|
|
{ |
59
|
|
|
return $this->resources; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the resources array. |
64
|
|
|
* |
65
|
|
|
* @param array $resources |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function setResources($resources) |
70
|
|
|
{ |
71
|
|
|
$this->resources = $resources; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Request a relationship to be included for all resources. |
76
|
|
|
* |
77
|
|
|
* @param string|array $relationships |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function with($relationships) |
82
|
|
|
{ |
83
|
|
|
foreach ($this->resources as $resource) { |
84
|
|
|
$resource->with($relationships); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Request a restricted set of fields. |
92
|
|
|
* |
93
|
|
|
* @param array|null $fields |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function fields($fields) |
98
|
|
|
{ |
99
|
|
|
foreach ($this->resources as $resource) { |
100
|
|
|
$resource->fields($fields); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function toArray() |
110
|
|
|
{ |
111
|
|
|
return array_map(function (Resource $resource) { |
112
|
|
|
return $resource->toArray(); |
113
|
|
|
}, $this->resources); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function toIdentifier() |
120
|
|
|
{ |
121
|
|
|
return array_map(function (Resource $resource) { |
122
|
|
|
return $resource->toIdentifier(); |
123
|
|
|
}, $this->resources); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.