Passed
Push — master ( 5d0dc1...648e0b )
by Radu
07:41
created

Document::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Api\JsonApi;
6
7
use WebServCo\Api\JsonApi\Interfaces\ResourceObjectInterface;
8
9
class Document implements \WebServCo\Framework\Interfaces\JsonInterface
10
{
11
    public const CONTENT_TYPE = 'application/vnd.api+json';
12
    public const VERSION = '1.0';
13
14
    /**
15
     * Meta.
16
     *
17
     * @var array<string,mixed>
18
     */
19
    protected array $meta;
20
21
    /**
22
     * JSON API
23
     *
24
     * @var array<string,string>
25
     */
26
    protected array $jsonapi;
27
28
    /**
29
    * Data.
30
    *
31
    * @var array<int,\WebServCo\Api\JsonApi\Interfaces\ResourceObjectInterface>
32
    */
33
    protected array $data;
34
35
    /**
36
    * Errors.
37
    *
38
    * @var array<int,\WebServCo\Api\JsonApi\Error>
39
    */
40
    protected array $errors;
41
42
    protected int $statusCode;
43
44
    public function __construct()
45
    {
46
        $this->meta = [];
47
        $this->jsonapi = ['version' => self::VERSION];
48
        $this->data = [];
49
        $this->errors = [];
50
        $this->statusCode = 200;
51
    }
52
53
    public function getStatusCode(): int
54
    {
55
        return $this->statusCode;
56
    }
57
58
    public function setData(ResourceObjectInterface $resourceObject): bool
59
    {
60
        $this->data[] = $resourceObject;
61
        return true;
62
    }
63
64
    public function setError(Error $error): bool
65
    {
66
        $this->errors[] = $error;
67
        $this->statusCode = $error->getStatus(); // set status code of last error.
68
        return true;
69
    }
70
71
    /**
72
    * @param mixed $value
73
    */
74
    public function setMeta(string $key, $value): bool
75
    {
76
        $this->meta[$key] = $value;
77
        return true;
78
    }
79
80
    public function setStatusCode(int $statusCode): bool
81
    {
82
        $this->statusCode = $statusCode;
83
        return true;
84
    }
85
86
    /**
87
    * @return array<string,mixed>
88
    */
89
    public function toArray(): array
90
    {
91
        $array = [
92
            'jsonapi' => $this->jsonapi,
93
        ];
94
        if ($this->meta) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->meta of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
95
            $array['meta'] = $this->meta;
96
        }
97
        if (!empty($this->errors)) {
98
            foreach ($this->errors as $error) {
99
                $array['errors'][] = $error->toArray();
100
            }
101
        } else {
102
            $dataItems = \count($this->data);
103
            if (1 < $dataItems) { // multiple items
104
                foreach ($this->data as $item) {
105
                    $array['data'][] = $item->toArray();
106
                }
107
            } else {
108
                $array['data'] = \array_key_exists(0, $this->data)
109
                    ? $this->data[0]->toArray() // one item
110
                    : []; // no data
111
            }
112
        }
113
        return $array;
114
    }
115
116
    public function toJson(int $flags = 0): string
117
    {
118
        $array = $this->toArray();
119
        return (string) \json_encode($array, $flags);
120
    }
121
}
122