Completed
Push — feature/EVO-5807 ( fb3b19 )
by
unknown
65:50
created

SchemaModel::getHideOnEmptyExtref()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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