Completed
Push — feature/EVO-6307-record-origin... ( 2c1fcb )
by Narcotic
61:45
created

SchemaModel::getRecordOriginModifiable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 7
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 4
nc 2
nop 0
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
    public function __construct()
34
    {
35
        list(, $bundle, , $model) = explode('\\', get_called_class());
36
        $file = __DIR__ . '/../../' . $bundle . '/Resources/config/schema/' . $model . '.json';
37
38
        if (!file_exists($file)) {
39
            // fallback try on model property (this should be available on some generated classes)
40
            if (isset($this->_modelPath)) {
41
                // try to find schema.json relative to the model involved..
42
                $file = dirname($this->_modelPath) . '/../Resources/config/schema/' . $model . '.json';
43
            }
44
45
            if (!file_exists($file)) {
46
                throw new \LogicException('Please create the schema file ' . $file);
47
            }
48
        }
49
50
        $this->schema = \json_decode(file_get_contents($file));
51
52
        if (is_null($this->schema)) {
53
            throw new \LogicException('The file ' . $file . ' doe not contain valid json');
54
        }
55
    }
56
57
    /**
58
     * inject container
59
     *
60
     * @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...
61
     *
62
     * @return self
63
     */
64
    public function setContainer(ContainerInterface $container = null)
65
    {
66
        $this->container = $container;
67
68
        return $this;
69
    }
70
71
    /**
72
     * get Title
73
     *
74
     * @return string
75
     */
76
    public function getTitle()
77
    {
78
        return $this->schema->title;
79
    }
80
81
    /**
82
     * get description
83
     *
84
     * @return string Description
85
     */
86
    public function getDescription()
87
    {
88
        return $this->schema->description;
89
    }
90
91
    /**
92
     * get recordOriginModifiable
93
     *
94
     * @return bool
95
     */
96
    public function getRecordOriginModifiable()
0 ignored issues
show
Coding Style introduced by
function getRecordOriginModifiable() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
97
    {
98
        if (isset($this->schema->recordOriginModifiable)) {
99
            return $this->schema->recordOriginModifiable;
100
        }
101
        return true;
102
    }
103
104
    /**
105
     * Returns the bare schema
106
     *
107
     * @return \stdClass Schema
108
     */
109
    public function getSchema()
110
    {
111
        return $this->schema;
112
    }
113
114
    /**
115
     * get title for a given field
116
     *
117
     * @param string $field field name
118
     *
119
     * @return string
120
     */
121
    public function getTitleOfField($field)
122
    {
123
        return $this->getSchemaField($field, 'title');
124
    }
125
126
    /**
127
     * get description for a given field
128
     *
129
     * @param string $field field name
130
     *
131
     * @return string
132
     */
133
    public function getDescriptionOfField($field)
134
    {
135
        return $this->getSchemaField($field, 'description');
136
    }
137
138
    /**
139
     * get property model for embedded field
140
     *
141
     * @param string $mapping name of mapping class
142
     *
143
     * @return $this
144
     */
145
    public function manyPropertyModelForTarget($mapping)
146
    {
147
        // @todo refactor to get rid of container dependency (maybe remove from here)
148
        list($app, $bundle, , $document) = explode('\\', $mapping);
149
        $app = strtolower($app);
150
        $bundle = strtolower(substr($bundle, 0, -6));
151
        $document = strtolower($document);
152
        $propertyService = implode('.', array($app, $bundle, 'model', $document));
153
        $propertyModel = $this->container->get($propertyService);
154
155
        return $propertyModel;
156
    }
157
158
    /**
159
     * get required fields for this object
160
     *
161
     * @return string[]
162
     */
163
    public function getRequiredFields()
164
    {
165
        return $this->schema->required;
166
    }
167
168
    /**
169
     * get a collection of service names that can extref refer to
170
     *
171
     * @param string $field field name
172
     *
173
     * @return array
174
     */
175
    public function getRefCollectionOfField($field)
176
    {
177
        return $this->getSchemaField($field, 'collection', array());
178
    }
179
180
    /**
181
     * get readOnly flag for a given field
182
     *
183
     * @param string $field field name
184
     *
185
     * @return boolean the readOnly flag
186
     */
187
    public function getReadOnlyOfField($field)
0 ignored issues
show
Coding Style introduced by
function getReadOnlyOfField() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
188
    {
189
        return $this->getSchemaField($field, 'readOnly', false);
190
    }
191
192
    /**
193
     * get readOnly flag for a given field
194
     *
195
     * @param string $field field name
196
     *
197
     * @return boolean the readOnly flag
198
     */
199
    public function getRecordOriginExceptionOfField($field)
0 ignored issues
show
Coding Style introduced by
function getRecordOriginExceptionOfField() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
200
    {
201
        return $this->getSchemaField($field, 'recordOriginException', false);
202
    }
203
204
    /**
205
     * get searchable flag for a given field, weight based.
206
     *
207
     * @param string $field field name
208
     *
209
     * @return integer the searchable flag
210
     */
211
    public function getSearchableOfField($field)
212
    {
213
        return (int) $this->getSchemaField($field, 'searchable', 0);
214
    }
215
216
    /**
217
     * tell us if a model what to be exposed using a key as field
218
     *
219
     * @param string $field field that we check for dynamic-key spec
220
     *
221
     * @return boolean
222
     */
223
    public function hasDynamicKey($field)
224
    {
225
        return $this->getSchemaField($field, 'x-dynamic-key', false) !== false;
226
    }
227
228
    /**
229
     * Get field used for setting stringy key value
230
     *
231
     * @param string $field field that we get dynamic-key spec from
232
     *
233
     * @return object
234
     */
235
    public function getDynamicKeySpec($field)
236
    {
237
        return $this->getSchemaField($field, 'x-dynamic-key');
238
    }
239
240
    /**
241
     * Gets the defined document class in shortform from schema
242
     *
243
     * @return string|false either the document class or false it not given
244
     */
245
    public function getDocumentClass()
246
    {
247
        $documentClass = false;
248
        if (isset($this->schema->{'x-documentClass'})) {
249
            $documentClass = $this->schema->{'x-documentClass'};
250
        }
251
        return $documentClass;
252
    }
253
254
    /**
255
     * Get defined constraints on this field (if any)
256
     *
257
     * @param string $field field that we get constraints spec from
258
     *
259
     * @return object
260
     */
261
    public function getConstraints($field)
262
    {
263
        return $this->getSchemaField($field, 'x-constraints', false);
264
    }
265
266
    /**
267
     * Tells us if in this model, the ID can be given on a POST request or not (in the payload).
268
     * This basically depends on if the "id" property is given in the JSON definition or not.
269
     *
270
     * @return bool true if yes, false otherwise
271
     */
272
    public function isIdInPostAllowed()
273
    {
274
        $isAllowed = true;
275
        if (isset($this->schema->{'x-id-in-post-allowed'})) {
276
            $isAllowed = $this->schema->{'x-id-in-post-allowed'};
277
        }
278
        return $isAllowed;
279
    }
280
281
    /**
282
     * get schema field value
283
     *
284
     * @param string $field         field name
285
     * @param string $property      property name
286
     * @param mixed  $fallbackValue fallback value if property isn't set
287
     *
288
     * @return mixed
289
     */
290
    private function getSchemaField($field, $property, $fallbackValue = '')
291
    {
292
        if (isset($this->schema->properties->$field->$property)) {
293
            $fallbackValue = $this->schema->properties->$field->$property;
294
        }
295
296
        return $fallbackValue;
297
    }
298
299
    /**
300
     * get searchable fields for this object
301
     *
302
     * @return string[]
303
     */
304
    public function getSearchableFields()
305
    {
306
        return $this->schema->searchable;
307
    }
308
}
309