@@ 14-126 (lines=113) @@ | ||
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 | public function __construct($data, SerializerInterface $serializer) |
|
28 | { |
|
29 | $this->resources = $this->buildResources($data, $serializer); |
|
30 | } |
|
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 | protected function buildResources($data, SerializerInterface $serializer) |
|
41 | { |
|
42 | $resources = []; |
|
43 | ||
44 | foreach ($data as $resource) { |
|
45 | if (! ($resource instanceof Resource)) { |
|
46 | $resource = new Resource($resource, $serializer); |
|
47 | } |
|
48 | ||
49 | $resources[] = $resource; |
|
50 | } |
|
51 | ||
52 | return $resources; |
|
53 | } |
|
54 | ||
55 | /** |
|
56 | * {@inheritdoc} |
|
57 | */ |
|
58 | public function getResources() |
|
59 | { |
|
60 | 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 | public function with($relationships) |
|
83 | { |
|
84 | foreach ($this->resources as $resource) { |
|
85 | $resource->with($relationships); |
|
86 | } |
|
87 | ||
88 | return $this; |
|
89 | } |
|
90 | ||
91 | /** |
|
92 | * Request a restricted set of fields. |
|
93 | * |
|
94 | * @param array|null $fields |
|
95 | * |
|
96 | * @return $this |
|
97 | */ |
|
98 | public function fields($fields) |
|
99 | { |
|
100 | foreach ($this->resources as $resource) { |
|
101 | $resource->fields($fields); |
|
102 | } |
|
103 | ||
104 | return $this; |
|
105 | } |
|
106 | ||
107 | /** |
|
108 | * {@inheritdoc} |
|
109 | */ |
|
110 | public function toArray() |
|
111 | { |
|
112 | return array_map(function (Resource $resource) { |
|
113 | return $resource->toArray(); |
|
114 | }, $this->resources); |
|
115 | } |
|
116 | ||
117 | /** |
|
118 | * {@inheritdoc} |
|
119 | */ |
|
120 | public function toIdentifier() |
|
121 | { |
|
122 | return array_map(function (Resource $resource) { |
|
123 | return $resource->toIdentifier(); |
|
124 | }, $this->resources); |
|
125 | } |
|
126 | } |
|
127 |
@@ 14-125 (lines=112) @@ | ||
11 | ||
12 | namespace Tobscure\JsonApi; |
|
13 | ||
14 | 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 |