Completed
Push — feature/other-validation ( 1442c4...18a759 )
by Narcotic
64:54
created

Schema::getRefCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
ccs 0
cts 2
cp 0
cc 2
eloc 5
nc 2
nop 0
crap 6
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 SchemaAdditionalProperties
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
     * @var string[]
83
     */
84
    protected $searchable = array();
85
86
    /**
87
     * @var int
88
     */
89
    protected $minLength;
90
91
    /**
92
     * @var int
93
     */
94
    protected $maxLength;
95
96
    /**
97
     * @var SchemaEnum
98
     */
99
    protected $enum;
100
101
    /**
102
     * these are the BSON primitive types.
103
     * http://json-schema.org/latest/json-schema-core.html#anchor8
104
     * every type set *not* in this set will be carried over to 'format'
105
     *
106
     * @var string[]
107
     */
108
    protected $primitiveTypes = array(
109
        'array',
110
        'boolean',
111
        'integer',
112
        'number',
113
        'null',
114
        'object',
115
        'string'
116
    );
117
118
    /**
119
     * known non-primitive types we map to primitives here.
120
     * the type itself is set to the format.
121
     *
122
     * @var string[]
123
     */
124
    protected $specialTypeMapping = [
125
        'extref' => 'string',
126
        'translatable' => 'object',
127
        'date' => 'string',
128
        'float' => 'number',
129
        'double' => 'number',
130
        'decimal' => 'number'
131
    ];
132
133
    protected $formatOverrides = [
134
        'date' => 'date-time'
135
    ];
136
137
    /**
138
     * Build properties
139
     */
140
    public function __construct()
141
    {
142
        $this->properties = new ArrayCollection();
143
    }
144
145
    /**
146
     * set title
147
     *
148
     * @param string $title title
149
     *
150
     * @return void
151
     */
152
    public function setTitle($title)
153
    {
154
        $this->title = $title;
155
    }
156
157
    /**
158
     * get title
159
     *
160
     * @return string
161
     */
162
    public function getTitle()
163
    {
164
        return $this->title;
165
    }
166
167
    /**
168
     * set description
169
     *
170
     * @param string $description description
171
     *
172
     * @return void
173
     */
174
    public function setDescription($description)
175
    {
176
        $this->description = $description;
177
    }
178
179
    /**
180
     * get description
181
     *
182
     * @return string
183
     */
184
    public function getDescription()
185
    {
186
        return $this->description;
187
    }
188
189
    /**
190
     * set type
191
     *
192
     * @param string $type type
193
     *
194
     * @return void
195
     */
196
    public function setType($type)
197
    {
198
        if ($type === 'int') {
199
            $type = 'integer';
200
        }
201
        if ($type === 'hash') {
202
            $type = 'object';
203
        }
204
205
        // handle non-primitive types
206
        if (!in_array($type, $this->primitiveTypes)) {
207
            $setType = 'string';
208
            if (isset($this->specialTypeMapping[$type])) {
209
                $setType = $this->specialTypeMapping[$type];
210
            }
211
            $this->type = $setType;
212
213
            if (isset($this->formatOverrides[$type])) {
214
                $type = $this->formatOverrides[$type];
215
            }
216
217
            $this->setFormat($type);
218
        } else {
219
            $this->type = $type;
220
        }
221
    }
222
223
    /**
224
     * get type
225
     *
226
     * @return string type
227
     */
228
    public function getType()
229
    {
230
        return $this->type;
231
    }
232
233
    /**
234
     * get format
235
     *
236
     * @return string format
237
     */
238
    public function getFormat()
239
    {
240
        return $this->format;
241
    }
242
243
    /**
244
     * sets format
245
     *
246
     * @param string $format format
247
     *
248
     * @return void
249
     */
250
    public function setFormat($format)
251
    {
252
        $this->format = $format;
253
    }
254
255
    /**
256
     * set min length
257
     *
258
     * @return int length
259
     */
260
    public function getMinLength()
261
    {
262
        return $this->minLength;
263
    }
264
265
    /**
266
     * get min length
267
     *
268
     * @param int $minLength length
269
     *
270
     * @return void
271
     */
272
    public function setMinLength($minLength)
273
    {
274
        $this->minLength = $minLength;
275
    }
276
277
    /**
278
     * gets maxlength
279
     *
280
     * @return int length
281
     */
282
    public function getMaxLength()
283
    {
284
        return $this->maxLength;
285
    }
286
287
    /**
288
     * set maxlength
289
     *
290
     * @param int $maxLength length
291
     *
292
     * @return void
293
     */
294
    public function setMaxLength($maxLength)
295
    {
296
        $this->maxLength = $maxLength;
297
    }
298
299
    /**
300
     * get Enum
301
     *
302
     * @return array Enum
0 ignored issues
show
Documentation introduced by
Should the return type not be SchemaEnum?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
303
     */
304
    public function getEnum()
305
    {
306
        return $this->enum;
307
    }
308
309
    /**
310
     * set Enum
311
     *
312
     * @param array $enum enum
313
     *
314
     * @return void
315
     */
316
    public function setEnum(array $enum)
317
    {
318
        $this->enum = new SchemaEnum($enum);
319
    }
320
321
    /**
322
     * set items
323
     *
324
     * @param Schema $items items schema
325
     *
326
     * @return void
327
     */
328
    public function setItems($items)
329
    {
330
        $this->items = $items;
331
    }
332
333
    /**
334
     * get items
335
     *
336
     * @return Schema
337
     */
338
    public function getItems()
339
    {
340
        return $this->items;
341
    }
342
343
    /**
344
     * add a property
345
     *
346
     * @param string $name     property name
347
     * @param Schema $property property
348
     *
349
     * @return void
350
     */
351
    public function addProperty($name, $property)
352
    {
353
        $this->properties->set($name, $property);
354
    }
355
356
    /**
357
     * removes a property
358
     *
359
     * @param string $name property name
360
     *
361
     * @return void
362
     */
363
    public function removeProperty($name)
364
    {
365
        if (!$this->properties->containsKey($name)) {
366
            $this->properties->remove($this->properties->get($name));
367
        }
368
    }
369
370
    /**
371
     * returns a property
372
     *
373
     * @param string $name property name
374
     *
375
     * @return void|Schema property
376
     */
377
    public function getProperty($name)
378
    {
379
        return $this->properties->get($name);
380
    }
381
382
    /**
383
     * get properties
384
     *
385
     * @return ArrayCollection|null
386
     */
387
    public function getProperties()
388
    {
389
        if ($this->properties->isEmpty()) {
390
            return null;
391
        } else {
392
            return $this->properties;
393
        }
394
    }
395
396
    /**
397
     * set additionalProperties on schema
398
     *
399
     * @param SchemaAdditionalProperties $additionalProperties additional properties
400
     *
401
     * @return void
402
     */
403
    public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties)
404
    {
405
        $this->additionalProperties = $additionalProperties;
406
    }
407
408
    /**
409
     * get addtionalProperties for schema
410
     *
411
     * @return SchemaAdditionalProperties
412
     */
413
    public function getAdditionalProperties()
414
    {
415
        return $this->additionalProperties;
416
    }
417
418
    /**
419
     * set required variables
420
     *
421
     * @param string[] $required arary of required fields
422
     *
423
     * @return void
424
     */
425
    public function setRequired($required)
426
    {
427
        $this->required = $required;
428
    }
429
430
    /**
431
     * get required fields
432
     *
433
     * @return string[]|null
434
     */
435
    public function getRequired()
436
    {
437
        $required = $this->required;
438
        if (empty($required)) {
439
            $required = null;
440
        }
441
442
        return $required;
443
    }
444
445
    /**
446
     * set translatable flag
447
     *
448
     * This flag is a local extension to json schema.
449
     *
450
     * @param boolean $translatable translatable flag
451
     *
452
     * @return void
453
     */
454
    public function setTranslatable($translatable)
455
    {
456
        if ($translatable === true) {
457
            $this->setType('translatable');
458
        } else {
459
            $this->setType('string');
460
        }
461
    }
462
463
    /**
464
     * get translatable flag
465
     *
466
     * @return boolean
467
     */
468
    public function isTranslatable()
469
    {
470
        $ret = false;
471
        if ($this->getFormat() == 'translatable') {
472
            $ret = true;
473
        }
474
475
        return $ret;
476
    }
477
478
    /**
479
     * set a array of urls that can extref refer to
480
     *
481
     * @param array $refCollection urls
482
     *
483
     * @return void
484
     */
485
    public function setRefCollection(array $refCollection)
486
    {
487
        $this->refCollection = $refCollection;
488
    }
489
490
    /**
491
     * get a collection of urls that can extref refer to
492
     *
493
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
494
     */
495
    public function getRefCollection()
496
    {
497
        $collection = $this->refCollection;
498
        if (empty($collection)) {
499
            $collection = null;
500
        }
501
502
        return $collection;
503
    }
504
505
    /**
506
     * set an array of possible event names
507
     *
508
     * @param array $eventNames event names
509
     *
510
     * @return void
511
     */
512
    public function setEventNames(array $eventNames)
513
    {
514
        $this->eventNames = array_values($eventNames);
515
    }
516
517
    /**
518
     * get a collection of possible event names
519
     *
520
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
521
     */
522
    public function getEventNames()
523
    {
524
        $collection = $this->eventNames;
525
        if (empty($collection)) {
526
            $collection = null;
527
        }
528
529
        return $collection;
530
    }
531
532
    /**
533
     * Set the readOnly flag
534
     *
535
     * @param bool $readOnly ReadOnly flag
536
     *
537
     * @return void
538
     */
539
    public function setReadOnly($readOnly)
540
    {
541
        $this->readOnly = (bool) $readOnly;
542
    }
543
544
    /**
545
     * Get the readOnly flag.
546
     * Returns null if the flag is set to false so the serializer will ignore it.
547
     *
548
     * @return bool|null true if readOnly isset to true or null if not
549
     */
550
    public function getReadOnly()
551
    {
552
        return $this->readOnly ? true : null;
553
    }
554
555
    /**
556
     * set searchable variables
557
     *
558
     * @param string[] $searchable arary of searchable fields
559
     *
560
     * @return void
561
     */
562
    public function setSearchable($searchable)
563
    {
564
        $this->searchable = $searchable;
565
    }
566
567
    /**
568
     * get searchable fields
569
     *
570
     * @return string[]|null
571
     */
572
    public function getSearchable()
573
    {
574
        $searchable = $this->searchable;
575
        if (empty($searchable)) {
576
            $searchable = null;
577
        }
578
579
        return $searchable;
580
    }
581
}
582