1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WebServCo\Api\JsonApi; |
6
|
|
|
|
7
|
|
|
abstract class AbstractResourceObject implements |
8
|
|
|
\WebServCo\Api\JsonApi\Interfaces\ResourceObjectInterface, |
9
|
|
|
\WebServCo\Framework\Interfaces\JsonInterface |
10
|
|
|
{ |
11
|
|
|
protected string $id; |
12
|
|
|
|
13
|
|
|
protected string $type; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Attributes. |
17
|
|
|
* |
18
|
|
|
* @var array<string,array<string,int|string>|string> |
19
|
|
|
*/ |
20
|
|
|
protected array $attributes; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Links. |
24
|
|
|
* |
25
|
|
|
* @var array<string,string> |
26
|
|
|
*/ |
27
|
|
|
protected array $links; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Meta. |
31
|
|
|
* |
32
|
|
|
* @var array<string,mixed> |
33
|
|
|
*/ |
34
|
|
|
protected array $meta; |
35
|
|
|
|
36
|
|
|
public function __construct(string $type) |
37
|
|
|
{ |
38
|
|
|
$this->id = ''; // id must be string, and can be ommited (for example when creating a new resource) |
39
|
|
|
$this->type = $type; |
40
|
|
|
$this->attributes = []; |
41
|
|
|
$this->links = []; |
42
|
|
|
$this->meta = []; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
public function getAttribute(string $key) |
49
|
|
|
{ |
50
|
|
|
if (!\array_key_exists($key, $this->attributes)) { |
51
|
|
|
throw new \InvalidArgumentException(\sprintf('Attribute not found: %s', $key)); |
52
|
|
|
} |
53
|
|
|
return $this->attributes[$key]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getId(): string |
57
|
|
|
{ |
58
|
|
|
return $this->id; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return int|string|null |
63
|
|
|
*/ |
64
|
|
|
public function getMeta(string $key) |
65
|
|
|
{ |
66
|
|
|
if (!\array_key_exists($key, $this->meta)) { |
67
|
|
|
throw new \InvalidArgumentException(\sprintf('Meta not found: %s', $key)); |
68
|
|
|
} |
69
|
|
|
return $this->meta[$key]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param mixed $value |
74
|
|
|
*/ |
75
|
|
|
public function setAttribute(string $key, $value): bool |
76
|
|
|
{ |
77
|
|
|
$this->attributes[$key] = $value; |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setId(string $id): bool |
82
|
|
|
{ |
83
|
|
|
$this->id = $id; |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function setLink(string $key, string $value): bool |
88
|
|
|
{ |
89
|
|
|
$this->links[$key] = $value; |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed $value |
95
|
|
|
*/ |
96
|
|
|
public function setMeta(string $key, $value): bool |
97
|
|
|
{ |
98
|
|
|
$this->meta[$key] = $value; |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array<string,mixed> |
104
|
|
|
*/ |
105
|
|
|
public function toArray(): array |
106
|
|
|
{ |
107
|
|
|
// phpcs:ignore SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder |
108
|
|
|
$array = [ |
109
|
|
|
'type' => $this->type, |
110
|
|
|
'id' => $this->id, |
111
|
|
|
]; |
112
|
|
|
if ($this->attributes) { |
|
|
|
|
113
|
|
|
$array['attributes'] = $this->attributes; |
114
|
|
|
} |
115
|
|
|
if (!empty($this->links)) { |
116
|
|
|
$array['links'] = $this->links; |
117
|
|
|
} |
118
|
|
|
if (!empty($this->meta)) { |
119
|
|
|
$array['meta'] = $this->meta; |
120
|
|
|
} |
121
|
|
|
return $array; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function toJson(int $flags = 0): string |
125
|
|
|
{ |
126
|
|
|
$array = $this->toArray(); |
127
|
|
|
return (string) \json_encode($array, $flags); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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.