1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Model based on Graviton\RestBundle\Model\DocumentModel. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\SchemaBundle\Model; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Model based on Graviton\RestBundle\Model\DocumentModel. |
13
|
|
|
* |
14
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
16
|
|
|
* @link http://swisscom.ch |
17
|
|
|
*/ |
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) |
70
|
|
|
{ |
71
|
4 |
|
$this->container = $container; |
72
|
|
|
|
73
|
4 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* get Title |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getTitle() |
82
|
|
|
{ |
83
|
|
|
return $this->schema->title; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* get description |
88
|
|
|
* |
89
|
|
|
* @return string Description |
90
|
|
|
*/ |
91
|
|
|
public function getDescription() |
92
|
|
|
{ |
93
|
|
|
return $this->schema->description; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* get recordOriginModifiable |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function getRecordOriginModifiable() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
if (isset($this->schema->recordOriginModifiable)) { |
104
|
|
|
return $this->schema->recordOriginModifiable; |
105
|
|
|
} |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns the bare schema |
111
|
|
|
* |
112
|
|
|
* @return \stdClass Schema |
113
|
|
|
*/ |
114
|
|
|
public function getSchema() |
115
|
|
|
{ |
116
|
|
|
return $this->schema; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* get title for a given field |
121
|
|
|
* |
122
|
|
|
* @param string $field field name |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getTitleOfField($field) |
127
|
|
|
{ |
128
|
|
|
return $this->getSchemaField($field, 'title'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* get description for a given field |
133
|
|
|
* |
134
|
|
|
* @param string $field field name |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getDescriptionOfField($field) |
139
|
|
|
{ |
140
|
|
|
return $this->getSchemaField($field, 'description'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* get property model for embedded field |
145
|
|
|
* |
146
|
|
|
* @param string $mapping name of mapping class |
147
|
|
|
* |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function manyPropertyModelForTarget($mapping) |
151
|
|
|
{ |
152
|
|
|
// @todo refactor to get rid of container dependency (maybe remove from here) |
153
|
|
|
list($app, $bundle, , $document) = explode('\\', $mapping); |
154
|
|
|
$app = strtolower($app); |
155
|
|
|
$bundle = strtolower(substr($bundle, 0, -6)); |
156
|
|
|
$document = strtolower($document); |
157
|
|
|
$propertyService = implode('.', array($app, $bundle, 'model', $document)); |
158
|
|
|
$propertyModel = $this->container->get($propertyService); |
159
|
|
|
|
160
|
|
|
return $propertyModel; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* get required fields for this object |
165
|
|
|
* |
166
|
|
|
* @return string[] |
167
|
|
|
*/ |
168
|
|
|
public function getRequiredFields() |
169
|
|
|
{ |
170
|
|
|
return $this->schema->required; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* get a collection of service names that can extref refer to |
175
|
|
|
* |
176
|
|
|
* @param string $field field name |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
public function getRefCollectionOfField($field) |
181
|
|
|
{ |
182
|
|
|
return $this->getSchemaField($field, 'collection', array()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* get readOnly flag for a given field |
187
|
|
|
* |
188
|
|
|
* @param string $field field name |
189
|
|
|
* |
190
|
|
|
* @return boolean the readOnly flag |
191
|
|
|
*/ |
192
|
|
|
public function getReadOnlyOfField($field) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
return $this->getSchemaField($field, 'readOnly', false); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* get readOnly flag for a given field |
199
|
|
|
* |
200
|
|
|
* @param string $field field name |
201
|
|
|
* |
202
|
|
|
* @return boolean the readOnly flag |
203
|
|
|
*/ |
204
|
|
|
public function getRecordOriginExceptionOfField($field) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
return $this->getSchemaField($field, 'recordOriginException', false); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* get searchable flag for a given field, weight based. |
211
|
|
|
* |
212
|
|
|
* @param string $field field name |
213
|
|
|
* |
214
|
|
|
* @return integer the searchable flag |
215
|
|
|
*/ |
216
|
|
|
public function getSearchableOfField($field) |
217
|
|
|
{ |
218
|
|
|
return (int) $this->getSchemaField($field, 'searchable', 0); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* tell us if a model what to be exposed using a key as field |
223
|
|
|
* |
224
|
|
|
* @param string $field field that we check for dynamic-key spec |
225
|
|
|
* |
226
|
|
|
* @return boolean |
227
|
|
|
*/ |
228
|
|
|
public function hasDynamicKey($field) |
229
|
|
|
{ |
230
|
|
|
return $this->getSchemaField($field, 'x-dynamic-key', false) !== false; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Get field used for setting stringy key value |
235
|
|
|
* |
236
|
|
|
* @param string $field field that we get dynamic-key spec from |
237
|
|
|
* |
238
|
|
|
* @return object |
239
|
|
|
*/ |
240
|
|
|
public function getDynamicKeySpec($field) |
241
|
|
|
{ |
242
|
|
|
return $this->getSchemaField($field, 'x-dynamic-key'); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Gets the defined document class in shortform from schema |
247
|
|
|
* |
248
|
|
|
* @return string|false either the document class or false it not given |
249
|
|
|
*/ |
250
|
|
|
public function getDocumentClass() |
251
|
|
|
{ |
252
|
|
|
$documentClass = false; |
253
|
|
|
if (isset($this->schema->{'x-documentClass'})) { |
254
|
|
|
$documentClass = $this->schema->{'x-documentClass'}; |
255
|
|
|
} |
256
|
|
|
return $documentClass; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get defined constraints on this field (if any) |
261
|
|
|
* |
262
|
|
|
* @param string $field field that we get constraints spec from |
263
|
|
|
* |
264
|
|
|
* @return object |
265
|
|
|
*/ |
266
|
|
|
public function getConstraints($field) |
267
|
|
|
{ |
268
|
|
|
return $this->getSchemaField($field, 'x-constraints', false); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Tells us if in this model, the ID can be given on a POST request or not (in the payload). |
273
|
|
|
* This basically depends on if the "id" property is given in the JSON definition or not. |
274
|
|
|
* |
275
|
|
|
* @return bool true if yes, false otherwise |
276
|
|
|
*/ |
277
|
|
|
public function isIdInPostAllowed() |
278
|
|
|
{ |
279
|
|
|
$isAllowed = true; |
280
|
|
|
if (isset($this->schema->{'x-id-in-post-allowed'})) { |
281
|
|
|
$isAllowed = $this->schema->{'x-id-in-post-allowed'}; |
282
|
|
|
} |
283
|
|
|
return $isAllowed; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* get schema field value |
288
|
|
|
* |
289
|
|
|
* @param string $field field name |
290
|
|
|
* @param string $property property name |
291
|
|
|
* @param mixed $fallbackValue fallback value if property isn't set |
292
|
|
|
* |
293
|
|
|
* @return mixed |
294
|
|
|
*/ |
295
|
|
|
private function getSchemaField($field, $property, $fallbackValue = '') |
296
|
|
|
{ |
297
|
|
|
if (isset($this->schema->properties->$field->$property)) { |
298
|
|
|
$fallbackValue = $this->schema->properties->$field->$property; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $fallbackValue; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* get searchable fields for this object |
306
|
|
|
* |
307
|
|
|
* @return string[] |
308
|
|
|
*/ |
309
|
|
|
public function getSearchableFields() |
310
|
|
|
{ |
311
|
|
|
return $this->schema->searchable; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
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.