Completed
Push — feature/OPTIONS_4_sf28_update ( b60ed0...30bad3 )
by
unknown
09:19
created

SchemaModel::getSearchableOfField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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';
0 ignored issues
show
Bug introduced by
The property _modelPath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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
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
     * Returns the bare schema
93
     *
94
     * @return \stdClass Schema
95
     */
96
    public function getSchema()
97
    {
98
        return $this->schema;
99
    }
100
101
    /**
102
     * get title for a given field
103
     *
104
     * @param string $field field name
105
     *
106
     * @return string
107
     */
108
    public function getTitleOfField($field)
109
    {
110
        return $this->getSchemaField($field, 'title');
111
    }
112
113
    /**
114
     * get description for a given field
115
     *
116
     * @param string $field field name
117
     *
118
     * @return string
119
     */
120
    public function getDescriptionOfField($field)
121
    {
122
        return $this->getSchemaField($field, 'description');
123
    }
124
125
    /**
126
     * get property model for embedded field
127
     *
128
     * @param string $mapping name of mapping class
129
     *
130
     * @return $this
131
     */
132
    public function manyPropertyModelForTarget($mapping)
133
    {
134
        // @todo refactor to get rid of container dependency (maybe remove from here)
135
        list($app, $bundle, , $document) = explode('\\', $mapping);
136
        $app = strtolower($app);
137
        $bundle = strtolower(substr($bundle, 0, -6));
138
        $document = strtolower($document);
139
        $propertyService = implode('.', array($app, $bundle, 'model', $document));
140
        $propertyModel = $this->container->get($propertyService);
141
142
        return $propertyModel;
143
    }
144
145
    /**
146
     * get required fields for this object
147
     *
148
     * @return string[]
149
     */
150
    public function getRequiredFields()
151
    {
152
        return $this->schema->required;
153
    }
154
155
    /**
156
     * get a collection of service names that can extref refer to
157
     *
158
     * @param string $field field name
159
     *
160
     * @return array
161
     */
162
    public function getRefCollectionOfField($field)
163
    {
164
        return $this->getSchemaField($field, 'collection', array());
165
    }
166
167
    /**
168
     * get readOnly flag for a given field
169
     *
170
     * @param string $field field name
171
     *
172
     * @return boolean the readOnly flag
173
     */
174
    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...
175
    {
176
        return $this->getSchemaField($field, 'readOnly', false);
177
    }
178
179
    /**
180
     * get searchable flag for a given field
181
     *
182
     * @param string $field field name
183
     *
184
     * @return boolean the searchable flag
185
     */
186
    public function getSearchableOfField($field)
0 ignored issues
show
Coding Style introduced by
function getSearchableOfField() 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...
187
    {
188
        return $this->getSchemaField($field, 'searchable', false);
189
    }
190
191
    /**
192
     * tell us if a model what to be exposed using a key as field
193
     *
194
     * @param string $field field that we check for dynamic-key spec
195
     *
196
     * @return boolean
197
     */
198
    public function hasDynamicKey($field)
199
    {
200
        return $this->getSchemaField($field, 'x-dynamic-key', false) !== false;
201
    }
202
203
    /**
204
     * Get field used for setting stringy key value
205
     *
206
     * @param string $field field that we get dynamic-key spec from
207
     *
208
     * @return object
209
     */
210
    public function getDynamicKeySpec($field)
211
    {
212
        return $this->getSchemaField($field, 'x-dynamic-key');
213
    }
214
215
    /**
216
     * get schema field value
217
     *
218
     * @param string $field         field name
219
     * @param string $property      property name
220
     * @param mixed  $fallbackValue fallback value if property isn't set
221
     *
222
     * @return mixed
223
     */
224
    private function getSchemaField($field, $property, $fallbackValue = '')
225
    {
226
        if (isset($this->schema->properties->$field->$property)) {
227
            $fallbackValue = $this->schema->properties->$field->$property;
228
        }
229
230
        return $fallbackValue;
231
    }
232
233
    /**
234
     * get searchable fields for this object
235
     *
236
     * @return string[]
237
     */
238
    public function getSearchableFields()
239
    {
240
        return $this->schema->searchable;
241
    }
242
}
243