Schema::setMinItems()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
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  https://opensource.org/licenses/MIT MIT 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 bool
93
     */
94
    protected $isVersioning;
95
96
    /**
97
     * @var string[]
98
     */
99
    protected $searchable = [];
100
101
    /**
102
     * @var int
103
     */
104
    protected $minLength;
105
106
    /**
107
     * @var int
108
     */
109
    protected $maxLength;
110
111
    /**
112
     * @var int
113
     */
114
    protected $minItems;
115
116
    /**
117
     * @var int
118
     */
119
    protected $maxItems;
120
121
    /**
122
     * @var float
123
     */
124
    protected $numericMinimum;
125
126
    /**
127
     * @var float
128
     */
129
    protected $numericMaximum;
130
131
    /**
132
     * @var SchemaEnum
133
     */
134
    protected $enum;
135
136
    /**
137
     * @var string
138
     */
139
    protected $regexPattern;
140
141
    /**
142
     * @var string
143
     */
144
    protected $documentClass;
145
146
    /**
147
     * @var array<string>
148
     */
149
    protected $groups;
150
151
    /**
152
     * @var array<string>
153
     */
154
    protected $constraints;
155
156
    /**
157
     * @var array
158
     */
159
    protected $variations;
160
161
    /**
162
     * @var array
163
     */
164
    protected $onVariation;
165
166
    /**
167
     * @var array<string>
168
     */
169
    protected $textIndexes;
170
171
    /**
172
     * these are the BSON primitive types.
173
     * http://json-schema.org/latest/json-schema-core.html#anchor8
174
     * every type set *not* in this set will be carried over to 'format'
175
     *
176
     * @var string[]
177
     */
178
    protected $primitiveTypes = [
179
        'array',
180
        'boolean',
181
        'integer',
182
        'number',
183
        'null',
184
        'object',
185
        'string'
186
    ];
187
188
    /**
189
     * those are types that when they are required, a minimal length
190
     * shall be specified in schema (or allow null if not required; that will lead
191
     * to the inclusion of "null" in the "type" property array)
192
     *
193
     * @var array
194
     */
195
    protected $minLengthTypes = [
196
        'integer',
197
        'number',
198
        'float',
199
        'double',
200
        'decimal',
201
        'string',
202
        'date',
203
        'extref',
204
        'translatable'
205
    ];
206
207
    /**
208
     * known non-primitive types we map to primitives here.
209
     * the type itself is set to the format.
210
     *
211
     * @var string[]
212
     */
213
    protected $specialTypeMapping = [
214
        'extref' => 'string',
215
        'translatable' => 'object',
216
        'date' => 'string',
217
        'float' => 'number',
218
        'double' => 'number',
219
        'decimal' => 'number'
220
    ];
221
222
    protected $formatOverrides = [
223
        'date' => 'date-time'
224
    ];
225
226
    /**
227
     * Build properties
228
     */
229 2
    public function __construct()
230
    {
231 2
        $this->properties = new ArrayCollection();
232 2
    }
233
234
    /**
235
     * set title
236
     *
237
     * @param string $title title
238
     *
239
     * @return void
240
     */
241 2
    public function setTitle($title)
242
    {
243 2
        $this->title = $title;
244 2
    }
245
246
    /**
247
     * get title
248
     *
249
     * @return string
250
     */
251 2
    public function getTitle()
252
    {
253 2
        return $this->title;
254
    }
255
256
    /**
257
     * set description
258
     *
259
     * @param string $description description
260
     *
261
     * @return void
262
     */
263
    public function setDescription($description)
264
    {
265
        $this->description = $description;
266
    }
267
268
    /**
269
     * get description
270
     *
271
     * @return string
272
     */
273
    public function getDescription()
274
    {
275
        return $this->description;
276
    }
277
278
    /**
279
     * set type
280
     *
281
     * @param string|array $types types
282
     *
283
     * @return void
284
     */
285
    public function setType($types)
286
    {
287
        if (!is_array($types)) {
288
            $types = [$types];
289
        }
290
291
        $typesToSet = [];
292
        foreach ($types as $type) {
293
            if ($type === 'int') {
294
                $type = 'integer';
295
            }
296
            if ($type === 'hash') {
297
                $type = 'object';
298
            }
299
300
            // handle non-primitive types
301
            if (!in_array($type, $this->primitiveTypes)) {
302
                $setType = 'string';
303
                if (isset($this->specialTypeMapping[$type])) {
304
                    $setType = $this->specialTypeMapping[$type];
305
                }
306
                $typesToSet[] = $setType;
307
308
                if (isset($this->formatOverrides[$type])) {
309
                    $type = $this->formatOverrides[$type];
310
                }
311
312
                $this->setFormat($type);
313
            } else {
314
                $typesToSet[] = $type;
315
            }
316
        }
317
318
        $this->type = new SchemaType($typesToSet);
319
    }
320
321
    /**
322
     * get type
323
     *
324
     * @return SchemaType type
325
     */
326
    public function getType()
327
    {
328
        return $this->type;
329
    }
330
331
    /**
332
     * get MinLengthTypes
333
     *
334
     * @return array MinLengthTypes
335
     */
336
    public function getMinLengthTypes()
337
    {
338
        return $this->minLengthTypes;
339
    }
340
341
    /**
342
     * get format
343
     *
344
     * @return string format
345
     */
346
    public function getFormat()
347
    {
348
        return $this->format;
349
    }
350
351
    /**
352
     * sets format
353
     *
354
     * @param string $format format
355
     *
356
     * @return void
357
     */
358
    public function setFormat($format)
359
    {
360
        $this->format = $format;
361
    }
362
363
    /**
364
     * get numeric minimum
365
     *
366
     * @return float numeric minimum
367
     */
368
    public function getNumericMinimum()
369
    {
370
        return $this->numericMinimum;
371
    }
372
373
    /**
374
     * set numeric minimum
375
     *
376
     * @param float $numericMinimum numeric mimimum
377
     *
378
     * @return void
379
     */
380
    public function setNumericMinimum($numericMinimum)
381
    {
382
        $this->numericMinimum = $numericMinimum;
383
    }
384
385
    /**
386
     * get numeric maximum
387
     *
388
     * @return float numeric maximum
389
     */
390
    public function getNumericMaximum()
391
    {
392
        return $this->numericMaximum;
393
    }
394
395
    /**
396
     * set numeric maximum
397
     *
398
     * @param float $numericMaximum maximum
399
     *
400
     * @return void
401
     */
402
    public function setNumericMaximum($numericMaximum)
403
    {
404
        $this->numericMaximum = $numericMaximum;
405
    }
406
407
    /**
408
     * set min length
409
     *
410
     * @return int length
411
     */
412
    public function getMinLength()
413
    {
414
        return $this->minLength;
415
    }
416
417
    /**
418
     * get min length
419
     *
420
     * @param int $minLength length
421
     *
422
     * @return void
423
     */
424
    public function setMinLength($minLength)
425
    {
426
        $this->minLength = $minLength;
427
    }
428
429
    /**
430
     * gets maxlength
431
     *
432
     * @return int length
433
     */
434
    public function getMaxLength()
435
    {
436
        return $this->maxLength;
437
    }
438
439
    /**
440
     * set maxlength
441
     *
442
     * @param int $maxLength length
443
     *
444
     * @return void
445
     */
446
    public function setMaxLength($maxLength)
447
    {
448
        $this->maxLength = $maxLength;
449
    }
450
451
    /**
452
     * set min Items
453
     *
454
     * @return int Items
455
     */
456
    public function getMinItems()
457
    {
458
        return $this->minItems;
459
    }
460
461
    /**
462
     * get min Items
463
     *
464
     * @param int $minItems length
465
     *
466
     * @return void
467
     */
468
    public function setMinItems($minItems)
469
    {
470
        $this->minItems = $minItems;
471
    }
472
473
    /**
474
     * gets maxItems
475
     *
476
     * @return int Items
477
     */
478
    public function getMaxItems()
479
    {
480
        return $this->maxItems;
481
    }
482
483
    /**
484
     * set maxItems
485
     *
486
     * @param int $maxItems Items
487
     *
488
     * @return void
489
     */
490
    public function setMaxItems($maxItems)
491
    {
492
        $this->maxItems = $maxItems;
493
    }
494
495
    /**
496
     * get Enum
497
     *
498
     * @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...
499
     */
500
    public function getEnum()
501
    {
502
        return $this->enum;
503
    }
504
505
    /**
506
     * set Enum
507
     *
508
     * @param array $enum enum
509
     *
510
     * @return void
511
     */
512
    public function setEnum(array $enum)
513
    {
514
        $this->enum = new SchemaEnum($enum);
515
    }
516
517
    /**
518
     * get regex pattern
519
     *
520
     * @return string pattern
521
     */
522
    public function getRegexPattern()
523
    {
524
        return $this->regexPattern;
525
    }
526
527
    /**
528
     * set regex pattern
529
     *
530
     * @param string $regexPattern regex pattern
531
     *
532
     * @return void
533
     */
534
    public function setRegexPattern($regexPattern)
535
    {
536
        $this->regexPattern = $regexPattern;
537
    }
538
539
    /**
540
     * get DocumentClass
541
     *
542
     * @return string DocumentClass
543
     */
544
    public function getDocumentClass()
545
    {
546
        return $this->documentClass;
547
    }
548
549
    /**
550
     * set DocumentClass
551
     *
552
     * @param string $documentClass documentClass
553
     *
554
     * @return void
555
     */
556
    public function setDocumentClass($documentClass)
557
    {
558
        $this->documentClass = $documentClass;
559
    }
560
561
    /**
562
     * get Groups
563
     *
564
     * @return array Groups
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...
565
     */
566
    public function getGroups()
567
    {
568
        return $this->groups;
569
    }
570
571
    /**
572
     * set Groups
573
     *
574
     * @param array $groups groups
575
     *
576
     * @return void
577
     */
578
    public function setGroups($groups)
579
    {
580
        $this->groups = $groups;
581
    }
582
583
    /**
584
     * get Constraints
585
     *
586
     * @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...
587
     */
588
    public function getConstraints()
589
    {
590
        return $this->constraints;
591
    }
592
593
    /**
594
     * set Constraints
595
     *
596
     * @param mixed $constraints constraints
597
     *
598
     * @return void
599
     */
600
    public function setConstraints($constraints)
601
    {
602
        $this->constraints = $constraints;
603
    }
604
605
    /**
606
     * add a constraint
607
     *
608
     * @param string $name constraint name
609
     *
610
     * @return void
611
     */
612
    public function addConstraint($name)
613
    {
614
        $this->constraints[] = $name;
615
    }
616
617
    /**
618
     * set items
619
     *
620
     * @param Schema $items items schema
621
     *
622
     * @return void
623
     */
624
    public function setItems($items)
625
    {
626
        $this->items = $items;
627
    }
628
629
    /**
630
     * get items
631
     *
632
     * @return Schema
633
     */
634
    public function getItems()
635
    {
636
        return $this->items;
637
    }
638
639
    /**
640
     * add a property
641
     *
642
     * @param string $name     property name
643
     * @param Schema $property property
644
     *
645
     * @return void
646
     */
647
    public function addProperty($name, $property)
648
    {
649
        $this->properties->set($name, $property);
650
    }
651
652
    /**
653
     * removes a property
654
     *
655
     * @param string $name property name
656
     *
657
     * @return void
658
     */
659
    public function removeProperty($name)
660
    {
661
        if ($this->properties->containsKey($name)) {
662
            $this->properties->remove($name);
663
        }
664
    }
665
666
    /**
667
     * returns a property
668
     *
669
     * @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...
670
     *
671
     * @return null|Schema property
672
     */
673
    public function getProperty($name = null)
674
    {
675
        if (is_null($name)) {
676
            return null;
677
        }
678
        return $this->properties->get($name);
679
    }
680
681
    /**
682
     * get properties
683
     *
684
     * @return ArrayCollection|null
685
     */
686
    public function getProperties()
687
    {
688
        if ($this->properties->isEmpty()) {
689
            return null;
690
        } else {
691
            return $this->properties;
692
        }
693
    }
694
695
    /**
696
     * set additionalProperties on schema
697
     *
698
     * @param SchemaAdditionalProperties $additionalProperties additional properties
699
     *
700
     * @return void
701
     */
702
    public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties)
703
    {
704
        $this->additionalProperties = $additionalProperties;
705
    }
706
707
    /**
708
     * get addtionalProperties for schema
709
     *
710
     * @return SchemaAdditionalProperties
711
     */
712
    public function getAdditionalProperties()
713
    {
714
        return $this->additionalProperties;
715
    }
716
717
    /**
718
     * set required variables
719
     *
720
     * @param string[] $required array of required fields
721
     *
722
     * @return void
723
     */
724
    public function setRequired(array $required)
725
    {
726
        // needed as indexes could we off and we want to enforce an array after json_encode
727
        $this->required = array_values($required);
728
    }
729
730
    /**
731
     * get required fields
732
     *
733
     * @return string[]|null
734
     */
735
    public function getRequired()
736
    {
737
        $required = $this->required;
738
        if (empty($required)) {
739
            $required = null;
740
        }
741
742
        return $required;
743
    }
744
745
    /**
746
     * set translatable flag
747
     *
748
     * This flag is a local extension to json schema.
749
     *
750
     * @param boolean $translatable translatable flag
751
     *
752
     * @return void
753
     */
754
    public function setTranslatable($translatable)
755
    {
756
        if ($translatable === true) {
757
            $this->setType('translatable');
758
        } else {
759
            $this->setType('string');
760
        }
761
    }
762
763
    /**
764
     * get translatable flag
765
     *
766
     * @return boolean
767
     */
768
    public function isTranslatable()
769
    {
770
        $ret = false;
771
        if ($this->getFormat() == 'translatable') {
772
            $ret = true;
773
        }
774
775
        return $ret;
776
    }
777
778
    /**
779
     * set a array of urls that can extref refer to
780
     *
781
     * @param array $refCollection urls
782
     *
783
     * @return void
784
     */
785
    public function setRefCollection(array $refCollection)
786
    {
787
        $this->refCollection = $refCollection;
788
    }
789
790
    /**
791
     * get a collection of urls that can extref refer to
792
     *
793
     * @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...
794
     */
795
    public function getRefCollection()
796
    {
797
        $collection = $this->refCollection;
798
        if (empty($collection)) {
799
            $collection = null;
800
        }
801
802
        return $collection;
803
    }
804
805
    /**
806
     * set an array of possible event names
807
     *
808
     * @param array $eventNames event names
809
     *
810
     * @return void
811
     */
812
    public function setEventNames(array $eventNames)
813
    {
814
        $this->eventNames = array_values($eventNames);
815
    }
816
817
    /**
818
     * get a collection of possible event names
819
     *
820
     * @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...
821
     */
822
    public function getEventNames()
823
    {
824
        $collection = $this->eventNames;
825
        if (empty($collection)) {
826
            $collection = null;
827
        }
828
829
        return $collection;
830
    }
831
832
    /**
833
     * Set the readOnly flag
834
     *
835
     * @param bool $readOnly ReadOnly flag
836
     *
837
     * @return void
838
     */
839
    public function setReadOnly($readOnly)
840
    {
841
        $this->readOnly = (bool) $readOnly;
842
    }
843
844
    /**
845
     * Get the readOnly flag.
846
     * Returns null if the flag is set to false so the serializer will ignore it.
847
     *
848
     * @return bool|null true if readOnly isset to true or null if not
849
     */
850
    public function getReadOnly()
851
    {
852
        return $this->readOnly ? true : null;
853
    }
854
855
    /**
856
     * get RecordOriginModifiable
857
     *
858
     * @return boolean RecordOriginModifiable
859
     */
860
    public function isRecordOriginModifiable()
861
    {
862
        return $this->recordOriginModifiable;
863
    }
864
865
    /**
866
     * set RecordOriginModifiable
867
     *
868
     * @param boolean $recordOriginModifiable recordOriginModifiable
869
     *
870
     * @return void
871
     */
872
    public function setRecordOriginModifiable($recordOriginModifiable)
873
    {
874
        $this->recordOriginModifiable = $recordOriginModifiable;
875
    }
876
877
    /**
878
     * get RecordOriginException
879
     *
880
     * @return boolean RecordOriginException
881
     */
882
    public function isRecordOriginException()
883
    {
884
        return $this->recordOriginException;
885
    }
886
887
    /**
888
     * set RecordOriginException
889
     *
890
     * @param boolean $recordOriginException recordOriginException
891
     *
892
     * @return void
893
     */
894
    public function setRecordOriginException($recordOriginException)
895
    {
896
        $this->recordOriginException = $recordOriginException;
897
    }
898
899
    /**
900
     * isVersioning
901
     *
902
     * @return bool IsVersioning
903
     */
904
    public function isVersioning()
905
    {
906
        return $this->isVersioning;
907
    }
908
909
    /**
910
     * set IsVersioning
911
     *
912
     * @param bool $isVersioning isVersioning
913
     *
914
     * @return void
915
     */
916
    public function setIsVersioning($isVersioning)
917
    {
918
        $this->isVersioning = $isVersioning;
919
    }
920
921
    /**
922
     * set searchable variables
923
     *
924
     * @param string[] $searchable array of searchable fields
925
     *
926
     * @return void
927
     */
928
    public function setSearchable($searchable)
929
    {
930
        $this->searchable = $searchable;
931
    }
932
933
    /**
934
     * get searchable fields
935
     *
936
     * @return string[]|null
937
     */
938
    public function getSearchable()
939
    {
940
        if (empty($this->searchable)) {
941
            return null;
942
        }
943
        return $this->searchable;
944
    }
945
946
    /**
947
     * get Variations
948
     *
949
     * @return array Variations
950
     */
951
    public function getVariations()
952
    {
953
        return $this->variations;
954
    }
955
956
    /**
957
     * set Variations
958
     *
959
     * @param array $variations variations
960
     *
961
     * @return void
962
     */
963
    public function setVariations($variations)
964
    {
965
        $this->variations = $variations;
966
    }
967
968
    /**
969
     * get OnVariation
970
     *
971
     * @return array OnVariation
972
     */
973
    public function getOnVariation()
974
    {
975
        return $this->onVariation;
976
    }
977
978
    /**
979
     * set OnVariation
980
     *
981
     * @param array $onVariation onVariation
982
     *
983
     * @return void
984
     */
985
    public function setOnVariation($onVariation)
986
    {
987
        $this->onVariation = $onVariation;
988
    }
989
990
    /**
991
     * @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...
992
     */
993
    public function getTextIndexes()
994
    {
995
        return $this->textIndexes;
996
    }
997
998
    /**
999
     * get textIndexes fields
1000
     *
1001
     * @param array $textIndexes Data array of special text search values
1002
     *
1003
     * @return void
1004
     */
1005
    public function setTextIndexes($textIndexes)
1006
    {
1007
        $this->textIndexes = $textIndexes;
1008
    }
1009
}
1010