Completed
Push — feature/EVO-5751-mongodb-3.x ( 5b7d5c...7088ec )
by Lucas
38:47 queued 22:41
created

Schema::setTextIndexes()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Graviton Schema Document
4
 */
5
6
namespace Graviton\SchemaBundle\Document;
7
8
use \Doctrine\Common\Collections\ArrayCollection;
9
10
/**
11
 * Graviton\SchemaBundle\Document\Schema
12
 *
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class Schema
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $title;
23
24
    /**
25
     * @var string
26
     */
27
    protected $description;
28
29
    /**
30
     * @var SchemaType
31
     */
32
    protected $type;
33
34
    /**
35
     * @var string
36
     */
37
    protected $format;
38
39
    /**
40
     * @var Schema
41
     */
42
    protected $items;
43
44
    /**
45
     * @var ArrayCollection
46
     */
47
    protected $properties;
48
49
    /**
50
     * @var SchemaAdditionalProperties
51
     */
52
    protected $additionalProperties;
53
54
    /**
55
     * @var string[]
56
     */
57
    protected $required = [];
58
59
    /**
60
     * @var boolean
61
     */
62
    protected $translatable;
63
64
    /**
65
     * @var array
66
     */
67
    protected $refCollection = [];
68
69
    /**
70
     * possible event names this collection implements (queue events)
71
     *
72
     * @var array
73
     */
74
    protected $eventNames = [];
75
76
    /**
77
     * @var bool
78
     */
79
    protected $readOnly = false;
80
81
    /**
82
     * @var bool
83
     */
84
    protected $recordOriginModifiable;
85
86
    /**
87
     * @var bool
88
     */
89
    protected $recordOriginException;
90
91
    /**
92
     * @var string[]
93
     */
94
    protected $searchable = [];
95
96
    /**
97
     * @var int
98
     */
99
    protected $minLength;
100
101
    /**
102
     * @var int
103
     */
104
    protected $maxLength;
105
106
    /**
107
     * @var int
108
     */
109
    protected $minItems;
110
111
    /**
112
     * @var int
113
     */
114
    protected $maxItems;
115
116
    /**
117
     * @var float
118
     */
119
    protected $numericMinimum;
120
121
    /**
122
     * @var float
123
     */
124
    protected $numericMaximum;
125
126
    /**
127
     * @var SchemaEnum
128
     */
129
    protected $enum;
130
131
    /**
132
     * @var string
133
     */
134
    protected $regexPattern;
135
136
    /**
137
     * @var string
138
     */
139
    protected $documentClass;
140
141
    /**
142
     * @var array<string>
143
     */
144
    protected $constraints;
145
146
    /**
147
     * @var array<string>
148
     */
149
    protected $textIndexes;
150
151
    /**
152
     * these are the BSON primitive types.
153
     * http://json-schema.org/latest/json-schema-core.html#anchor8
154
     * every type set *not* in this set will be carried over to 'format'
155
     *
156
     * @var string[]
157
     */
158
    protected $primitiveTypes = [
159
        'array',
160
        'boolean',
161
        'integer',
162
        'number',
163
        'null',
164
        'object',
165
        'string'
166
    ];
167
168
    /**
169
     * those are types that when they are required, a minimal length
170
     * shall be specified in schema (or allow null if not required; that will lead
171
     * to the inclusion of "null" in the "type" property array)
172
     *
173
     * @var array
174
     */
175
    protected $minLengthTypes = [
176
        'integer',
177
        'number',
178
        'float',
179
        'double',
180
        'decimal',
181
        'string',
182
        'date',
183
        'extref'
184
    ];
185
186
    /**
187
     * known non-primitive types we map to primitives here.
188
     * the type itself is set to the format.
189
     *
190
     * @var string[]
191
     */
192
    protected $specialTypeMapping = [
193
        'extref' => 'string',
194
        'translatable' => 'object',
195
        'date' => 'string',
196
        'float' => 'number',
197
        'double' => 'number',
198
        'decimal' => 'number'
199
    ];
200
201
    protected $formatOverrides = [
202
        'date' => 'date-time'
203
    ];
204
205
    /**
206
     * Build properties
207
     */
208 2
    public function __construct()
209
    {
210 2
        $this->properties = new ArrayCollection();
211 2
    }
212
213
    /**
214
     * set title
215
     *
216
     * @param string $title title
217
     *
218
     * @return void
219
     */
220 2
    public function setTitle($title)
221
    {
222 2
        $this->title = $title;
223 2
    }
224
225
    /**
226
     * get title
227
     *
228
     * @return string
229
     */
230 2
    public function getTitle()
231
    {
232 2
        return $this->title;
233
    }
234
235
    /**
236
     * set description
237
     *
238
     * @param string $description description
239
     *
240
     * @return void
241
     */
242
    public function setDescription($description)
243
    {
244
        $this->description = $description;
245
    }
246
247
    /**
248
     * get description
249
     *
250
     * @return string
251
     */
252
    public function getDescription()
253
    {
254
        return $this->description;
255
    }
256
257
    /**
258
     * set type
259
     *
260
     * @param string|array $types types
261
     *
262
     * @return void
263
     */
264
    public function setType($types)
265
    {
266
        if (!is_array($types)) {
267
            $types = [$types];
268
        }
269
270
        $typesToSet = [];
271
        foreach ($types as $type) {
272
            if ($type === 'int') {
273
                $type = 'integer';
274
            }
275
            if ($type === 'hash') {
276
                $type = 'object';
277
            }
278
279
            // handle non-primitive types
280
            if (!in_array($type, $this->primitiveTypes)) {
281
                $setType = 'string';
282
                if (isset($this->specialTypeMapping[$type])) {
283
                    $setType = $this->specialTypeMapping[$type];
284
                }
285
                $typesToSet[] = $setType;
286
287
                if (isset($this->formatOverrides[$type])) {
288
                    $type = $this->formatOverrides[$type];
289
                }
290
291
                $this->setFormat($type);
292
            } else {
293
                $typesToSet[] = $type;
294
            }
295
        }
296
297
        $this->type = new SchemaType($typesToSet);
298
    }
299
300
    /**
301
     * get type
302
     *
303
     * @return SchemaType type
304
     */
305
    public function getType()
306
    {
307
        return $this->type;
308
    }
309
310
    /**
311
     * get MinLengthTypes
312
     *
313
     * @return array MinLengthTypes
314
     */
315
    public function getMinLengthTypes()
316
    {
317
        return $this->minLengthTypes;
318
    }
319
320
    /**
321
     * get format
322
     *
323
     * @return string format
324
     */
325
    public function getFormat()
326
    {
327
        return $this->format;
328
    }
329
330
    /**
331
     * sets format
332
     *
333
     * @param string $format format
334
     *
335
     * @return void
336
     */
337
    public function setFormat($format)
338
    {
339
        $this->format = $format;
340
    }
341
342
    /**
343
     * get numeric minimum
344
     *
345
     * @return float numeric minimum
346
     */
347
    public function getNumericMinimum()
348
    {
349
        return $this->numericMinimum;
350
    }
351
352
    /**
353
     * set numeric minimum
354
     *
355
     * @param float $numericMinimum numeric mimimum
356
     *
357
     * @return void
358
     */
359
    public function setNumericMinimum($numericMinimum)
360
    {
361
        $this->numericMinimum = $numericMinimum;
362
    }
363
364
    /**
365
     * get numeric maximum
366
     *
367
     * @return float numeric maximum
368
     */
369
    public function getNumericMaximum()
370
    {
371
        return $this->numericMaximum;
372
    }
373
374
    /**
375
     * set numeric maximum
376
     *
377
     * @param float $numericMaximum maximum
378
     *
379
     * @return void
380
     */
381
    public function setNumericMaximum($numericMaximum)
382
    {
383
        $this->numericMaximum = $numericMaximum;
384
    }
385
386
    /**
387
     * set min length
388
     *
389
     * @return int length
390
     */
391
    public function getMinLength()
392
    {
393
        return $this->minLength;
394
    }
395
396
    /**
397
     * get min length
398
     *
399
     * @param int $minLength length
400
     *
401
     * @return void
402
     */
403
    public function setMinLength($minLength)
404
    {
405
        $this->minLength = $minLength;
406
    }
407
408
    /**
409
     * gets maxlength
410
     *
411
     * @return int length
412
     */
413
    public function getMaxLength()
414
    {
415
        return $this->maxLength;
416
    }
417
418
    /**
419
     * set maxlength
420
     *
421
     * @param int $maxLength length
422
     *
423
     * @return void
424
     */
425
    public function setMaxLength($maxLength)
426
    {
427
        $this->maxLength = $maxLength;
428
    }
429
430
    /**
431
     * set min Items
432
     *
433
     * @return int Items
434
     */
435
    public function getMinItems()
436
    {
437
        return $this->minItems;
438
    }
439
440
    /**
441
     * get min Items
442
     *
443
     * @param int $minItems length
444
     *
445
     * @return void
446
     */
447
    public function setMinItems($minItems)
448
    {
449
        $this->minItems = $minItems;
450
    }
451
452
    /**
453
     * gets maxItems
454
     *
455
     * @return int Items
456
     */
457
    public function getMaxItems()
458
    {
459
        return $this->maxItems;
460
    }
461
462
    /**
463
     * set maxItems
464
     *
465
     * @param int $maxItems Items
466
     *
467
     * @return void
468
     */
469
    public function setMaxItems($maxItems)
470
    {
471
        $this->maxItems = $maxItems;
472
    }
473
474
    /**
475
     * get Enum
476
     *
477
     * @return array Enum
0 ignored issues
show
Documentation introduced by
Should the return type not be SchemaEnum?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
478
     */
479
    public function getEnum()
480
    {
481
        return $this->enum;
482
    }
483
484
    /**
485
     * set Enum
486
     *
487
     * @param array $enum enum
488
     *
489
     * @return void
490
     */
491
    public function setEnum(array $enum)
492
    {
493
        $this->enum = new SchemaEnum($enum);
494
    }
495
496
    /**
497
     * get regex pattern
498
     *
499
     * @return string pattern
500
     */
501
    public function getRegexPattern()
502
    {
503
        return $this->regexPattern;
504
    }
505
506
    /**
507
     * set regex pattern
508
     *
509
     * @param string $regexPattern regex pattern
510
     *
511
     * @return void
512
     */
513
    public function setRegexPattern($regexPattern)
514
    {
515
        $this->regexPattern = $regexPattern;
516
    }
517
518
    /**
519
     * get DocumentClass
520
     *
521
     * @return string DocumentClass
522
     */
523
    public function getDocumentClass()
524
    {
525
        return $this->documentClass;
526
    }
527
528
    /**
529
     * set DocumentClass
530
     *
531
     * @param string $documentClass documentClass
532
     *
533
     * @return void
534
     */
535
    public function setDocumentClass($documentClass)
536
    {
537
        $this->documentClass = $documentClass;
538
    }
539
540
    /**
541
     * get Constraints
542
     *
543
     * @return mixed Constraints
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
544
     */
545
    public function getConstraints()
546
    {
547
        return $this->constraints;
548
    }
549
550
    /**
551
     * set Constraints
552
     *
553
     * @param mixed $constraints constraints
554
     *
555
     * @return void
556
     */
557
    public function setConstraints($constraints)
558
    {
559
        $this->constraints = $constraints;
0 ignored issues
show
Documentation Bug introduced by
It seems like $constraints of type * is incompatible with the declared type array<integer,string> of property $constraints.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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