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 | * @param string $type |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | 73 | protected function hasProviderMapping(string $type): bool |
|
195 | |||
196 | /** |
||
197 | * @param string $type |
||
198 | * |
||
199 | * @return mixed |
||
200 | */ |
||
201 | 62 | protected function getProviderMapping(string $type) |
|
205 | |||
206 | /** |
||
207 | * @param string $type |
||
208 | * @param string|Closure $schema |
||
209 | * |
||
210 | * @return void |
||
211 | */ |
||
212 | 71 | protected function setProviderMapping(string $type, $schema): void |
|
216 | |||
217 | /** |
||
218 | * @param string $type |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | 63 | protected function hasCreatedProvider(string $type): bool |
|
226 | |||
227 | /** |
||
228 | * @param string $type |
||
229 | * |
||
230 | * @return SchemaInterface |
||
231 | */ |
||
232 | 31 | protected function getCreatedProvider(string $type): SchemaInterface |
|
236 | |||
237 | /** |
||
238 | * @param string $type |
||
239 | * @param SchemaInterface $provider |
||
240 | * |
||
241 | * @return void |
||
242 | */ |
||
243 | 63 | protected function setCreatedProvider(string $type, SchemaInterface $provider): void |
|
247 | |||
248 | /** |
||
249 | * @param string $resourceType |
||
250 | * @param string $jsonType |
||
251 | * |
||
252 | * @return void |
||
253 | */ |
||
254 | 63 | protected function setResourceToJsonTypeMapping(string $resourceType, string $jsonType): void |
|
258 | |||
259 | /** |
||
260 | * @param object $resource |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | 67 | protected function getResourceType($resource): string |
|
273 | |||
274 | /** |
||
275 | * @param callable $callable |
||
276 | * |
||
277 | * @return SchemaInterface |
||
278 | */ |
||
279 | 37 | protected function createSchemaFromCallable(callable $callable): SchemaInterface |
|
285 | |||
286 | /** |
||
287 | * @param string $className |
||
288 | * |
||
289 | * @return SchemaInterface |
||
290 | */ |
||
291 | 34 | protected function createSchemaFromClassName(string $className): SchemaInterface |
|
297 | } |
||
298 |