|
1
|
|
|
<?php namespace Neomerx\JsonApi\Schema; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2018 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Closure; |
|
20
|
|
|
use InvalidArgumentException; |
|
21
|
|
|
use Neomerx\JsonApi\Contracts\Schema\ContainerInterface; |
|
22
|
|
|
use Neomerx\JsonApi\Contracts\Schema\SchemaFactoryInterface; |
|
23
|
|
|
use Neomerx\JsonApi\Contracts\Schema\SchemaInterface; |
|
24
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
25
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
26
|
|
|
use function Neomerx\JsonApi\I18n\translate as _; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @package Neomerx\JsonApi |
|
30
|
|
|
*/ |
|
31
|
|
|
class Container implements ContainerInterface, LoggerAwareInterface |
|
32
|
|
|
{ |
|
33
|
|
|
use LoggerAwareTrait; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Message code. |
|
37
|
|
|
*/ |
|
38
|
|
|
const MSG_INVALID_TYPE = 0; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Message code. |
|
42
|
|
|
*/ |
|
43
|
|
|
const MSG_INVALID_SCHEME = self::MSG_INVALID_TYPE + 1; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Message code. |
|
47
|
|
|
*/ |
|
48
|
|
|
const MSG_TYPE_REUSE_FORBIDDEN = self::MSG_INVALID_SCHEME + 1; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Message code. |
|
52
|
|
|
*/ |
|
53
|
|
|
const MSG_UNREGISTERED_SCHEME_FOR_TYPE = self::MSG_TYPE_REUSE_FORBIDDEN + 1; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Message code. |
|
57
|
|
|
*/ |
|
58
|
|
|
const MSG_UNREGISTERED_SCHEME_FOR_RESOURCE_TYPE = self::MSG_UNREGISTERED_SCHEME_FOR_TYPE + 1; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Default messages. |
|
62
|
|
|
*/ |
|
63
|
|
|
const MESSAGES = [ |
|
64
|
|
|
self::MSG_INVALID_TYPE => 'Type must be non-empty string.', |
|
65
|
|
|
self::MSG_INVALID_SCHEME => |
|
66
|
|
|
'Schema for type \'%s\' must be non-empty string, callable or SchemaInterface instance.', |
|
67
|
|
|
self::MSG_TYPE_REUSE_FORBIDDEN => |
|
68
|
|
|
'Type should not be used more than once to register a schema (\'%s\').', |
|
69
|
|
|
self::MSG_UNREGISTERED_SCHEME_FOR_TYPE => 'Schema is not registered for type \'%s\'.', |
|
70
|
|
|
self::MSG_UNREGISTERED_SCHEME_FOR_RESOURCE_TYPE => 'Schema is not registered for resource type \'%s\'.', |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var array |
|
75
|
|
|
*/ |
|
76
|
|
|
private $providerMapping = []; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var SchemaInterface[] |
|
80
|
|
|
*/ |
|
81
|
|
|
private $createdProviders = []; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var array |
|
85
|
|
|
*/ |
|
86
|
|
|
private $resType2JsonType = []; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var SchemaFactoryInterface |
|
90
|
|
|
*/ |
|
91
|
|
|
private $factory; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @var array |
|
95
|
|
|
*/ |
|
96
|
|
|
private $messages; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param SchemaFactoryInterface $factory |
|
100
|
|
|
* @param iterable $schemas |
|
|
|
|
|
|
101
|
|
|
* @param array $messages |
|
102
|
|
|
*/ |
|
103
|
96 |
|
public function __construct(SchemaFactoryInterface $factory, iterable $schemas = [], $messages = self::MESSAGES) |
|
104
|
|
|
{ |
|
105
|
96 |
|
$this->factory = $factory; |
|
106
|
96 |
|
$this->messages = $messages; |
|
107
|
96 |
|
$this->registerArray($schemas); |
|
108
|
94 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Register provider for resource type. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $type |
|
114
|
|
|
* @param string|Closure $schema |
|
115
|
|
|
* |
|
116
|
|
|
* @return void |
|
117
|
|
|
* |
|
118
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
119
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
|
120
|
|
|
*/ |
|
121
|
85 |
|
public function register(string $type, $schema): void |
|
122
|
|
|
{ |
|
123
|
85 |
|
if (empty($type) === true || (class_exists($type) === false && interface_exists($type) === false)) { |
|
124
|
1 |
|
throw new InvalidArgumentException(_($this->messages[self::MSG_INVALID_TYPE])); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$isOk = ( |
|
128
|
84 |
|
(is_string($schema) === true && empty($schema) === false) || |
|
129
|
41 |
|
is_callable($schema) || |
|
130
|
84 |
|
$schema instanceof SchemaInterface |
|
131
|
|
|
); |
|
132
|
84 |
|
if ($isOk === false) { |
|
133
|
1 |
|
throw new InvalidArgumentException(_($this->messages[self::MSG_INVALID_SCHEME], $type)); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
83 |
|
if ($this->hasProviderMapping($type) === true) { |
|
137
|
1 |
|
throw new InvalidArgumentException(_($this->messages[self::MSG_TYPE_REUSE_FORBIDDEN], $type)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
83 |
|
if ($schema instanceof SchemaInterface) { |
|
141
|
4 |
|
$this->setProviderMapping($type, get_class($schema)); |
|
142
|
4 |
|
$this->setResourceToJsonTypeMapping($schema->getResourceType(), $type); |
|
143
|
4 |
|
$this->setCreatedProvider($type, $schema); |
|
144
|
|
|
} else { |
|
145
|
79 |
|
$this->setProviderMapping($type, $schema); |
|
146
|
|
|
} |
|
147
|
83 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Register providers for resource types. |
|
151
|
|
|
* |
|
152
|
|
|
* @param iterable $schemas |
|
153
|
|
|
* |
|
154
|
|
|
* @return void |
|
155
|
|
|
*/ |
|
156
|
96 |
|
public function registerArray(iterable $schemas): void |
|
157
|
|
|
{ |
|
158
|
96 |
|
foreach ($schemas as $type => $schema) { |
|
159
|
84 |
|
$this->register($type, $schema); |
|
160
|
|
|
} |
|
161
|
94 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @inheritdoc |
|
165
|
|
|
*/ |
|
166
|
74 |
|
public function getSchema($resource): ?SchemaInterface |
|
167
|
|
|
{ |
|
168
|
74 |
|
if ($resource === null) { |
|
169
|
1 |
|
return null; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
73 |
|
$resourceType = $this->getResourceType($resource); |
|
173
|
|
|
|
|
174
|
73 |
|
return $this->getSchemaByType($resourceType); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @inheritdoc |
|
179
|
|
|
*/ |
|
180
|
74 |
|
public function hasSchema($resourceObject): bool |
|
181
|
|
|
{ |
|
182
|
74 |
|
return is_object($resourceObject) === true && |
|
183
|
74 |
|
$this->hasProviderMapping($this->getResourceType($resourceObject)) === true; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @inheritdoc |
|
188
|
|
|
* |
|
189
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
190
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
|
191
|
|
|
*/ |
|
192
|
75 |
|
public function getSchemaByType(string $type): SchemaInterface |
|
193
|
|
|
{ |
|
194
|
75 |
|
if ($this->hasCreatedProvider($type) === true) { |
|
195
|
63 |
|
return $this->getCreatedProvider($type); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
71 |
|
if ($this->hasProviderMapping($type) === false) { |
|
199
|
3 |
|
throw new InvalidArgumentException(_($this->messages[self::MSG_UNREGISTERED_SCHEME_FOR_TYPE], $type)); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
70 |
|
$classNameOrCallable = $this->getProviderMapping($type); |
|
203
|
70 |
|
if (is_string($classNameOrCallable) === true) { |
|
204
|
48 |
|
$schema = $this->createSchemaFromClassName($classNameOrCallable); |
|
205
|
|
|
} else { |
|
206
|
34 |
|
assert(is_callable($classNameOrCallable) === true); |
|
207
|
34 |
|
$schema = $this->createSchemaFromCallable($classNameOrCallable); |
|
208
|
|
|
} |
|
209
|
70 |
|
$this->setCreatedProvider($type, $schema); |
|
210
|
|
|
|
|
211
|
|
|
/** @var SchemaInterface $schema */ |
|
212
|
|
|
|
|
213
|
70 |
|
$this->setResourceToJsonTypeMapping($schema->getResourceType(), $type); |
|
214
|
|
|
|
|
215
|
70 |
|
return $schema; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @inheritdoc |
|
220
|
|
|
* |
|
221
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
222
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
|
223
|
|
|
*/ |
|
224
|
48 |
|
public function getSchemaByResourceType(string $resourceType): SchemaInterface |
|
225
|
|
|
{ |
|
226
|
|
|
// Schema is not found among instantiated schemas for resource type $resourceType |
|
227
|
48 |
|
$isOk = (is_string($resourceType) === true && $this->hasResourceToJsonTypeMapping($resourceType) === true); |
|
228
|
|
|
|
|
229
|
|
|
// Schema might not be found if it hasn't been searched by type (not resource type) before. |
|
230
|
|
|
// We instantiate all schemas and then find one. |
|
231
|
48 |
|
if ($isOk === false) { |
|
232
|
1 |
|
foreach ($this->getProviderMappings() as $type => $schema) { |
|
233
|
1 |
|
if ($this->hasCreatedProvider($type) === false) { |
|
234
|
|
|
// it will instantiate the schema |
|
235
|
1 |
|
$this->getSchemaByType($type); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
// search one more time |
|
241
|
48 |
|
$isOk = (is_string($resourceType) === true && $this->hasResourceToJsonTypeMapping($resourceType) === true); |
|
242
|
|
|
|
|
243
|
48 |
|
if ($isOk === false) { |
|
244
|
1 |
|
throw new InvalidArgumentException(_( |
|
245
|
1 |
|
$this->messages[self::MSG_UNREGISTERED_SCHEME_FOR_RESOURCE_TYPE], |
|
246
|
1 |
|
$resourceType |
|
247
|
|
|
)); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
48 |
|
return $this->getSchemaByType($this->getJsonType($resourceType)); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @return SchemaFactoryInterface |
|
255
|
|
|
*/ |
|
256
|
70 |
|
protected function getFactory(): SchemaFactoryInterface |
|
257
|
|
|
{ |
|
258
|
70 |
|
return $this->factory; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @return array |
|
263
|
|
|
*/ |
|
264
|
75 |
|
protected function getProviderMappings(): array |
|
265
|
|
|
{ |
|
266
|
75 |
|
return $this->providerMapping; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @param string $type |
|
271
|
|
|
* |
|
272
|
|
|
* @return bool |
|
273
|
|
|
*/ |
|
274
|
83 |
|
protected function hasProviderMapping(string $type): bool |
|
275
|
|
|
{ |
|
276
|
83 |
|
return array_key_exists($type, $this->providerMapping); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* @param string $type |
|
281
|
|
|
* |
|
282
|
|
|
* @return mixed |
|
283
|
|
|
*/ |
|
284
|
70 |
|
protected function getProviderMapping(string $type) |
|
285
|
|
|
{ |
|
286
|
70 |
|
return $this->providerMapping[$type]; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* @param string $type |
|
291
|
|
|
* @param string|Closure $schema |
|
292
|
|
|
* |
|
293
|
|
|
* @return void |
|
294
|
|
|
*/ |
|
295
|
83 |
|
protected function setProviderMapping(string $type, $schema): void |
|
296
|
|
|
{ |
|
297
|
83 |
|
$this->providerMapping[$type] = $schema; |
|
298
|
83 |
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* @param string $type |
|
302
|
|
|
* |
|
303
|
|
|
* @return bool |
|
304
|
|
|
*/ |
|
305
|
75 |
|
protected function hasCreatedProvider(string $type): bool |
|
306
|
|
|
{ |
|
307
|
75 |
|
return array_key_exists($type, $this->createdProviders); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* @param string $type |
|
312
|
|
|
* |
|
313
|
|
|
* @return SchemaInterface |
|
314
|
|
|
*/ |
|
315
|
63 |
|
protected function getCreatedProvider(string $type): SchemaInterface |
|
316
|
|
|
{ |
|
317
|
63 |
|
return $this->createdProviders[$type]; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* @param string $type |
|
322
|
|
|
* @param SchemaInterface $provider |
|
323
|
|
|
* |
|
324
|
|
|
* @return void |
|
325
|
|
|
*/ |
|
326
|
74 |
|
protected function setCreatedProvider(string $type, SchemaInterface $provider): void |
|
327
|
|
|
{ |
|
328
|
74 |
|
$this->createdProviders[$type] = $provider; |
|
329
|
74 |
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* @param string $resourceType |
|
333
|
|
|
* |
|
334
|
|
|
* @return bool |
|
335
|
|
|
*/ |
|
336
|
48 |
|
protected function hasResourceToJsonTypeMapping(string $resourceType): bool |
|
337
|
|
|
{ |
|
338
|
48 |
|
return array_key_exists($resourceType, $this->resType2JsonType); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* @param string $resourceType |
|
343
|
|
|
* |
|
344
|
|
|
* @return string |
|
345
|
|
|
*/ |
|
346
|
48 |
|
protected function getJsonType(string $resourceType): string |
|
347
|
|
|
{ |
|
348
|
48 |
|
return $this->resType2JsonType[$resourceType]; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* @param string $resourceType |
|
353
|
|
|
* @param string $jsonType |
|
354
|
|
|
* |
|
355
|
|
|
* @return void |
|
356
|
|
|
*/ |
|
357
|
74 |
|
protected function setResourceToJsonTypeMapping(string $resourceType, string $jsonType): void |
|
358
|
|
|
{ |
|
359
|
74 |
|
$this->resType2JsonType[$resourceType] = $jsonType; |
|
360
|
74 |
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* @param object $resource |
|
364
|
|
|
* |
|
365
|
|
|
* @return string |
|
366
|
|
|
*/ |
|
367
|
74 |
|
protected function getResourceType($resource): string |
|
368
|
|
|
{ |
|
369
|
74 |
|
assert( |
|
370
|
74 |
|
is_object($resource) === true, |
|
371
|
74 |
|
'Unable to get a type of the resource as it is not an object.' |
|
372
|
|
|
); |
|
373
|
|
|
|
|
374
|
74 |
|
foreach (array_keys($this->getProviderMappings()) as $schema) { |
|
375
|
74 |
|
if (is_subclass_of($resource, $schema)) { |
|
|
|
|
|
|
376
|
74 |
|
return $schema; |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
72 |
|
return get_class($resource); |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
/** |
|
384
|
|
|
* @param callable $callable |
|
385
|
|
|
* |
|
386
|
|
|
* @return SchemaInterface |
|
387
|
|
|
*/ |
|
388
|
33 |
|
protected function createSchemaFromCallable(callable $callable): SchemaInterface |
|
389
|
|
|
{ |
|
390
|
33 |
|
$schema = call_user_func($callable, $this->getFactory()); |
|
391
|
|
|
|
|
392
|
33 |
|
return $schema; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* @param string $className |
|
397
|
|
|
* |
|
398
|
|
|
* @return SchemaInterface |
|
399
|
|
|
*/ |
|
400
|
47 |
|
protected function createSchemaFromClassName(string $className): SchemaInterface |
|
401
|
|
|
{ |
|
402
|
47 |
|
$schema = new $className($this->getFactory()); |
|
403
|
|
|
|
|
404
|
47 |
|
return $schema; |
|
405
|
|
|
} |
|
406
|
|
|
} |
|
407
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.