Completed
Push — feature/other-validation ( aebe33...b701a5 )
by Narcotic
61:14
created

Schema::getMinLengthTypes()   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 0
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 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 int
98
     */
99
    protected $minItems;
100
101
    /**
102
     * @var int
103
     */
104
    protected $maxItems;
105
106
    /**
107
     * @var float
108
     */
109
    protected $numericMinimum;
110
111
    /**
112
     * @var float
113
     */
114
    protected $numericMaximum;
115
116
    /**
117
     * @var SchemaEnum
118
     */
119
    protected $enum;
120
121
    /**
122
     * @var string
123
     */
124
    protected $regexPattern;
125
126
    /**
127
     * these are the BSON primitive types.
128
     * http://json-schema.org/latest/json-schema-core.html#anchor8
129
     * every type set *not* in this set will be carried over to 'format'
130
     *
131
     * @var string[]
132
     */
133
    protected $primitiveTypes = [
134
        'array',
135
        'boolean',
136
        'integer',
137
        'number',
138
        'null',
139
        'object',
140
        'string'
141
    ];
142
143
    /**
144
     * those are types that when they are required, a minimal length
145
     * shall be specified in schema
146
     *
147
     * @var array
148
     */
149
    protected $minLengthTypes = [
150
        'integer',
151
        'number',
152
        'string'
153
    ];
154
155
    /**
156
     * known non-primitive types we map to primitives here.
157
     * the type itself is set to the format.
158
     *
159
     * @var string[]
160
     */
161
    protected $specialTypeMapping = [
162
        'extref' => 'string',
163
        'translatable' => 'object',
164
        'date' => 'string',
165
        'float' => 'number',
166
        'double' => 'number',
167
        'decimal' => 'number'
168
    ];
169
170
    protected $formatOverrides = [
171
        'date' => 'date-time'
172
    ];
173
174
    /**
175
     * Build properties
176
     */
177
    public function __construct()
178
    {
179
        $this->properties = new ArrayCollection();
180
    }
181
182
    /**
183
     * set title
184
     *
185
     * @param string $title title
186
     *
187
     * @return void
188
     */
189
    public function setTitle($title)
190
    {
191
        $this->title = $title;
192
    }
193
194
    /**
195
     * get title
196
     *
197
     * @return string
198
     */
199
    public function getTitle()
200
    {
201
        return $this->title;
202
    }
203
204
    /**
205
     * set description
206
     *
207
     * @param string $description description
208
     *
209
     * @return void
210
     */
211
    public function setDescription($description)
212
    {
213
        $this->description = $description;
214
    }
215
216
    /**
217
     * get description
218
     *
219
     * @return string
220
     */
221
    public function getDescription()
222
    {
223
        return $this->description;
224
    }
225
226
    /**
227
     * set type
228
     *
229
     * @param string $type type
230
     *
231
     * @return void
232
     */
233
    public function setType($type)
234
    {
235
        if ($type === 'int') {
236
            $type = 'integer';
237
        }
238
        if ($type === 'hash') {
239
            $type = 'object';
240
        }
241
242
        // handle non-primitive types
243
        if (!in_array($type, $this->primitiveTypes)) {
244
            $setType = 'string';
245
            if (isset($this->specialTypeMapping[$type])) {
246
                $setType = $this->specialTypeMapping[$type];
247
            }
248
            $this->type = $setType;
249
250
            if (isset($this->formatOverrides[$type])) {
251
                $type = $this->formatOverrides[$type];
252
            }
253
254
            $this->setFormat($type);
255
        } else {
256
            $this->type = $type;
257
        }
258
    }
259
260
    /**
261
     * get type
262
     *
263
     * @return string type
264
     */
265
    public function getType()
266
    {
267
        return $this->type;
268
    }
269
270
    /**
271
     * get MinLengthTypes
272
     *
273
     * @return array MinLengthTypes
274
     */
275
    public function getMinLengthTypes()
276
    {
277
        return $this->minLengthTypes;
278
    }
279
280
    /**
281
     * get format
282
     *
283
     * @return string format
284
     */
285
    public function getFormat()
286
    {
287
        return $this->format;
288
    }
289
290
    /**
291
     * sets format
292
     *
293
     * @param string $format format
294
     *
295
     * @return void
296
     */
297
    public function setFormat($format)
298
    {
299
        $this->format = $format;
300
    }
301
302
    /**
303
     * get numeric minimum
304
     *
305
     * @return float numeric minimum
306
     */
307
    public function getNumericMinimum()
308
    {
309
        return $this->numericMinimum;
310
    }
311
312
    /**
313
     * set numeric minimum
314
     *
315
     * @param float $numericMinimum numeric mimimum
316
     *
317
     * @return void
318
     */
319
    public function setNumericMinimum($numericMinimum)
320
    {
321
        $this->numericMinimum = $numericMinimum;
322
    }
323
324
    /**
325
     * get numeric maximum
326
     *
327
     * @return float numeric maximum
328
     */
329
    public function getNumericMaximum()
330
    {
331
        return $this->numericMaximum;
332
    }
333
334
    /**
335
     * set numeric maximum
336
     *
337
     * @param float $numericMaximum maximum
338
     *
339
     * @return void
340
     */
341
    public function setNumericMaximum($numericMaximum)
342
    {
343
        $this->numericMaximum = $numericMaximum;
344
    }
345
346
    /**
347
     * set min length
348
     *
349
     * @return int length
350
     */
351
    public function getMinLength()
352
    {
353
        return $this->minLength;
354
    }
355
356
    /**
357
     * get min length
358
     *
359
     * @param int $minLength length
360
     *
361
     * @return void
362
     */
363
    public function setMinLength($minLength)
364
    {
365
        $this->minLength = $minLength;
366
    }
367
368
    /**
369
     * gets maxlength
370
     *
371
     * @return int length
372
     */
373
    public function getMaxLength()
374
    {
375
        return $this->maxLength;
376
    }
377
378
    /**
379
     * set maxlength
380
     *
381
     * @param int $maxLength length
382
     *
383
     * @return void
384
     */
385
    public function setMaxLength($maxLength)
386
    {
387
        $this->maxLength = $maxLength;
388
    }
389
390
    /**
391
     * set min Items
392
     *
393
     * @return int Items
394
     */
395
    public function getMinItems()
396
    {
397
        return $this->minItems;
398
    }
399
400
    /**
401
     * get min Items
402
     *
403
     * @param int $minItems length
404
     *
405
     * @return void
406
     */
407
    public function setMinItems($minItems)
408
    {
409
        $this->minItems = $minItems;
410
    }
411
412
    /**
413
     * gets maxItems
414
     *
415
     * @return int Items
416
     */
417
    public function getMaxItems()
418
    {
419
        return $this->maxItems;
420
    }
421
422
    /**
423
     * set maxItems
424
     *
425
     * @param int $maxItems Items
426
     *
427
     * @return void
428
     */
429
    public function setMaxItems($maxItems)
430
    {
431
        $this->maxItems = $maxItems;
432
    }
433
434
    /**
435
     * get Enum
436
     *
437
     * @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...
438
     */
439
    public function getEnum()
440
    {
441
        return $this->enum;
442
    }
443
444
    /**
445
     * set Enum
446
     *
447
     * @param array $enum enum
448
     *
449
     * @return void
450
     */
451
    public function setEnum(array $enum)
452
    {
453
        $this->enum = new SchemaEnum($enum);
454
    }
455
456
    /**
457
     * get regex pattern
458
     *
459
     * @return string pattern
460
     */
461
    public function getRegexPattern()
462
    {
463
        return $this->regexPattern;
464
    }
465
466
    /**
467
     * set regex pattern
468
     *
469
     * @param string $regexPattern regex pattern
470
     *
471
     * @return void
472
     */
473
    public function setRegexPattern($regexPattern)
474
    {
475
        $this->regexPattern = $regexPattern;
476
    }
477
478
    /**
479
     * set items
480
     *
481
     * @param Schema $items items schema
482
     *
483
     * @return void
484
     */
485
    public function setItems($items)
486
    {
487
        $this->items = $items;
488
    }
489
490
    /**
491
     * get items
492
     *
493
     * @return Schema
494
     */
495
    public function getItems()
496
    {
497
        return $this->items;
498
    }
499
500
    /**
501
     * add a property
502
     *
503
     * @param string $name     property name
504
     * @param Schema $property property
505
     *
506
     * @return void
507
     */
508
    public function addProperty($name, $property)
509
    {
510
        $this->properties->set($name, $property);
511
    }
512
513
    /**
514
     * removes a property
515
     *
516
     * @param string $name property name
517
     *
518
     * @return void
519
     */
520
    public function removeProperty($name)
521
    {
522
        if (!$this->properties->containsKey($name)) {
523
            $this->properties->remove($this->properties->get($name));
524
        }
525
    }
526
527
    /**
528
     * returns a property
529
     *
530
     * @param string $name property name
531
     *
532
     * @return void|Schema property
533
     */
534
    public function getProperty($name)
535
    {
536
        return $this->properties->get($name);
537
    }
538
539
    /**
540
     * get properties
541
     *
542
     * @return ArrayCollection|null
543
     */
544
    public function getProperties()
545
    {
546
        if ($this->properties->isEmpty()) {
547
            return null;
548
        } else {
549
            return $this->properties;
550
        }
551
    }
552
553
    /**
554
     * set additionalProperties on schema
555
     *
556
     * @param SchemaAdditionalProperties $additionalProperties additional properties
557
     *
558
     * @return void
559
     */
560
    public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties)
561
    {
562
        $this->additionalProperties = $additionalProperties;
563
    }
564
565
    /**
566
     * get addtionalProperties for schema
567
     *
568
     * @return SchemaAdditionalProperties
569
     */
570
    public function getAdditionalProperties()
571
    {
572
        return $this->additionalProperties;
573
    }
574
575
    /**
576
     * set required variables
577
     *
578
     * @param string[] $required arary of required fields
579
     *
580
     * @return void
581
     */
582
    public function setRequired($required)
583
    {
584
        $this->required = $required;
585
    }
586
587
    /**
588
     * get required fields
589
     *
590
     * @return string[]|null
591
     */
592
    public function getRequired()
593
    {
594
        $required = $this->required;
595
        if (empty($required)) {
596
            $required = null;
597
        }
598
599
        return $required;
600
    }
601
602
    /**
603
     * set translatable flag
604
     *
605
     * This flag is a local extension to json schema.
606
     *
607
     * @param boolean $translatable translatable flag
608
     *
609
     * @return void
610
     */
611
    public function setTranslatable($translatable)
612
    {
613
        if ($translatable === true) {
614
            $this->setType('translatable');
615
        } else {
616
            $this->setType('string');
617
        }
618
    }
619
620
    /**
621
     * get translatable flag
622
     *
623
     * @return boolean
624
     */
625
    public function isTranslatable()
626
    {
627
        $ret = false;
628
        if ($this->getFormat() == 'translatable') {
629
            $ret = true;
630
        }
631
632
        return $ret;
633
    }
634
635
    /**
636
     * set a array of urls that can extref refer to
637
     *
638
     * @param array $refCollection urls
639
     *
640
     * @return void
641
     */
642
    public function setRefCollection(array $refCollection)
643
    {
644
        $this->refCollection = $refCollection;
645
    }
646
647
    /**
648
     * get a collection of urls that can extref refer to
649
     *
650
     * @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...
651
     */
652
    public function getRefCollection()
653
    {
654
        $collection = $this->refCollection;
655
        if (empty($collection)) {
656
            $collection = null;
657
        }
658
659
        return $collection;
660
    }
661
662
    /**
663
     * set an array of possible event names
664
     *
665
     * @param array $eventNames event names
666
     *
667
     * @return void
668
     */
669
    public function setEventNames(array $eventNames)
670
    {
671
        $this->eventNames = array_values($eventNames);
672
    }
673
674
    /**
675
     * get a collection of possible event names
676
     *
677
     * @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...
678
     */
679
    public function getEventNames()
680
    {
681
        $collection = $this->eventNames;
682
        if (empty($collection)) {
683
            $collection = null;
684
        }
685
686
        return $collection;
687
    }
688
689
    /**
690
     * Set the readOnly flag
691
     *
692
     * @param bool $readOnly ReadOnly flag
693
     *
694
     * @return void
695
     */
696
    public function setReadOnly($readOnly)
697
    {
698
        $this->readOnly = (bool) $readOnly;
699
    }
700
701
    /**
702
     * Get the readOnly flag.
703
     * Returns null if the flag is set to false so the serializer will ignore it.
704
     *
705
     * @return bool|null true if readOnly isset to true or null if not
706
     */
707
    public function getReadOnly()
708
    {
709
        return $this->readOnly ? true : null;
710
    }
711
712
    /**
713
     * set searchable variables
714
     *
715
     * @param string[] $searchable arary of searchable fields
716
     *
717
     * @return void
718
     */
719
    public function setSearchable($searchable)
720
    {
721
        $this->searchable = $searchable;
722
    }
723
724
    /**
725
     * get searchable fields
726
     *
727
     * @return string[]|null
728
     */
729
    public function getSearchable()
730
    {
731
        $searchable = $this->searchable;
732
        if (empty($searchable)) {
733
            $searchable = null;
734
        }
735
736
        return $searchable;
737
    }
738
}
739