Completed
Push — feature/searchable-wip ( 58d35d )
by
unknown
09:20
created

SchemaModel::getSearchableFields()   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 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';
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 searchable fields for this object
157
     *
158
     * @return string[]
159
     */
160
    public function getSearchableFields()
161
    {
162
        return $this->schema->searchable;
163
    }
164
165
    /**
166
     * get a collection of service names that can extref refer to
167
     *
168
     * @param string $field field name
169
     *
170
     * @return array
171
     */
172
    public function getRefCollectionOfField($field)
173
    {
174
        return $this->getSchemaField($field, 'collection', array());
175
    }
176
177
    /**
178
     * get readOnly flag for a given field
179
     *
180
     * @param string $field field name
181
     *
182
     * @return boolean the readOnly flag
183
     */
184
    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...
185
    {
186
        return $this->getSchemaField($field, 'readOnly', false);
187
    }
188
189
    /**
190
     * tell us if a model what to be exposed using a key as field
191
     *
192
     * @param string $field field that we check for dynamic-key spec
193
     *
194
     * @return boolean
195
     */
196
    public function hasDynamicKey($field)
197
    {
198
        return $this->getSchemaField($field, 'x-dynamic-key', false) !== false;
199
    }
200
201
    /**
202
     * Get field used for setting stringy key value
203
     *
204
     * @param string $field field that we get dynamic-key spec from
205
     *
206
     * @return object
207
     */
208
    public function getDynamicKeySpec($field)
209
    {
210
        return $this->getSchemaField($field, 'x-dynamic-key');
211
    }
212
213
    /**
214
     * get schema field value
215
     *
216
     * @param string $field         field name
217
     * @param string $property      property name
218
     * @param mixed  $fallbackValue fallback value if property isn't set
219
     *
220
     * @return mixed
221
     */
222
    private function getSchemaField($field, $property, $fallbackValue = '')
223
    {
224
        if (isset($this->schema->properties->$field->$property)) {
225
            $fallbackValue = $this->schema->properties->$field->$property;
226
        }
227
228
        return $fallbackValue;
229
    }
230
}
231