Completed
Push — feature/fix-somescrutinizer-is... ( 840c34...8bd6e3 )
by Narcotic
15:02 queued 08:41
created

Schema::setAdditionalProperties()   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
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Graviton Schema Document
4
 */
5
6
namespace Graviton\SchemaBundle\Document;
7
8
/**
9
 * Graviton\SchemaBundle\Document\Schema
10
 *
11
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
12
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
13
 * @link     http://swisscom.ch
14
 */
15
class Schema
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $title;
21
22
    /**
23
     * @var string
24
     */
25
    protected $description;
26
27
    /**
28
     * @var string
29
     */
30
    protected $type;
31
32
    /**
33
     * @var string
34
     */
35
    protected $format;
36
37
    /**
38
     * @var Schema
39
     */
40
    protected $items;
41
42
    /**
43
     * @var Schema[]
44
     */
45
    protected $properties = array();
46
47
    /**
48
     * @var Schema
49
     */
50
    protected $additionalProperties;
51
52
    /**
53
     * @var string[]
54
     */
55
    protected $required = array();
56
57
    /**
58
     * @var boolean
59
     */
60
    protected $translatable;
61
62
    /**
63
     * @var array
64
     */
65
    protected $refCollection = array();
66
67
    /**
68
     * possible event names this collection implements (queue events)
69
     *
70
     * @var array
71
     */
72
    protected $eventNames = array();
73
74
    /**
75
     * @var bool
76
     */
77
    protected $readOnly = false;
78
79
    /**
80
     * these are the BSON primitive types.
81
     * http://json-schema.org/latest/json-schema-core.html#anchor8
82
     * every type set *not* in this set will be carried over to 'format'
83
     *
84
     * @var string[]
85
     */
86
    protected $primitiveTypes = array(
87
        'array',
88
        'boolean',
89
        'integer',
90
        'number',
91
        'null',
92
        'object',
93
        'string'
94
    );
95
96
    /**
97
     * known non-primitive types we map to primitives here.
98
     * the type itself is set to the format.
99
     *
100
     * @var string[]
101
     */
102
    protected $specialTypeMapping = array(
103
        'extref' => 'string',
104
        'translatable' => 'object'
105
    );
106
107
    /**
108
     * set title
109
     *
110
     * @param string $title title
111
     *
112
     * @return void
113
     */
114 1
    public function setTitle($title)
115
    {
116 1
        $this->title = $title;
117 1
    }
118
119
    /**
120
     * get title
121
     *
122
     * @return string
123
     */
124 1
    public function getTitle()
125
    {
126 1
        return $this->title;
127
    }
128
129
    /**
130
     * set description
131
     *
132
     * @param string $description description
133
     *
134
     * @return void
135
     */
136 1
    public function setDescription($description)
137
    {
138 1
        $this->description = $description;
139 1
    }
140
141
    /**
142
     * get description
143
     *
144
     * @return string
145
     */
146 1
    public function getDescription()
147
    {
148 1
        return $this->description;
149
    }
150
151
    /**
152
     * set type
153
     *
154
     * @param string $type type
155
     *
156
     * @return void
157
     */
158 1
    public function setType($type)
159
    {
160 1
        if ($type === 'int') {
161 1
            $type = 'integer';
162 1
        }
163 1
        if ($type === 'hash') {
164
            $type = 'object';
165
        }
166
167
        // handle non-primitive types
168 1
        if (!in_array($type, $this->primitiveTypes)) {
169 1
            $setType = 'string';
170 1
            if (isset($this->specialTypeMapping[$type])) {
171 1
                $setType = $this->specialTypeMapping[$type];
172 1
            }
173 1
            $this->type = $setType;
174 1
            $this->setFormat($type);
175 1
        } else {
176 1
            $this->type = $type;
177
        }
178 1
    }
179
180
    /**
181
     * get type
182
     *
183
     * @return string type
184
     */
185 1
    public function getType()
186
    {
187 1
        return $this->type;
188
    }
189
190
    /**
191
     * get format
192
     *
193
     * @return string format
194
     */
195 1
    public function getFormat()
196
    {
197 1
        return $this->format;
198
    }
199
200
    /**
201
     * sets format
202
     *
203
     * @param string $format format
204
     *
205
     * @return void
206
     */
207 1
    public function setFormat($format)
208
    {
209 1
        $this->format = $format;
210 1
    }
211
212
    /**
213
     * set items
214
     *
215
     * @param Schema $items items schema
216
     *
217
     * @return void
218
     */
219 1
    public function setItems($items)
220
    {
221 1
        $this->items = $items;
222 1
    }
223
224
    /**
225
     * get items
226
     *
227
     * @return Schema
228
     */
229 1
    public function getItems()
230
    {
231 1
        return $this->items;
232
    }
233
234
    /**
235
     * add a property
236
     *
237
     * @param string $name     property name
238
     * @param Schema $property property
239
     *
240
     * @return void
241
     */
242 1
    public function addProperty($name, $property)
243
    {
244 1
        $this->properties[$name] = $property;
245 1
    }
246
247
    /**
248
     * removes a property
249
     *
250
     * @param string $name property name
251
     *
252
     * @return void
253
     */
254 1
    public function removeProperty($name)
255
    {
256 1
        unset($this->properties[$name]);
257 1
    }
258
259
    /**
260
     * returns a property
261
     *
262
     * @param string $name property name
263
     *
264
     * @return Schema property
265
     */
266
    public function getProperty($name)
267
    {
268
        return $this->properties[$name];
269
    }
270
271
    /**
272
     * get properties
273
     *
274
     * @return Schema[]|null
275
     */
276 1
    public function getProperties()
277
    {
278 1
        $properties = $this->properties;
279 1
        if (empty($properties)) {
280 1
            $properties = null;
281 1
        }
282
283 1
        return $properties;
284
    }
285
286
    /**
287
     * set additionalProperties on schema
288
     *
289
     * @param Schema $schema schema to use for additionalProperties type
290
     *
291
     * @return void
292
     */
293
    public function setAdditionalProperties(Schema $schema)
294
    {
295
        $this->additionalProperties = $schema;
296
    }
297
298
    /**
299
     * get addtionalProperties for schema
300
     *
301
     * @return Schema
302
     */
303 1
    public function getAdditionalProperties()
304
    {
305 1
        return $this->additionalProperties;
306
    }
307
308
    /**
309
     * set required variables
310
     *
311
     * @param string[] $required arary of required fields
312
     *
313
     * @return void
314
     */
315 1
    public function setRequired($required)
316
    {
317 1
        $this->required = $required;
318 1
    }
319
320
    /**
321
     * get required fields
322
     *
323
     * @return string[]|null
324
     */
325 1
    public function getRequired()
326
    {
327 1
        $required = $this->required;
328 1
        if (empty($required)) {
329 1
            $required = null;
330 1
        }
331
332 1
        return $required;
333
    }
334
335
    /**
336
     * set translatable flag
337
     *
338
     * This flag is a local extension to json schema.
339
     *
340
     * @param boolean $translatable translatable flag
341
     *
342
     * @return void
343
     */
344
    public function setTranslatable($translatable)
345
    {
346
        if ($translatable === true) {
347
            $this->setType('translatable');
348
        } else {
349
            $this->setType('string');
350
        }
351
    }
352
353
    /**
354
     * get translatable flag
355
     *
356
     * @return boolean
357
     */
358
    public function isTranslatable()
359
    {
360
        $ret = false;
361
        if ($this->getFormat() == 'translatable') {
362
            $ret = true;
363
        }
364
365
        return $ret;
366
    }
367
368
    /**
369
     * set a array of urls that can extref refer to
370
     *
371
     * @param array $refCollection urls
372
     *
373
     * @return void
374
     */
375 1
    public function setRefCollection(array $refCollection)
376
    {
377 1
        $this->refCollection = $refCollection;
378 1
    }
379
380
    /**
381
     * get a collection of urls that can extref refer to
382
     *
383
     * @return array
384
     */
385 1
    public function getRefCollection()
386
    {
387 1
        $collection = $this->refCollection;
388 1
        if (empty($collection)) {
389 1
            $collection = null;
390 1
        }
391
392 1
        return $collection;
393
    }
394
395
    /**
396
     * set an array of possible event names
397
     *
398
     * @param array $eventNames event names
399
     *
400
     * @return void
401
     */
402 1
    public function setEventNames(array $eventNames)
403
    {
404 1
        $this->eventNames = array_values($eventNames);
405 1
    }
406
407
    /**
408
     * get a collection of possible event names
409
     *
410
     * @return array
411
     */
412 1
    public function getEventNames()
413
    {
414 1
        $collection = $this->eventNames;
415 1
        if (empty($collection)) {
416 1
            $collection = null;
417 1
        }
418
419 1
        return $collection;
420
    }
421
422
    /**
423
     * Set the readOnly flag
424
     *
425
     * @param bool $readOnly ReadOnly flag
426
     *
427
     * @return void
428
     */
429 1
    public function setReadOnly($readOnly)
430
    {
431 1
        $this->readOnly = (bool) $readOnly;
432 1
    }
433
434
    /**
435
     * Get the readOnly flag.
436
     * Returns null if the flag is set to false so the serializer will ignore it.
437
     *
438
     * @return bool|null true if readOnly isset to true or null if not
439
     */
440 1
    public function getReadOnly()
441
    {
442 1
        return $this->readOnly ? true : null;
443
    }
444
}
445