Completed
Push — feature/EVO-6307-record-origin... ( 2c1fcb )
by Narcotic
61:45
created

Schema::setRecordOriginException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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