Completed
Push — master ( 751246...4d4e9b )
by Lucas
10:58
created

Schema::__construct()   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

Importance

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