Completed
Push — feature/EVO-9770-doc-version-c... ( 9971a0...81e34e )
by Narcotic
08:19
created

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