Total Complexity | 10 |
Total Lines | 57 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
4 | class Structure |
||
5 | { |
||
6 | protected $meta; |
||
7 | protected $jsonapi; |
||
8 | protected $data; |
||
9 | protected $errors; |
||
10 | |||
11 | const CONTENT_TYPE = 'application/vnd.api+json'; |
||
12 | const VERSION = '1.0'; |
||
13 | |||
14 | public function __construct() |
||
15 | { |
||
16 | $this->meta = []; |
||
17 | $this->jsonapi = ['version' => self::VERSION]; |
||
18 | $this->data = []; |
||
19 | $this->errors = []; |
||
20 | } |
||
21 | |||
22 | public function setData(\WebServCo\Api\JsonApi\Interfaces\ResourceObjectInterface $resourceObject) |
||
23 | { |
||
24 | $this->data[] = $resourceObject; |
||
25 | } |
||
26 | |||
27 | public function setError(\WebServCo\Api\JsonApi\Error $error) |
||
30 | } |
||
31 | |||
32 | public function toArray() |
||
33 | { |
||
34 | $array = [ |
||
35 | 'jsonapi' => $this->jsonapi, |
||
36 | ]; |
||
37 | if ($this->meta) { |
||
|
|||
38 | $array['meta'] = $this->meta; |
||
39 | } |
||
40 | if (!empty($this->errors)) { |
||
41 | foreach ($this->errors as $error) { |
||
42 | $array['errors'][] = $error->toArray(); |
||
43 | } |
||
44 | } else { |
||
45 | $dataItems = count($this->data); |
||
46 | if (1 < $dataItems) { |
||
47 | foreach ($this->data as $item) { |
||
48 | $array['data'][] = $item->toArray(); |
||
49 | } |
||
50 | } else { |
||
51 | $array['data'] = $this->data[0]->toArray(); |
||
52 | } |
||
53 | } |
||
54 | return $array; |
||
55 | } |
||
56 | |||
57 | public function toJson() |
||
61 | } |
||
62 | } |
||
63 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.