Completed
Push — feature/EVO-7964_fundInfo-exte... ( 142d3d...cf9459 )
by Bastian
08:28 queued 08:21
created

Schema::setConstraints()   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 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
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
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 bool
83
     */
84
    protected $recordOriginModifiable;
85
86
    /**
87
     * @var bool
88
     */
89
    protected $recordOriginException;
90
91
    /**
92
     * @var string[]
93
     */
94
    protected $searchable = [];
95
96
    /**
97
     * @var int
98
     */
99
    protected $minLength;
100
101
    /**
102
     * @var int
103
     */
104
    protected $maxLength;
105
106
    /**
107
     * @var int
108
     */
109
    protected $minItems;
110
111
    /**
112
     * @var int
113
     */
114
    protected $maxItems;
115
116
    /**
117
     * @var float
118
     */
119
    protected $numericMinimum;
120
121
    /**
122
     * @var float
123
     */
124
    protected $numericMaximum;
125
126
    /**
127
     * @var SchemaEnum
128
     */
129
    protected $enum;
130
131
    /**
132
     * @var string
133
     */
134
    protected $regexPattern;
135
136
    /**
137
     * @var string
138
     */
139
    protected $documentClass;
140
141
    /**
142
     * @var array<string>
143
     */
144
    protected $constraints;
145
146
    /**
147
     * @var array<string>
148
     */
149
    protected $textIndexes;
150
151
    /**
152
     * these are the BSON primitive types.
153
     * http://json-schema.org/latest/json-schema-core.html#anchor8
154
     * every type set *not* in this set will be carried over to 'format'
155
     *
156
     * @var string[]
157
     */
158
    protected $primitiveTypes = [
159
        'array',
160
        'boolean',
161
        'integer',
162
        'number',
163
        'null',
164
        'object',
165
        'string'
166
    ];
167
168
    /**
169
     * those are types that when they are required, a minimal length
170
     * shall be specified in schema (or allow null if not required; that will lead
171
     * to the inclusion of "null" in the "type" property array)
172
     *
173
     * @var array
174
     */
175
    protected $minLengthTypes = [
176
        'integer',
177
        'number',
178
        'float',
179
        'double',
180
        'decimal',
181
        'string',
182
        'date',
183
        'extref'
184
    ];
185
186
    /**
187
     * known non-primitive types we map to primitives here.
188
     * the type itself is set to the format.
189
     *
190
     * @var string[]
191
     */
192
    protected $specialTypeMapping = [
193
        'extref' => 'string',
194
        'translatable' => 'object',
195
        'date' => 'string',
196
        'float' => 'number',
197
        'double' => 'number',
198
        'decimal' => 'number'
199
    ];
200
201
    protected $formatOverrides = [
202
        'date' => 'date-time'
203
    ];
204
205
    /**
206
     * Build properties
207
     */
208 2
    public function __construct()
209
    {
210 2
        $this->properties = new ArrayCollection();
211 2
    }
212
213
    /**
214
     * set title
215
     *
216
     * @param string $title title
217
     *
218
     * @return void
219
     */
220 2
    public function setTitle($title)
221
    {
222 2
        $this->title = $title;
223 2
    }
224
225
    /**
226
     * get title
227
     *
228
     * @return string
229
     */
230 2
    public function getTitle()
231
    {
232 2
        return $this->title;
233
    }
234
235
    /**
236
     * set description
237
     *
238
     * @param string $description description
239
     *
240
     * @return void
241
     */
242
    public function setDescription($description)
243
    {
244
        $this->description = $description;
245
    }
246
247
    /**
248
     * get description
249
     *
250
     * @return string
251
     */
252
    public function getDescription()
253
    {
254
        return $this->description;
255
    }
256
257
    /**
258
     * set type
259
     *
260
     * @param string|array $types types
261
     *
262
     * @return void
263
     */
264
    public function setType($types)
265
    {
266
        if (!is_array($types)) {
267
            $types = [$types];
268
        }
269
270
        $typesToSet = [];
271
        foreach ($types as $type) {
272
            if ($type === 'int') {
273
                $type = 'integer';
274
            }
275
            if ($type === 'hash') {
276
                $type = 'object';
277
            }
278
279
            // handle non-primitive types
280
            if (!in_array($type, $this->primitiveTypes)) {
281
                $setType = 'string';
282
                if (isset($this->specialTypeMapping[$type])) {
283
                    $setType = $this->specialTypeMapping[$type];
284
                }
285
                $typesToSet[] = $setType;
286
287
                if (isset($this->formatOverrides[$type])) {
288
                    $type = $this->formatOverrides[$type];
289
                }
290
291
                $this->setFormat($type);
292
            } else {
293
                $typesToSet[] = $type;
294
            }
295
        }
296
297
        $this->type = new SchemaType($typesToSet);
298
    }
299
300
    /**
301
     * get type
302
     *
303
     * @return SchemaType type
304
     */
305
    public function getType()
306
    {
307
        return $this->type;
308
    }
309
310
    /**
311
     * get MinLengthTypes
312
     *
313
     * @return array MinLengthTypes
314
     */
315
    public function getMinLengthTypes()
316
    {
317
        return $this->minLengthTypes;
318
    }
319
320
    /**
321
     * get format
322
     *
323
     * @return string format
324
     */
325
    public function getFormat()
326
    {
327
        return $this->format;
328
    }
329
330
    /**
331
     * sets format
332
     *
333
     * @param string $format format
334
     *
335
     * @return void
336
     */
337
    public function setFormat($format)
338
    {
339
        $this->format = $format;
340
    }
341
342
    /**
343
     * get numeric minimum
344
     *
345
     * @return float numeric minimum
346
     */
347
    public function getNumericMinimum()
348
    {
349
        return $this->numericMinimum;
350
    }
351
352
    /**
353
     * set numeric minimum
354
     *
355
     * @param float $numericMinimum numeric mimimum
356
     *
357
     * @return void
358
     */
359
    public function setNumericMinimum($numericMinimum)
360
    {
361
        $this->numericMinimum = $numericMinimum;
362
    }
363
364
    /**
365
     * get numeric maximum
366
     *
367
     * @return float numeric maximum
368
     */
369
    public function getNumericMaximum()
370
    {
371
        return $this->numericMaximum;
372
    }
373
374
    /**
375
     * set numeric maximum
376
     *
377
     * @param float $numericMaximum maximum
378
     *
379
     * @return void
380
     */
381
    public function setNumericMaximum($numericMaximum)
382
    {
383
        $this->numericMaximum = $numericMaximum;
384
    }
385
386
    /**
387
     * set min length
388
     *
389
     * @return int length
390
     */
391
    public function getMinLength()
392
    {
393
        return $this->minLength;
394
    }
395
396
    /**
397
     * get min length
398
     *
399
     * @param int $minLength length
400
     *
401
     * @return void
402
     */
403
    public function setMinLength($minLength)
404
    {
405
        $this->minLength = $minLength;
406
    }
407
408
    /**
409
     * gets maxlength
410
     *
411
     * @return int length
412
     */
413
    public function getMaxLength()
414
    {
415
        return $this->maxLength;
416
    }
417
418
    /**
419
     * set maxlength
420
     *
421
     * @param int $maxLength length
422
     *
423
     * @return void
424
     */
425
    public function setMaxLength($maxLength)
426
    {
427
        $this->maxLength = $maxLength;
428
    }
429
430
    /**
431
     * set min Items
432
     *
433
     * @return int Items
434
     */
435
    public function getMinItems()
436
    {
437
        return $this->minItems;
438
    }
439
440
    /**
441
     * get min Items
442
     *
443
     * @param int $minItems length
444
     *
445
     * @return void
446
     */
447
    public function setMinItems($minItems)
448
    {
449
        $this->minItems = $minItems;
450
    }
451
452
    /**
453
     * gets maxItems
454
     *
455
     * @return int Items
456
     */
457
    public function getMaxItems()
458
    {
459
        return $this->maxItems;
460
    }
461
462
    /**
463
     * set maxItems
464
     *
465
     * @param int $maxItems Items
466
     *
467
     * @return void
468
     */
469
    public function setMaxItems($maxItems)
470
    {
471
        $this->maxItems = $maxItems;
472
    }
473
474
    /**
475
     * get Enum
476
     *
477
     * @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...
478
     */
479
    public function getEnum()
480
    {
481
        return $this->enum;
482
    }
483
484
    /**
485
     * set Enum
486
     *
487
     * @param array $enum enum
488
     *
489
     * @return void
490
     */
491
    public function setEnum(array $enum)
492
    {
493
        $this->enum = new SchemaEnum($enum);
494
    }
495
496
    /**
497
     * get regex pattern
498
     *
499
     * @return string pattern
500
     */
501
    public function getRegexPattern()
502
    {
503
        return $this->regexPattern;
504
    }
505
506
    /**
507
     * set regex pattern
508
     *
509
     * @param string $regexPattern regex pattern
510
     *
511
     * @return void
512
     */
513
    public function setRegexPattern($regexPattern)
514
    {
515
        $this->regexPattern = $regexPattern;
516
    }
517
518
    /**
519
     * get DocumentClass
520
     *
521
     * @return string DocumentClass
522
     */
523
    public function getDocumentClass()
524
    {
525
        return $this->documentClass;
526
    }
527
528
    /**
529
     * set DocumentClass
530
     *
531
     * @param string $documentClass documentClass
532
     *
533
     * @return void
534
     */
535
    public function setDocumentClass($documentClass)
536
    {
537
        $this->documentClass = $documentClass;
538
    }
539
540
    /**
541
     * get Constraints
542
     *
543
     * @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...
544
     */
545
    public function getConstraints()
546
    {
547
        return $this->constraints;
548
    }
549
550
    /**
551
     * set Constraints
552
     *
553
     * @param mixed $constraints constraints
554
     *
555
     * @return void
556
     */
557
    public function setConstraints($constraints)
558
    {
559
        $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...
560
    }
561
562
    /**
563
     * add a constraint
564
     *
565
     * @param string $name constraint name
566
     *
567
     * @return void
568
     */
569
    public function addConstraint($name)
570
    {
571
        $this->constraints[] = $name;
572
    }
573
574
    /**
575
     * set items
576
     *
577
     * @param Schema $items items schema
578
     *
579
     * @return void
580
     */
581
    public function setItems($items)
582
    {
583
        $this->items = $items;
584
    }
585
586
    /**
587
     * get items
588
     *
589
     * @return Schema
590
     */
591
    public function getItems()
592
    {
593
        return $this->items;
594
    }
595
596
    /**
597
     * add a property
598
     *
599
     * @param string $name     property name
600
     * @param Schema $property property
601
     *
602
     * @return void
603
     */
604
    public function addProperty($name, $property)
605
    {
606
        $this->properties->set($name, $property);
607
    }
608
609
    /**
610
     * removes a property
611
     *
612
     * @param string $name property name
613
     *
614
     * @return void
615
     */
616
    public function removeProperty($name)
617
    {
618
        if (!$this->properties->containsKey($name)) {
619
            $this->properties->remove($this->properties->get($name));
620
        }
621
    }
622
623
    /**
624
     * returns a property
625
     *
626
     * @param string $name property name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $name not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
627
     *
628
     * @return null|Schema property
629
     */
630
    public function getProperty($name = null)
631
    {
632
        if (is_null($name)) {
633
            return null;
634
        }
635
        return $this->properties->get($name);
636
    }
637
638
    /**
639
     * get properties
640
     *
641
     * @return ArrayCollection|null
642
     */
643
    public function getProperties()
644
    {
645
        if ($this->properties->isEmpty()) {
646
            return null;
647
        } else {
648
            return $this->properties;
649
        }
650
    }
651
652
    /**
653
     * set additionalProperties on schema
654
     *
655
     * @param SchemaAdditionalProperties $additionalProperties additional properties
656
     *
657
     * @return void
658
     */
659
    public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties)
660
    {
661
        $this->additionalProperties = $additionalProperties;
662
    }
663
664
    /**
665
     * get addtionalProperties for schema
666
     *
667
     * @return SchemaAdditionalProperties
668
     */
669
    public function getAdditionalProperties()
670
    {
671
        return $this->additionalProperties;
672
    }
673
674
    /**
675
     * set required variables
676
     *
677
     * @param string[] $required array of required fields
678
     *
679
     * @return void
680
     */
681
    public function setRequired(array $required)
682
    {
683
        // needed as indexes could we off and we want to enforce an array after json_encode
684
        $this->required = array_values($required);
685
    }
686
687
    /**
688
     * get required fields
689
     *
690
     * @return string[]|null
691
     */
692
    public function getRequired()
693
    {
694
        $required = $this->required;
695
        if (empty($required)) {
696
            $required = null;
697
        }
698
699
        return $required;
700
    }
701
702
    /**
703
     * set translatable flag
704
     *
705
     * This flag is a local extension to json schema.
706
     *
707
     * @param boolean $translatable translatable flag
708
     *
709
     * @return void
710
     */
711
    public function setTranslatable($translatable)
712
    {
713
        if ($translatable === true) {
714
            $this->setType('translatable');
715
        } else {
716
            $this->setType('string');
717
        }
718
    }
719
720
    /**
721
     * get translatable flag
722
     *
723
     * @return boolean
724
     */
725
    public function isTranslatable()
726
    {
727
        $ret = false;
728
        if ($this->getFormat() == 'translatable') {
729
            $ret = true;
730
        }
731
732
        return $ret;
733
    }
734
735
    /**
736
     * set a array of urls that can extref refer to
737
     *
738
     * @param array $refCollection urls
739
     *
740
     * @return void
741
     */
742
    public function setRefCollection(array $refCollection)
743
    {
744
        $this->refCollection = $refCollection;
745
    }
746
747
    /**
748
     * get a collection of urls that can extref refer to
749
     *
750
     * @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...
751
     */
752
    public function getRefCollection()
753
    {
754
        $collection = $this->refCollection;
755
        if (empty($collection)) {
756
            $collection = null;
757
        }
758
759
        return $collection;
760
    }
761
762
    /**
763
     * set an array of possible event names
764
     *
765
     * @param array $eventNames event names
766
     *
767
     * @return void
768
     */
769
    public function setEventNames(array $eventNames)
770
    {
771
        $this->eventNames = array_values($eventNames);
772
    }
773
774
    /**
775
     * get a collection of possible event names
776
     *
777
     * @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...
778
     */
779
    public function getEventNames()
780
    {
781
        $collection = $this->eventNames;
782
        if (empty($collection)) {
783
            $collection = null;
784
        }
785
786
        return $collection;
787
    }
788
789
    /**
790
     * Set the readOnly flag
791
     *
792
     * @param bool $readOnly ReadOnly flag
793
     *
794
     * @return void
795
     */
796
    public function setReadOnly($readOnly)
797
    {
798
        $this->readOnly = (bool) $readOnly;
799
    }
800
801
    /**
802
     * Get the readOnly flag.
803
     * Returns null if the flag is set to false so the serializer will ignore it.
804
     *
805
     * @return bool|null true if readOnly isset to true or null if not
806
     */
807
    public function getReadOnly()
808
    {
809
        return $this->readOnly ? true : null;
810
    }
811
812
    /**
813
     * get RecordOriginModifiable
814
     *
815
     * @return boolean RecordOriginModifiable
816
     */
817
    public function isRecordOriginModifiable()
818
    {
819
        return $this->recordOriginModifiable;
820
    }
821
822
    /**
823
     * set RecordOriginModifiable
824
     *
825
     * @param boolean $recordOriginModifiable recordOriginModifiable
826
     *
827
     * @return void
828
     */
829
    public function setRecordOriginModifiable($recordOriginModifiable)
830
    {
831
        $this->recordOriginModifiable = $recordOriginModifiable;
832
    }
833
834
    /**
835
     * get RecordOriginException
836
     *
837
     * @return boolean RecordOriginException
838
     */
839
    public function isRecordOriginException()
840
    {
841
        return $this->recordOriginException;
842
    }
843
844
    /**
845
     * set RecordOriginException
846
     *
847
     * @param boolean $recordOriginException recordOriginException
848
     *
849
     * @return void
850
     */
851
    public function setRecordOriginException($recordOriginException)
852
    {
853
        $this->recordOriginException = $recordOriginException;
854
    }
855
856
    /**
857
     * set searchable variables
858
     *
859
     * @param string[] $searchable array of searchable fields
860
     *
861
     * @return void
862
     */
863
    public function setSearchable($searchable)
864
    {
865
        $this->searchable = $searchable;
866
    }
867
868
    /**
869
     * get searchable fields
870
     *
871
     * @return string[]|null
872
     */
873
    public function getSearchable()
874
    {
875
        if (empty($this->searchable)) {
876
            return null;
877
        }
878
        return $this->searchable;
879
    }
880
881
    /**
882
     * @return array
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...
883
     */
884
    public function getTextIndexes()
885
    {
886
        return $this->textIndexes;
887
    }
888
889
    /**
890
     * get textIndexes fields
891
     *
892
     * @param array $textIndexes Data array of special text search values
893
     *
894
     * @return void
895
     */
896
    public function setTextIndexes($textIndexes)
897
    {
898
        $this->textIndexes = $textIndexes;
899
    }
900
}
901