Completed
Push — master ( 714b6a...f9d007 )
by Narcotic
06:13
created

SchemaModel::getGroupsOfField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
0 ignored issues
show
Documentation introduced by
Should the type for parameter $container not be null|ContainerInterface?

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.

Loading history...
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
     * 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()
129
    {
130
        return $this->schema;
131
    }
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)
141
    {
142
        return $this->getSchemaField($field, 'title');
143
    }
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)
153
    {
154
        return $this->getSchemaField($field, 'description');
155
    }
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)
165
    {
166
        return $this->getSchemaField($field, 'x-groups', []);
167
    }
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()
195
    {
196
        return $this->schema->required;
197
    }
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)
207
    {
208
        return $this->getSchemaField($field, 'collection', array());
209
    }
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)
219
    {
220
        return $this->getSchemaField($field, 'readOnly', false);
221
    }
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)
231
    {
232
        return $this->getSchemaField($field, 'recordOriginException', false);
233
    }
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)
243
    {
244
        return (int) $this->getSchemaField($field, 'searchable', 0);
245
    }
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)
255
    {
256
        return $this->getSchemaField($field, 'x-dynamic-key', false) !== false;
257
    }
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)
267
    {
268
        return $this->getSchemaField($field, 'x-dynamic-key');
269
    }
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)
293
    {
294
        return $this->getSchemaField($field, 'x-constraints', false);
295
    }
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()
336
    {
337
        return $this->schema->searchable;
338
    }
339
}
340