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
|
|
|
|
12
|
|
|
public const CONTENT_TYPE = 'application/vnd.api+json'; |
13
|
|
|
public const VERSION = '1.0'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Meta. |
17
|
|
|
* |
18
|
|
|
* @var array<string,int|string> |
19
|
|
|
*/ |
20
|
|
|
protected array $meta; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* JSON API |
24
|
|
|
* |
25
|
|
|
* @var array<string,string> |
26
|
|
|
*/ |
27
|
|
|
protected array $jsonapi; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Data. |
31
|
|
|
* |
32
|
|
|
* @var array<int, \WebServCo\Api\JsonApi\Interfaces\ResourceObjectInterface> |
33
|
|
|
*/ |
34
|
|
|
protected array $data; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Errors. |
38
|
|
|
* |
39
|
|
|
* @var array<int,\WebServCo\Api\JsonApi\Error> |
40
|
|
|
*/ |
41
|
|
|
protected array $errors; |
42
|
|
|
|
43
|
|
|
protected int $statusCode; |
44
|
|
|
|
45
|
|
|
public function __construct() |
46
|
|
|
{ |
47
|
|
|
$this->meta = []; |
48
|
|
|
$this->jsonapi = ['version' => self::VERSION]; |
49
|
|
|
$this->data = []; |
50
|
|
|
$this->errors = []; |
51
|
|
|
$this->statusCode = 200; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getStatusCode(): int |
55
|
|
|
{ |
56
|
|
|
return $this->statusCode; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setData(ResourceObjectInterface $resourceObject): bool |
60
|
|
|
{ |
61
|
|
|
$this->data[] = $resourceObject; |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function setError(Error $error): bool |
66
|
|
|
{ |
67
|
|
|
$this->errors[] = $error; |
68
|
|
|
$this->statusCode = $error->getStatus(); // set status code of last error. |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function setStatusCode(int $statusCode): bool |
73
|
|
|
{ |
74
|
|
|
$this->statusCode = $statusCode; |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array<string,mixed> |
80
|
|
|
*/ |
81
|
|
|
public function toArray(): array |
82
|
|
|
{ |
83
|
|
|
$array = [ |
84
|
|
|
'jsonapi' => $this->jsonapi, |
85
|
|
|
]; |
86
|
|
|
if ($this->meta) { |
|
|
|
|
87
|
|
|
$array['meta'] = $this->meta; |
88
|
|
|
} |
89
|
|
|
if (!empty($this->errors)) { |
90
|
|
|
foreach ($this->errors as $error) { |
91
|
|
|
$array['errors'][] = $error->toArray(); |
92
|
|
|
} |
93
|
|
|
} else { |
94
|
|
|
$dataItems = \count($this->data); |
95
|
|
|
if (1 < $dataItems) { // multiple items |
96
|
|
|
foreach ($this->data as $item) { |
97
|
|
|
$array['data'][] = $item->toArray(); |
98
|
|
|
} |
99
|
|
|
} else { |
100
|
|
|
$array['data'] = \array_key_exists(0, $this->data) |
101
|
|
|
? $this->data[0]->toArray() // one item |
102
|
|
|
: []; // no data |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
return $array; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function toJson(): string |
109
|
|
|
{ |
110
|
|
|
$array = $this->toArray(); |
111
|
|
|
return (string) \json_encode($array); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.