1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiStructure\Concern; |
6
|
|
|
|
7
|
|
|
use VGirol\JsonApiConstant\Members; |
8
|
|
|
use VGirol\JsonApiStructure\Messages; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Assertions relating to the object's members |
12
|
|
|
*/ |
13
|
|
|
trait ValidateMembers |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Asserts that a json object has an expected member. |
17
|
|
|
* |
18
|
|
|
* @param string $expected |
19
|
|
|
* @param array $json |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
23
|
|
|
*/ |
24
|
69 |
|
public function hasMember($expected, $json): void |
25
|
|
|
{ |
26
|
69 |
|
$this->isValidArgument(1, 'string', $expected); |
|
|
|
|
27
|
66 |
|
$this->isValidArgument(2, 'array', $json); |
28
|
|
|
|
29
|
60 |
|
if (!\array_key_exists($expected, $json)) { |
30
|
30 |
|
$this->throw(sprintf(Messages::HAS_MEMBER, $expected), 403); |
|
|
|
|
31
|
|
|
} |
32
|
30 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Asserts that a json object has expected members. |
36
|
|
|
* |
37
|
|
|
* @param array<string> $expected |
38
|
|
|
* @param array $json |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
42
|
|
|
*/ |
43
|
12 |
|
public function hasMembers($expected, $json): void |
44
|
|
|
{ |
45
|
12 |
|
$this->isValidArgument(1, 'array', $expected); |
46
|
|
|
|
47
|
9 |
|
foreach ($expected as $key) { |
48
|
9 |
|
$this->hasMember($key, $json); |
49
|
|
|
} |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Asserts that a json object has only expected members. |
54
|
|
|
* |
55
|
|
|
* @param array<string> $expected |
56
|
|
|
* @param array $json |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
60
|
|
|
*/ |
61
|
12 |
|
public function hasOnlyMembers($expected, $json): void |
62
|
|
|
{ |
63
|
12 |
|
$this->isValidArgument(1, 'array', $expected); |
64
|
9 |
|
$this->isValidArgument(2, 'array', $json); |
65
|
|
|
|
66
|
6 |
|
if (\array_keys($json) != $expected) { |
67
|
3 |
|
$this->throw(sprintf(Messages::HAS_ONLY_MEMBERS, implode(', ', $expected)), 403); |
68
|
|
|
} |
69
|
3 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Asserts that a json object not has an unexpected member. |
73
|
|
|
* |
74
|
|
|
* @param string $expected |
75
|
|
|
* @param array $json |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
79
|
|
|
*/ |
80
|
21 |
|
public function notHasMember($expected, $json): void |
81
|
|
|
{ |
82
|
21 |
|
$this->isValidArgument(1, 'string', $expected); |
83
|
18 |
|
$this->isValidArgument(2, 'array', $json); |
84
|
|
|
|
85
|
12 |
|
if (\array_key_exists($expected, $json)) { |
86
|
6 |
|
$this->throw(sprintf(Messages::NOT_HAS_MEMBER, $expected), 403); |
87
|
|
|
} |
88
|
6 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Asserts that a json object not has unexpected members. |
92
|
|
|
* |
93
|
|
|
* @param array<string> $expected |
94
|
|
|
* @param array $json |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
98
|
|
|
*/ |
99
|
12 |
|
public function notHasMembers($expected, $json): void |
100
|
|
|
{ |
101
|
12 |
|
$this->isValidArgument(1, 'array', $expected); |
102
|
|
|
|
103
|
9 |
|
foreach ($expected as $key) { |
104
|
9 |
|
$this->notHasMember($key, $json); |
105
|
|
|
} |
106
|
3 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Asserts that a json object has a "data" member. |
110
|
|
|
* |
111
|
|
|
* @see hasMember |
112
|
|
|
* |
113
|
|
|
* @param array $json |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
117
|
|
|
*/ |
118
|
6 |
|
public function hasData($json): void |
119
|
|
|
{ |
120
|
6 |
|
$this->hasMember(Members::DATA, $json); |
121
|
3 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Asserts that a json object has an "attributes" member. |
125
|
|
|
* |
126
|
|
|
* @see hasMember |
127
|
|
|
* |
128
|
|
|
* @param array $json |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
132
|
|
|
*/ |
133
|
6 |
|
public function hasAttributes($json): void |
134
|
|
|
{ |
135
|
6 |
|
$this->hasMember(Members::ATTRIBUTES, $json); |
136
|
3 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Asserts that a json object has a "links" member. |
140
|
|
|
* |
141
|
|
|
* @see hasMember |
142
|
|
|
* |
143
|
|
|
* @param array $json |
144
|
|
|
* |
145
|
|
|
* @return void |
146
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
147
|
|
|
*/ |
148
|
6 |
|
public function hasLinks($json): void |
149
|
|
|
{ |
150
|
6 |
|
$this->hasMember(Members::LINKS, $json); |
151
|
3 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Asserts that a json object has a "meta" member. |
155
|
|
|
* |
156
|
|
|
* @see hasMember |
157
|
|
|
* |
158
|
|
|
* @param array $json |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
162
|
|
|
*/ |
163
|
6 |
|
public function hasMeta($json): void |
164
|
|
|
{ |
165
|
6 |
|
$this->hasMember(Members::META, $json); |
166
|
3 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Asserts that a json object has an "included" member. |
170
|
|
|
* |
171
|
|
|
* @see hasMember |
172
|
|
|
* |
173
|
|
|
* @param array $json |
174
|
|
|
* |
175
|
|
|
* @return void |
176
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
177
|
|
|
*/ |
178
|
6 |
|
public function hasIncluded($json): void |
179
|
|
|
{ |
180
|
6 |
|
$this->hasMember(Members::INCLUDED, $json); |
181
|
3 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Asserts that a json object has a "relationships" member. |
185
|
|
|
* |
186
|
|
|
* @see hasMember |
187
|
|
|
* |
188
|
|
|
* @param array $json |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
192
|
|
|
*/ |
193
|
6 |
|
public function hasRelationships($json): void |
194
|
|
|
{ |
195
|
6 |
|
$this->hasMember(Members::RELATIONSHIPS, $json); |
196
|
3 |
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Asserts that a json object has an "errors" member. |
200
|
|
|
* |
201
|
|
|
* @see hasMember |
202
|
|
|
* |
203
|
|
|
* @param array $json |
204
|
|
|
* |
205
|
|
|
* @return void |
206
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
207
|
|
|
*/ |
208
|
6 |
|
public function hasErrors($json): void |
209
|
|
|
{ |
210
|
6 |
|
$this->hasMember(Members::ERRORS, $json); |
211
|
3 |
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Asserts that a json object has a "jsonapi" member. |
215
|
|
|
* |
216
|
|
|
* @see hasMember |
217
|
|
|
* |
218
|
|
|
* @param array $json |
219
|
|
|
* |
220
|
|
|
* @return void |
221
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
222
|
|
|
*/ |
223
|
6 |
|
public function hasJsonapi($json): void |
224
|
|
|
{ |
225
|
6 |
|
$this->hasMember(Members::JSONAPI, $json); |
226
|
3 |
|
} |
227
|
|
|
} |
228
|
|
|
|