1 | <?php declare(strict_types=1); |
||
31 | class SchemaContainer implements SchemaContainerInterface |
||
32 | { |
||
33 | /** |
||
34 | * Message code. |
||
35 | */ |
||
36 | const MSG_INVALID_MODEL_TYPE = 'Invalid model type.'; |
||
37 | |||
38 | /** |
||
39 | * Message code. |
||
40 | */ |
||
41 | const MSG_INVALID_SCHEME = 'Schema for type `%s` must be non-empty string, callable or SchemaInterface instance.'; |
||
42 | |||
43 | /** |
||
44 | * Message code. |
||
45 | */ |
||
46 | const MSG_TYPE_REUSE_FORBIDDEN = 'Type should not be used more than once to register a schema (`%s`).'; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | private $providerMapping = []; |
||
52 | |||
53 | /** |
||
54 | * @var SchemaInterface[] |
||
55 | */ |
||
56 | private $createdProviders = []; |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | private $resType2JsonType = []; |
||
62 | |||
63 | /** |
||
64 | * @var FactoryInterface |
||
65 | */ |
||
66 | private $factory; |
||
67 | |||
68 | /** |
||
69 | * @param FactoryInterface $factory |
||
70 | * @param iterable $schemas |
||
71 | */ |
||
72 | 90 | public function __construct(FactoryInterface $factory, iterable $schemas) |
|
77 | |||
78 | /** |
||
79 | * Register provider for resource type. |
||
80 | * |
||
81 | * @param string $type |
||
82 | * @param string|Closure $schema |
||
83 | * |
||
84 | * @return void |
||
85 | * |
||
86 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
87 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
88 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
89 | */ |
||
90 | 73 | public function register(string $type, $schema): void |
|
122 | |||
123 | /** |
||
124 | * Register providers for resource types. |
||
125 | * |
||
126 | * @param iterable $schemas |
||
127 | * |
||
128 | * @return void |
||
129 | */ |
||
130 | 90 | public function registerCollection(iterable $schemas): void |
|
136 | |||
137 | /** |
||
138 | * @inheritdoc |
||
139 | */ |
||
140 | 63 | public function getSchema($resource): SchemaInterface |
|
148 | |||
149 | /** |
||
150 | * @inheritdoc |
||
151 | */ |
||
152 | 72 | public function hasSchema($resourceObject): bool |
|
157 | |||
158 | /** |
||
159 | * @inheritdoc |
||
160 | * |
||
161 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
162 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
163 | */ |
||
164 | 63 | protected function getSchemaByType(string $type): SchemaInterface |
|
185 | |||
186 | /** |
||
187 | * @return FactoryInterface |
||
188 | */ |
||
189 | 62 | protected function getFactory(): FactoryInterface |
|
193 | |||
194 | /** |
||
195 | * @return array |
||
196 | */ |
||
197 | 73 | protected function getProviderMappings(): array |
|
201 | |||
202 | /** |
||
203 | * @param string $type |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | 73 | protected function hasProviderMapping(string $type): bool |
|
211 | |||
212 | /** |
||
213 | * @param string $type |
||
214 | * |
||
215 | * @return mixed |
||
216 | */ |
||
217 | 62 | protected function getProviderMapping(string $type) |
|
221 | |||
222 | /** |
||
223 | * @param string $type |
||
224 | * @param string|Closure $schema |
||
225 | * |
||
226 | * @return void |
||
227 | */ |
||
228 | 71 | protected function setProviderMapping(string $type, $schema): void |
|
232 | |||
233 | /** |
||
234 | * @param string $type |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | 63 | protected function hasCreatedProvider(string $type): bool |
|
242 | |||
243 | /** |
||
244 | * @param string $type |
||
245 | * |
||
246 | * @return SchemaInterface |
||
247 | */ |
||
248 | 31 | protected function getCreatedProvider(string $type): SchemaInterface |
|
252 | |||
253 | /** |
||
254 | * @param string $type |
||
255 | * @param SchemaInterface $provider |
||
256 | * |
||
257 | * @return void |
||
258 | */ |
||
259 | 63 | protected function setCreatedProvider(string $type, SchemaInterface $provider): void |
|
263 | |||
264 | /** |
||
265 | * @param string $resourceType |
||
266 | * @param string $jsonType |
||
267 | * |
||
268 | * @return void |
||
269 | */ |
||
270 | 63 | protected function setResourceToJsonTypeMapping(string $resourceType, string $jsonType): void |
|
274 | |||
275 | /** |
||
276 | * @param object $resource |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | 67 | protected function getResourceType($resource): string |
|
289 | |||
290 | /** |
||
291 | * @param callable $callable |
||
292 | * |
||
293 | * @return SchemaInterface |
||
294 | */ |
||
295 | 37 | protected function createSchemaFromCallable(callable $callable): SchemaInterface |
|
301 | |||
302 | /** |
||
303 | * @param string $className |
||
304 | * |
||
305 | * @return SchemaInterface |
||
306 | */ |
||
307 | 34 | protected function createSchemaFromClassName(string $className): SchemaInterface |
|
313 | } |
||
314 |