1 | <?php namespace Neomerx\JsonApi\Schema; |
||
32 | class Container implements ContainerInterface, LoggerAwareInterface |
||
33 | { |
||
34 | use LoggerAwareTrait; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $providerMapping = []; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $createdProviders = []; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $resourceType2Type = []; |
||
50 | |||
51 | /** |
||
52 | * @var SchemaFactoryInterface |
||
53 | */ |
||
54 | private $factory; |
||
55 | |||
56 | /** |
||
57 | * @param SchemaFactoryInterface $factory |
||
58 | * @param array $schemas |
||
59 | */ |
||
60 | 75 | public function __construct(SchemaFactoryInterface $factory, array $schemas = []) |
|
65 | |||
66 | /** |
||
67 | * Register provider for resource type. |
||
68 | * |
||
69 | * @param string $type |
||
70 | * @param string|Closure $schema |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | 68 | public function register($type, $schema) |
|
75 | { |
||
76 | // Type must be non-empty string |
||
77 | 68 | $isOk = (is_string($type) === true && empty($type) === false); |
|
78 | 68 | if ($isOk === false) { |
|
79 | 1 | throw new InvalidArgumentException(T::t('Type must be non-empty string.')); |
|
80 | } |
||
81 | |||
82 | 67 | $isOk = ((is_string($schema) === true && empty($schema) === false) || $schema instanceof Closure); |
|
83 | 67 | if ($isOk === false) { |
|
84 | 1 | throw new InvalidArgumentException(T::t( |
|
85 | 1 | 'Schema for type \'%s\' must be non-empty string or Closure.', |
|
86 | 1 | [$type] |
|
87 | 1 | )); |
|
88 | } |
||
89 | |||
90 | 66 | if (isset($this->providerMapping[$type]) === true) { |
|
91 | 1 | throw new InvalidArgumentException(T::t( |
|
92 | 1 | 'Type should not be used more than once to register a schema (\'%s\').', |
|
93 | 1 | [$type] |
|
94 | 2 | )); |
|
95 | } |
||
96 | |||
97 | 66 | $this->providerMapping[$type] = $schema; |
|
98 | 66 | } |
|
99 | |||
100 | /** |
||
101 | * Register providers for resource types. |
||
102 | * |
||
103 | * @param array $schemas |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | 75 | public function registerArray(array $schemas) |
|
113 | |||
114 | /** |
||
115 | * @inheritdoc |
||
116 | */ |
||
117 | 57 | public function getSchema($resource) |
|
123 | |||
124 | /** |
||
125 | * @inheritdoc |
||
126 | */ |
||
127 | 59 | public function getSchemaByType($type) |
|
128 | { |
||
129 | 59 | is_string($type) === true ?: Exceptions::throwInvalidArgument('type', $type); |
|
130 | |||
131 | 59 | if (isset($this->createdProviders[$type])) { |
|
132 | 51 | return $this->createdProviders[$type]; |
|
133 | } |
||
134 | |||
135 | 59 | if (isset($this->providerMapping[$type]) === false) { |
|
136 | 2 | throw new InvalidArgumentException(T::t('Schema is not registered for type \'%s\'.', [$type])); |
|
137 | } |
||
138 | |||
139 | 59 | $classNameOrClosure = $this->providerMapping[$type]; |
|
140 | 59 | if ($classNameOrClosure instanceof Closure) { |
|
141 | 27 | $this->createdProviders[$type] = ($schema = $this->createSchemaFromClosure($classNameOrClosure)); |
|
142 | 27 | } else { |
|
143 | 42 | $this->createdProviders[$type] = ($schema = $this->createSchemaFromClassName($classNameOrClosure)); |
|
144 | } |
||
145 | |||
146 | /** @var SchemaProviderInterface $schema */ |
||
147 | |||
148 | 59 | $this->resourceType2Type[$schema->getResourceType()] = $type; |
|
149 | |||
150 | 59 | return $schema; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * @inheritdoc |
||
155 | */ |
||
156 | 42 | public function getSchemaByResourceType($resourceType) |
|
184 | |||
185 | /** |
||
186 | * @return SchemaFactoryInterface |
||
187 | */ |
||
188 | 59 | protected function getFactory() |
|
192 | |||
193 | /** |
||
194 | * @param object $resource |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | 57 | protected function getResourceType($resource) |
|
202 | |||
203 | /** |
||
204 | * @param Closure $closure |
||
205 | * |
||
206 | * @return SchemaProviderInterface |
||
207 | */ |
||
208 | 26 | protected function createSchemaFromClosure(Closure $closure) |
|
214 | |||
215 | /** |
||
216 | * @param string $className |
||
217 | * |
||
218 | * @return SchemaProviderInterface |
||
219 | */ |
||
220 | 41 | protected function createSchemaFromClassName($className) |
|
226 | } |
||
227 |