Completed
Push — master ( 714b6a...f9d007 )
by Narcotic
06:13
created

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