Collection   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 75.76%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 113
ccs 25
cts 33
cp 0.7576
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getResources() 0 4 1
A setResources() 0 4 1
A with() 0 8 2
A buildResources() 0 14 3
A fields() 0 8 2
A toArray() 0 6 1
A toIdentifier() 0 6 1
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 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 9
        }, $this->resources);
115
    }
116 9
117 6
    /**
118 9
     * {@inheritdoc}
119
     */
120 9
    public function toIdentifier()
121
    {
122
        return array_map(function (Resource $resource) {
123
            return $resource->toIdentifier();
124
        }, $this->resources);
125
    }
126
}
127