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
|
|
|
|
12
|
|
|
protected string $id; |
13
|
|
|
|
14
|
|
|
protected string $type; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Attributes. |
18
|
|
|
* |
19
|
|
|
* @var array<string,array<string,int|string>|string> |
20
|
|
|
*/ |
21
|
|
|
protected array $attributes; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Links. |
25
|
|
|
* |
26
|
|
|
* @var array<string,string> |
27
|
|
|
*/ |
28
|
|
|
protected array $links; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Meta. |
32
|
|
|
* |
33
|
|
|
* @var array<string,int|string> |
34
|
|
|
*/ |
35
|
|
|
protected array $meta; |
36
|
|
|
|
37
|
|
|
public function __construct(string $type) |
38
|
|
|
{ |
39
|
|
|
$this->id = ''; // id must be string, and can be ommited (for example when creating a new resource) |
40
|
|
|
$this->type = $type; |
41
|
|
|
$this->attributes = []; |
42
|
|
|
$this->links = []; |
43
|
|
|
$this->meta = []; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return array<string,int|string>|string |
48
|
|
|
*/ |
49
|
|
|
public function getAttribute(string $key) |
50
|
|
|
{ |
51
|
|
|
if (!\array_key_exists($key, $this->attributes)) { |
52
|
|
|
throw new \InvalidArgumentException(\sprintf('Attribute not found: %s', $key)); |
53
|
|
|
} |
54
|
|
|
return $this->attributes[$key]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getId(): string |
58
|
|
|
{ |
59
|
|
|
return $this->id; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return int|string |
64
|
|
|
*/ |
65
|
|
|
public function getMeta(string $key) |
66
|
|
|
{ |
67
|
|
|
if (!\array_key_exists($key, $this->meta)) { |
68
|
|
|
throw new \InvalidArgumentException(\sprintf('Meta not found: %s', $key)); |
69
|
|
|
} |
70
|
|
|
return $this->meta[$key]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array<string,int|string>|string $value |
75
|
|
|
*/ |
76
|
|
|
public function setAttribute(string $key, $value): bool |
77
|
|
|
{ |
78
|
|
|
$this->attributes[$key] = $value; |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setId(string $id): bool |
83
|
|
|
{ |
84
|
|
|
$this->id = $id; |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function setLink(string $key, string $value): bool |
89
|
|
|
{ |
90
|
|
|
$this->links[$key] = $value; |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param int|string $value |
96
|
|
|
*/ |
97
|
|
|
public function setMeta(string $key, $value): bool |
98
|
|
|
{ |
99
|
|
|
$this->meta[$key] = $value; |
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return array<string,mixed> |
105
|
|
|
*/ |
106
|
|
|
public function toArray(): array |
107
|
|
|
{ |
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(): string |
125
|
|
|
{ |
126
|
|
|
$array = $this->toArray(); |
127
|
|
|
return (string) \json_encode($array); |
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.