1 | <?php |
||
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) |
|
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() |
|
62 | |||
63 | /** |
||
64 | * Set the resources array. |
||
65 | * |
||
66 | * @param array $resources |
||
67 | * |
||
68 | * @return void |
||
69 | */ |
||
70 | public function setResources($resources) |
||
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) |
|
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) |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function toArray() |
||
116 | 9 | ||
117 | 6 | /** |
|
118 | 9 | * {@inheritdoc} |
|
119 | */ |
||
120 | 9 | public function toIdentifier() |
|
126 | } |
||
127 |