1 | <?php |
||
18 | class SchemaModel implements ContainerAwareInterface |
||
19 | { |
||
20 | /** |
||
21 | * object |
||
22 | */ |
||
23 | private $schema; |
||
24 | |||
25 | /** |
||
26 | * @var ContainerInterface |
||
27 | */ |
||
28 | private $container; |
||
29 | |||
30 | /** |
||
31 | * load some schema info for the model |
||
32 | */ |
||
33 | 4 | public function __construct() |
|
34 | { |
||
35 | 4 | list(, $bundle, , $model) = explode('\\', get_called_class()); |
|
36 | 4 | $file = __DIR__ . '/../../' . $bundle . '/Resources/config/schema/' . $model . '.json'; |
|
37 | |||
38 | 4 | if (!file_exists($file)) { |
|
39 | $reflection = new \ReflectionClass($this); |
||
40 | $file = dirname($reflection->getFileName()).'/../Resources/config/schema/' . $model . '.json'; |
||
41 | } |
||
42 | |||
43 | 4 | if (!file_exists($file)) { |
|
44 | // fallback try on model property (this should be available on some generated classes) |
||
45 | if (isset($this->_modelPath)) { |
||
46 | // try to find schema.json relative to the model involved.. |
||
47 | $file = dirname($this->_modelPath) . '/../Resources/config/schema/' . $model . '.json'; |
||
48 | } |
||
49 | |||
50 | if (!file_exists($file)) { |
||
51 | throw new \LogicException('Please create the schema file ' . $file); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | 4 | $this->schema = \json_decode(file_get_contents($file)); |
|
56 | |||
57 | 4 | if (is_null($this->schema)) { |
|
58 | throw new \LogicException('The file ' . $file . ' doe not contain valid json'); |
||
59 | } |
||
60 | 4 | } |
|
61 | |||
62 | /** |
||
63 | * inject container |
||
64 | * |
||
65 | * @param ContainerInterface $container service container |
||
|
|||
66 | * |
||
67 | * @return self |
||
68 | */ |
||
69 | 4 | public function setContainer(ContainerInterface $container = null) |
|
75 | |||
76 | /** |
||
77 | * get Title |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getTitle() |
||
85 | |||
86 | /** |
||
87 | * get description |
||
88 | * |
||
89 | * @return string Description |
||
90 | */ |
||
91 | public function getDescription() |
||
95 | |||
96 | /** |
||
97 | * get recordOriginModifiable |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function getRecordOriginModifiable() |
||
108 | |||
109 | /** |
||
110 | * get isVersioning |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function isVersioning() |
||
115 | { |
||
116 | $isVersioning = false; |
||
117 | if (isset($this->schema->{'x-versioning'})) { |
||
118 | $isVersioning = $this->schema->{'x-versioning'}; |
||
119 | } |
||
120 | return $isVersioning; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Returns the bare schema |
||
125 | * |
||
126 | * @return \stdClass Schema |
||
127 | */ |
||
128 | public function getSchema() |
||
132 | |||
133 | /** |
||
134 | * get title for a given field |
||
135 | * |
||
136 | * @param string $field field name |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getTitleOfField($field) |
||
144 | |||
145 | /** |
||
146 | * get description for a given field |
||
147 | * |
||
148 | * @param string $field field name |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getDescriptionOfField($field) |
||
156 | |||
157 | /** |
||
158 | * get groups for a given field |
||
159 | * |
||
160 | * @param string $field field name |
||
161 | * |
||
162 | * @return array<string> group names |
||
163 | */ |
||
164 | public function getGroupsOfField($field) |
||
168 | |||
169 | /** |
||
170 | * get property model for embedded field |
||
171 | * |
||
172 | * @param string $mapping name of mapping class |
||
173 | * |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function manyPropertyModelForTarget($mapping) |
||
177 | { |
||
178 | // @todo refactor to get rid of container dependency (maybe remove from here) |
||
179 | list($app, $bundle, , $document) = explode('\\', $mapping); |
||
180 | $app = strtolower($app); |
||
181 | $bundle = strtolower(substr($bundle, 0, -6)); |
||
182 | $document = strtolower($document); |
||
183 | $propertyService = implode('.', array($app, $bundle, 'model', $document)); |
||
184 | $propertyModel = $this->container->get($propertyService); |
||
185 | |||
186 | return $propertyModel; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * get required fields for this object |
||
191 | * |
||
192 | * @return string[] |
||
193 | */ |
||
194 | public function getRequiredFields() |
||
198 | |||
199 | /** |
||
200 | * get a collection of service names that can extref refer to |
||
201 | * |
||
202 | * @param string $field field name |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | public function getRefCollectionOfField($field) |
||
210 | |||
211 | /** |
||
212 | * get readOnly flag for a given field |
||
213 | * |
||
214 | * @param string $field field name |
||
215 | * |
||
216 | * @return boolean the readOnly flag |
||
217 | */ |
||
218 | public function getReadOnlyOfField($field) |
||
222 | |||
223 | /** |
||
224 | * get readOnly flag for a given field |
||
225 | * |
||
226 | * @param string $field field name |
||
227 | * |
||
228 | * @return boolean the readOnly flag |
||
229 | */ |
||
230 | public function getRecordOriginExceptionOfField($field) |
||
234 | |||
235 | /** |
||
236 | * get searchable flag for a given field, weight based. |
||
237 | * |
||
238 | * @param string $field field name |
||
239 | * |
||
240 | * @return integer the searchable flag |
||
241 | */ |
||
242 | public function getSearchableOfField($field) |
||
246 | |||
247 | /** |
||
248 | * tell us if a model what to be exposed using a key as field |
||
249 | * |
||
250 | * @param string $field field that we check for dynamic-key spec |
||
251 | * |
||
252 | * @return boolean |
||
253 | */ |
||
254 | public function hasDynamicKey($field) |
||
258 | |||
259 | /** |
||
260 | * Get field used for setting stringy key value |
||
261 | * |
||
262 | * @param string $field field that we get dynamic-key spec from |
||
263 | * |
||
264 | * @return object |
||
265 | */ |
||
266 | public function getDynamicKeySpec($field) |
||
270 | |||
271 | /** |
||
272 | * Gets the defined document class in shortform from schema |
||
273 | * |
||
274 | * @return string|false either the document class or false it not given |
||
275 | */ |
||
276 | public function getDocumentClass() |
||
277 | { |
||
278 | $documentClass = false; |
||
279 | if (isset($this->schema->{'x-documentClass'})) { |
||
280 | $documentClass = $this->schema->{'x-documentClass'}; |
||
281 | } |
||
282 | return $documentClass; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Get defined constraints on this field (if any) |
||
287 | * |
||
288 | * @param string $field field that we get constraints spec from |
||
289 | * |
||
290 | * @return object |
||
291 | */ |
||
292 | public function getConstraints($field) |
||
296 | |||
297 | /** |
||
298 | * Tells us if in this model, the ID can be given on a POST request or not (in the payload). |
||
299 | * This basically depends on if the "id" property is given in the JSON definition or not. |
||
300 | * |
||
301 | * @return bool true if yes, false otherwise |
||
302 | */ |
||
303 | public function isIdInPostAllowed() |
||
304 | { |
||
305 | $isAllowed = true; |
||
306 | if (isset($this->schema->{'x-id-in-post-allowed'})) { |
||
307 | $isAllowed = $this->schema->{'x-id-in-post-allowed'}; |
||
308 | } |
||
309 | return $isAllowed; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * get schema field value |
||
314 | * |
||
315 | * @param string $field field name |
||
316 | * @param string $property property name |
||
317 | * @param mixed $fallbackValue fallback value if property isn't set |
||
318 | * |
||
319 | * @return mixed |
||
320 | */ |
||
321 | private function getSchemaField($field, $property, $fallbackValue = '') |
||
322 | { |
||
323 | if (isset($this->schema->properties->$field->$property)) { |
||
324 | $fallbackValue = $this->schema->properties->$field->$property; |
||
325 | } |
||
326 | |||
327 | return $fallbackValue; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * get searchable fields for this object |
||
332 | * |
||
333 | * @return string[] |
||
334 | */ |
||
335 | public function getSearchableFields() |
||
339 | } |
||
340 |
This check looks for
@param
annotations 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.
Most often this is a case of a parameter that can be null in addition to its declared types.