Completed
Push — feature/EVO-5751-text-index-mo... ( 0ab3ac...8fecb1 )
by
unknown
62:16 queued 57:01
created

Schema::setType()   C

Complexity

Conditions 8
Paths 42

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9.6648

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 35
ccs 19
cts 27
cp 0.7037
rs 5.3846
c 2
b 0
f 0
cc 8
eloc 20
nc 42
nop 1
crap 9.6648
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 SchemaType
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 = [];
58
59
    /**
60
     * @var boolean
61
     */
62
    protected $translatable;
63
64
    /**
65
     * @var array
66
     */
67
    protected $refCollection = [];
68
69
    /**
70
     * possible event names this collection implements (queue events)
71
     *
72
     * @var array
73
     */
74
    protected $eventNames = [];
75
76
    /**
77
     * @var bool
78
     */
79
    protected $readOnly = false;
80
81
    /**
82
     * @var string[]
83
     */
84
    protected $searchable = [];
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
     * @var string
128
     */
129
    protected $documentClass;
130
131
    /**
132
     * @var array<string>
133
     */
134
    protected $constraints;
135
136
    /**
137
     * these are the BSON primitive types.
138
     * http://json-schema.org/latest/json-schema-core.html#anchor8
139
     * every type set *not* in this set will be carried over to 'format'
140
     *
141
     * @var string[]
142
     */
143
    protected $primitiveTypes = [
144
        'array',
145
        'boolean',
146
        'integer',
147
        'number',
148
        'null',
149
        'object',
150
        'string'
151
    ];
152
153
    /**
154
     * those are types that when they are required, a minimal length
155
     * shall be specified in schema (or allow null if not required; that will lead
156
     * to the inclusion of "null" in the "type" property array)
157
     *
158
     * @var array
159
     */
160
    protected $minLengthTypes = [
161
        'integer',
162
        'number',
163
        'float',
164
        'double',
165
        'decimal',
166
        'string',
167
        'date',
168
        'extref'
169
    ];
170
171
    /**
172
     * known non-primitive types we map to primitives here.
173
     * the type itself is set to the format.
174
     *
175
     * @var string[]
176
     */
177
    protected $specialTypeMapping = [
178
        'extref' => 'string',
179
        'translatable' => 'object',
180
        'date' => 'string',
181
        'float' => 'number',
182
        'double' => 'number',
183
        'decimal' => 'number'
184
    ];
185
186
    protected $formatOverrides = [
187
        'date' => 'date-time'
188
    ];
189
190
    /**
191
     * Build properties
192
     */
193 4
    public function __construct()
194
    {
195 4
        $this->properties = new ArrayCollection();
196 4
    }
197
198
    /**
199
     * set title
200
     *
201
     * @param string $title title
202
     *
203
     * @return void
204
     */
205 4
    public function setTitle($title)
206
    {
207 4
        $this->title = $title;
208 4
    }
209
210
    /**
211
     * get title
212
     *
213
     * @return string
214
     */
215 4
    public function getTitle()
216
    {
217 4
        return $this->title;
218
    }
219
220
    /**
221
     * set description
222
     *
223
     * @param string $description description
224
     *
225
     * @return void
226
     */
227 2
    public function setDescription($description)
228
    {
229 2
        $this->description = $description;
230 2
    }
231
232
    /**
233
     * get description
234
     *
235
     * @return string
236
     */
237 2
    public function getDescription()
238
    {
239 2
        return $this->description;
240
    }
241
242
    /**
243
     * set type
244
     *
245
     * @param string|array $types types
246
     *
247
     * @return void
248
     */
249 2
    public function setType($types)
250
    {
251 2
        if (!is_array($types)) {
252 2
            $types = [$types];
253 1
        }
254
255 2
        $typesToSet = [];
256 2
        foreach ($types as $type) {
257 2
            if ($type === 'int') {
258
                $type = 'integer';
259
            }
260 2
            if ($type === 'hash') {
261
                $type = 'object';
262
            }
263
264
            // handle non-primitive types
265 2
            if (!in_array($type, $this->primitiveTypes)) {
266 2
                $setType = 'string';
267 2
                if (isset($this->specialTypeMapping[$type])) {
268
                    $setType = $this->specialTypeMapping[$type];
269
                }
270 2
                $typesToSet[] = $setType;
271
272 2
                if (isset($this->formatOverrides[$type])) {
273
                    $type = $this->formatOverrides[$type];
274
                }
275
276 2
                $this->setFormat($type);
277 1
            } else {
278 2
                $typesToSet[] = $type;
279
            }
280 1
        }
281
282 2
        $this->type = new SchemaType($typesToSet);
283 2
    }
284
285
    /**
286
     * get type
287
     *
288
     * @return SchemaType type
289
     */
290 2
    public function getType()
291
    {
292 2
        return $this->type;
293
    }
294
295
    /**
296
     * get MinLengthTypes
297
     *
298
     * @return array MinLengthTypes
299
     */
300 2
    public function getMinLengthTypes()
301
    {
302 2
        return $this->minLengthTypes;
303
    }
304
305
    /**
306
     * get format
307
     *
308
     * @return string format
309
     */
310 2
    public function getFormat()
311
    {
312 2
        return $this->format;
313
    }
314
315
    /**
316
     * sets format
317
     *
318
     * @param string $format format
319
     *
320
     * @return void
321
     */
322 2
    public function setFormat($format)
323
    {
324 2
        $this->format = $format;
325 2
    }
326
327
    /**
328
     * get numeric minimum
329
     *
330
     * @return float numeric minimum
331
     */
332 2
    public function getNumericMinimum()
333
    {
334 2
        return $this->numericMinimum;
335
    }
336
337
    /**
338
     * set numeric minimum
339
     *
340
     * @param float $numericMinimum numeric mimimum
341
     *
342
     * @return void
343
     */
344
    public function setNumericMinimum($numericMinimum)
345
    {
346
        $this->numericMinimum = $numericMinimum;
347
    }
348
349
    /**
350
     * get numeric maximum
351
     *
352
     * @return float numeric maximum
353
     */
354 2
    public function getNumericMaximum()
355
    {
356 2
        return $this->numericMaximum;
357
    }
358
359
    /**
360
     * set numeric maximum
361
     *
362
     * @param float $numericMaximum maximum
363
     *
364
     * @return void
365
     */
366
    public function setNumericMaximum($numericMaximum)
367
    {
368
        $this->numericMaximum = $numericMaximum;
369
    }
370
371
    /**
372
     * set min length
373
     *
374
     * @return int length
375
     */
376 2
    public function getMinLength()
377
    {
378 2
        return $this->minLength;
379
    }
380
381
    /**
382
     * get min length
383
     *
384
     * @param int $minLength length
385
     *
386
     * @return void
387
     */
388
    public function setMinLength($minLength)
389
    {
390
        $this->minLength = $minLength;
391
    }
392
393
    /**
394
     * gets maxlength
395
     *
396
     * @return int length
397
     */
398 2
    public function getMaxLength()
399
    {
400 2
        return $this->maxLength;
401
    }
402
403
    /**
404
     * set maxlength
405
     *
406
     * @param int $maxLength length
407
     *
408
     * @return void
409
     */
410
    public function setMaxLength($maxLength)
411
    {
412
        $this->maxLength = $maxLength;
413
    }
414
415
    /**
416
     * set min Items
417
     *
418
     * @return int Items
419
     */
420 2
    public function getMinItems()
421
    {
422 2
        return $this->minItems;
423
    }
424
425
    /**
426
     * get min Items
427
     *
428
     * @param int $minItems length
429
     *
430
     * @return void
431
     */
432
    public function setMinItems($minItems)
433
    {
434
        $this->minItems = $minItems;
435
    }
436
437
    /**
438
     * gets maxItems
439
     *
440
     * @return int Items
441
     */
442 2
    public function getMaxItems()
443
    {
444 2
        return $this->maxItems;
445
    }
446
447
    /**
448
     * set maxItems
449
     *
450
     * @param int $maxItems Items
451
     *
452
     * @return void
453
     */
454
    public function setMaxItems($maxItems)
455
    {
456
        $this->maxItems = $maxItems;
457
    }
458
459
    /**
460
     * get Enum
461
     *
462
     * @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...
463
     */
464 2
    public function getEnum()
465
    {
466 2
        return $this->enum;
467
    }
468
469
    /**
470
     * set Enum
471
     *
472
     * @param array $enum enum
473
     *
474
     * @return void
475
     */
476
    public function setEnum(array $enum)
477
    {
478
        $this->enum = new SchemaEnum($enum);
479
    }
480
481
    /**
482
     * get regex pattern
483
     *
484
     * @return string pattern
485
     */
486 2
    public function getRegexPattern()
487
    {
488 2
        return $this->regexPattern;
489
    }
490
491
    /**
492
     * set regex pattern
493
     *
494
     * @param string $regexPattern regex pattern
495
     *
496
     * @return void
497
     */
498
    public function setRegexPattern($regexPattern)
499
    {
500
        $this->regexPattern = $regexPattern;
501
    }
502
503
    /**
504
     * get DocumentClass
505
     *
506
     * @return string DocumentClass
507
     */
508 2
    public function getDocumentClass()
509
    {
510 2
        return $this->documentClass;
511
    }
512
513
    /**
514
     * set DocumentClass
515
     *
516
     * @param string $documentClass documentClass
517
     *
518
     * @return void
519
     */
520 2
    public function setDocumentClass($documentClass)
521
    {
522 2
        $this->documentClass = $documentClass;
523 2
    }
524
525
    /**
526
     * get Constraints
527
     *
528
     * @return mixed Constraints
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
529
     */
530 2
    public function getConstraints()
531
    {
532 2
        return $this->constraints;
533
    }
534
535
    /**
536
     * set Constraints
537
     *
538
     * @param mixed $constraints constraints
539
     *
540
     * @return void
541
     */
542
    public function setConstraints($constraints)
543
    {
544
        $this->constraints = $constraints;
0 ignored issues
show
Documentation Bug introduced by
It seems like $constraints of type * is incompatible with the declared type array<integer,string> of property $constraints.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
545
    }
546
547
    /**
548
     * add a constraint
549
     *
550
     * @param string $name constraint name
551
     *
552
     * @return void
553
     */
554
    public function addConstraint($name)
555
    {
556
        $this->constraints[] = $name;
557
    }
558
559
    /**
560
     * set items
561
     *
562
     * @param Schema $items items schema
563
     *
564
     * @return void
565
     */
566 2
    public function setItems($items)
567
    {
568 2
        $this->items = $items;
569 2
    }
570
571
    /**
572
     * get items
573
     *
574
     * @return Schema
575
     */
576 2
    public function getItems()
577
    {
578 2
        return $this->items;
579
    }
580
581
    /**
582
     * add a property
583
     *
584
     * @param string $name     property name
585
     * @param Schema $property property
586
     *
587
     * @return void
588
     */
589 2
    public function addProperty($name, $property)
590
    {
591 2
        $this->properties->set($name, $property);
592 2
    }
593
594
    /**
595
     * removes a property
596
     *
597
     * @param string $name property name
598
     *
599
     * @return void
600
     */
601 2
    public function removeProperty($name)
602
    {
603 2
        if (!$this->properties->containsKey($name)) {
604
            $this->properties->remove($this->properties->get($name));
605
        }
606 2
    }
607
608
    /**
609
     * returns a property
610
     *
611
     * @param string $name property name
612
     *
613
     * @return void|Schema property
614
     */
615
    public function getProperty($name)
616
    {
617
        return $this->properties->get($name);
618
    }
619
620
    /**
621
     * get properties
622
     *
623
     * @return ArrayCollection|null
624
     */
625 2
    public function getProperties()
626
    {
627 2
        if ($this->properties->isEmpty()) {
628 2
            return null;
629
        } else {
630 2
            return $this->properties;
631
        }
632
    }
633
634
    /**
635
     * set additionalProperties on schema
636
     *
637
     * @param SchemaAdditionalProperties $additionalProperties additional properties
638
     *
639
     * @return void
640
     */
641 2
    public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties)
642
    {
643 2
        $this->additionalProperties = $additionalProperties;
644 2
    }
645
646
    /**
647
     * get addtionalProperties for schema
648
     *
649
     * @return SchemaAdditionalProperties
650
     */
651 2
    public function getAdditionalProperties()
652
    {
653 2
        return $this->additionalProperties;
654
    }
655
656
    /**
657
     * set required variables
658
     *
659
     * @param string[] $required array of required fields
660
     *
661
     * @return void
662
     */
663 2
    public function setRequired(array $required)
664
    {
665
        // needed as indexes could we off and we want to enforce an array after json_encode
666 2
        $this->required = array_values($required);
667 2
    }
668
669
    /**
670
     * get required fields
671
     *
672
     * @return string[]|null
673
     */
674 2
    public function getRequired()
675
    {
676 2
        $required = $this->required;
677 2
        if (empty($required)) {
678 2
            $required = null;
679 1
        }
680
681 2
        return $required;
682
    }
683
684
    /**
685
     * set translatable flag
686
     *
687
     * This flag is a local extension to json schema.
688
     *
689
     * @param boolean $translatable translatable flag
690
     *
691
     * @return void
692
     */
693
    public function setTranslatable($translatable)
694
    {
695
        if ($translatable === true) {
696
            $this->setType('translatable');
697
        } else {
698
            $this->setType('string');
699
        }
700
    }
701
702
    /**
703
     * get translatable flag
704
     *
705
     * @return boolean
706
     */
707
    public function isTranslatable()
708
    {
709
        $ret = false;
710
        if ($this->getFormat() == 'translatable') {
711
            $ret = true;
712
        }
713
714
        return $ret;
715
    }
716
717
    /**
718
     * set a array of urls that can extref refer to
719
     *
720
     * @param array $refCollection urls
721
     *
722
     * @return void
723
     */
724
    public function setRefCollection(array $refCollection)
725
    {
726
        $this->refCollection = $refCollection;
727
    }
728
729
    /**
730
     * get a collection of urls that can extref refer to
731
     *
732
     * @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...
733
     */
734 2
    public function getRefCollection()
735
    {
736 2
        $collection = $this->refCollection;
737 2
        if (empty($collection)) {
738 2
            $collection = null;
739 1
        }
740
741 2
        return $collection;
742
    }
743
744
    /**
745
     * set an array of possible event names
746
     *
747
     * @param array $eventNames event names
748
     *
749
     * @return void
750
     */
751 2
    public function setEventNames(array $eventNames)
752
    {
753 2
        $this->eventNames = array_values($eventNames);
754 2
    }
755
756
    /**
757
     * get a collection of possible event names
758
     *
759
     * @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...
760
     */
761 2
    public function getEventNames()
762
    {
763 2
        $collection = $this->eventNames;
764 2
        if (empty($collection)) {
765 2
            $collection = null;
766 1
        }
767
768 2
        return $collection;
769
    }
770
771
    /**
772
     * Set the readOnly flag
773
     *
774
     * @param bool $readOnly ReadOnly flag
775
     *
776
     * @return void
777
     */
778 2
    public function setReadOnly($readOnly)
779
    {
780 2
        $this->readOnly = (bool) $readOnly;
781 2
    }
782
783
    /**
784
     * Get the readOnly flag.
785
     * Returns null if the flag is set to false so the serializer will ignore it.
786
     *
787
     * @return bool|null true if readOnly isset to true or null if not
788
     */
789 2
    public function getReadOnly()
790
    {
791 2
        return $this->readOnly ? true : null;
792
    }
793
794
    /**
795
     * set searchable variables
796
     *
797
     * @param string[] $searchable array of searchable fields
798
     *
799
     * @return void
800
     */
801 2
    public function setSearchable($searchable)
802
    {
803 2
        $this->searchable = $searchable;
804 2
    }
805
806
    /**
807
     * get searchable fields
808
     *
809
     * @return string[]|null
810
     */
811 2
    public function getSearchable()
812
    {
813 2
        if (empty($this->searchable)) {
814 2
            return null;
815
        }
816
        return $this->searchable;
817
    }
818
}
819