Completed
Pull Request — develop (#430)
by Narcotic
20:51 queued 13:20
created

Schema::getMaxItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 0
crap 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 string
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 = array();
58
59
    /**
60
     * @var boolean
61
     */
62
    protected $translatable;
63
64
    /**
65
     * @var array
66
     */
67
    protected $refCollection = array();
68
69
    /**
70
     * possible event names this collection implements (queue events)
71
     *
72
     * @var array
73
     */
74
    protected $eventNames = array();
75
76
    /**
77
     * @var bool
78
     */
79
    protected $readOnly = false;
80
81
    /**
82
     * @var string[]
83
     */
84
    protected $searchable = array();
85
86
    /**
87
     * @var int
88
     */
89
    protected $minLength;
90
91
    /**
92
     * @var int
93
     */
94
    protected $maxLength;
95
96
    /**
97
     * @var int
98
     */
99
    protected $minItems;
100
101
    /**
102
     * @var int
103
     */
104
    protected $maxItems;
105
106
    /**
107
     * @var float
108
     */
109
    protected $numericMinimum;
110
111
    /**
112
     * @var float
113
     */
114
    protected $numericMaximum;
115
116
    /**
117
     * @var SchemaEnum
118
     */
119
    protected $enum;
120
121
    /**
122
     * @var string
123
     */
124
    protected $regexPattern;
125
126
    /**
127
     * @var
128
     */
129
    protected $documentClass;
130
131
    /**
132
     * these are the BSON primitive types.
133
     * http://json-schema.org/latest/json-schema-core.html#anchor8
134
     * every type set *not* in this set will be carried over to 'format'
135
     *
136
     * @var string[]
137
     */
138
    protected $primitiveTypes = [
139
        'array',
140
        'boolean',
141
        'integer',
142
        'number',
143
        'null',
144
        'object',
145
        'string'
146
    ];
147
148
    /**
149
     * those are types that when they are required, a minimal length
150
     * shall be specified in schema (or allow null if not required; that will lead
151
     * to the inclusion of "null" in the "type" property array)
152
     *
153
     * @var array
154
     */
155
    protected $minLengthTypes = [
156
        'integer',
157
        'number',
158
        'float',
159
        'double',
160
        'decimal',
161
        'string',
162
        'date',
163
        'extref'
164
    ];
165
166
    /**
167
     * known non-primitive types we map to primitives here.
168
     * the type itself is set to the format.
169
     *
170
     * @var string[]
171
     */
172
    protected $specialTypeMapping = [
173
        'extref' => 'string',
174
        'translatable' => 'object',
175
        'date' => 'string',
176
        'float' => 'number',
177
        'double' => 'number',
178
        'decimal' => 'number'
179
    ];
180
181
    protected $formatOverrides = [
182
        'date' => 'date-time'
183
    ];
184
185
    /**
186
     * Build properties
187
     */
188 4
    public function __construct()
189
    {
190 4
        $this->properties = new ArrayCollection();
191 4
    }
192
193
    /**
194
     * set title
195
     *
196
     * @param string $title title
197
     *
198
     * @return void
199
     */
200 4
    public function setTitle($title)
201
    {
202 4
        $this->title = $title;
203 4
    }
204
205
    /**
206
     * get title
207
     *
208
     * @return string
209
     */
210 4
    public function getTitle()
211
    {
212 4
        return $this->title;
213
    }
214
215
    /**
216
     * set description
217
     *
218
     * @param string $description description
219
     *
220
     * @return void
221
     */
222 2
    public function setDescription($description)
223
    {
224 2
        $this->description = $description;
225 2
    }
226
227
    /**
228
     * get description
229
     *
230
     * @return string
231
     */
232 2
    public function getDescription()
233
    {
234 2
        return $this->description;
235
    }
236
237
    /**
238
     * set type
239
     *
240
     * @param string|array $types types
241
     *
242
     * @return void
243
     */
244 2
    public function setType($types)
245
    {
246 2
        if (!is_array($types)) {
247 2
            $types = [$types];
248 1
        }
249
250 2
        $typesToSet = [];
251 2
        foreach ($types as $type) {
252 2
            if ($type === 'int') {
253
                $type = 'integer';
254
            }
255 2
            if ($type === 'hash') {
256
                $type = 'object';
257
            }
258
259
            // handle non-primitive types
260 2
            if (!in_array($type, $this->primitiveTypes)) {
261 2
                $setType = 'string';
262 2
                if (isset($this->specialTypeMapping[$type])) {
263
                    $setType = $this->specialTypeMapping[$type];
264
                }
265 2
                $typesToSet[] = $setType;
266
267 2
                if (isset($this->formatOverrides[$type])) {
268
                    $type = $this->formatOverrides[$type];
269
                }
270
271 2
                $this->setFormat($type);
272 1
            } else {
273 2
                $typesToSet[] = $type;
274
            }
275 1
        }
276
277 2
        $this->type = new SchemaType($typesToSet);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Graviton\SchemaBund...SchemaType($typesToSet) of type object<Graviton\SchemaBundle\Document\SchemaType> is incompatible with the declared type string of property $type.

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...
278 2
    }
279
280
    /**
281
     * get type
282
     *
283
     * @return SchemaType type
0 ignored issues
show
Documentation introduced by
Should the return type not be 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.

Loading history...
284
     */
285 2
    public function getType()
286
    {
287 2
        return $this->type;
288
    }
289
290
    /**
291
     * get MinLengthTypes
292
     *
293
     * @return array MinLengthTypes
294
     */
295 2
    public function getMinLengthTypes()
296
    {
297 2
        return $this->minLengthTypes;
298
    }
299
300
    /**
301
     * get format
302
     *
303
     * @return string format
304
     */
305 2
    public function getFormat()
306
    {
307 2
        return $this->format;
308
    }
309
310
    /**
311
     * sets format
312
     *
313
     * @param string $format format
314
     *
315
     * @return void
316
     */
317 2
    public function setFormat($format)
318
    {
319 2
        $this->format = $format;
320 2
    }
321
322
    /**
323
     * get numeric minimum
324
     *
325
     * @return float numeric minimum
326
     */
327 2
    public function getNumericMinimum()
328
    {
329 2
        return $this->numericMinimum;
330
    }
331
332
    /**
333
     * set numeric minimum
334
     *
335
     * @param float $numericMinimum numeric mimimum
336
     *
337
     * @return void
338
     */
339
    public function setNumericMinimum($numericMinimum)
340
    {
341
        $this->numericMinimum = $numericMinimum;
342
    }
343
344
    /**
345
     * get numeric maximum
346
     *
347
     * @return float numeric maximum
348
     */
349 2
    public function getNumericMaximum()
350
    {
351 2
        return $this->numericMaximum;
352
    }
353
354
    /**
355
     * set numeric maximum
356
     *
357
     * @param float $numericMaximum maximum
358
     *
359
     * @return void
360
     */
361
    public function setNumericMaximum($numericMaximum)
362
    {
363
        $this->numericMaximum = $numericMaximum;
364
    }
365
366
    /**
367
     * set min length
368
     *
369
     * @return int length
370
     */
371 2
    public function getMinLength()
372
    {
373 2
        return $this->minLength;
374
    }
375
376
    /**
377
     * get min length
378
     *
379
     * @param int $minLength length
380
     *
381
     * @return void
382
     */
383
    public function setMinLength($minLength)
384
    {
385
        $this->minLength = $minLength;
386
    }
387
388
    /**
389
     * gets maxlength
390
     *
391
     * @return int length
392
     */
393 2
    public function getMaxLength()
394
    {
395 2
        return $this->maxLength;
396
    }
397
398
    /**
399
     * set maxlength
400
     *
401
     * @param int $maxLength length
402
     *
403
     * @return void
404
     */
405
    public function setMaxLength($maxLength)
406
    {
407
        $this->maxLength = $maxLength;
408
    }
409
410
    /**
411
     * set min Items
412
     *
413
     * @return int Items
414
     */
415 2
    public function getMinItems()
416
    {
417 2
        return $this->minItems;
418
    }
419
420
    /**
421
     * get min Items
422
     *
423
     * @param int $minItems length
424
     *
425
     * @return void
426
     */
427
    public function setMinItems($minItems)
428
    {
429
        $this->minItems = $minItems;
430
    }
431
432
    /**
433
     * gets maxItems
434
     *
435
     * @return int Items
436
     */
437 2
    public function getMaxItems()
438
    {
439 2
        return $this->maxItems;
440
    }
441
442
    /**
443
     * set maxItems
444
     *
445
     * @param int $maxItems Items
446
     *
447
     * @return void
448
     */
449
    public function setMaxItems($maxItems)
450
    {
451
        $this->maxItems = $maxItems;
452
    }
453
454
    /**
455
     * get Enum
456
     *
457
     * @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...
458
     */
459 2
    public function getEnum()
460
    {
461 2
        return $this->enum;
462
    }
463
464
    /**
465
     * set Enum
466
     *
467
     * @param array $enum enum
468
     *
469
     * @return void
470
     */
471
    public function setEnum(array $enum)
472
    {
473
        $this->enum = new SchemaEnum($enum);
474
    }
475
476
    /**
477
     * get regex pattern
478
     *
479
     * @return string pattern
480
     */
481 2
    public function getRegexPattern()
482
    {
483 2
        return $this->regexPattern;
484
    }
485
486
    /**
487
     * set regex pattern
488
     *
489
     * @param string $regexPattern regex pattern
490
     *
491
     * @return void
492
     */
493
    public function setRegexPattern($regexPattern)
494
    {
495
        $this->regexPattern = $regexPattern;
496
    }
497
498
    /**
499
     * get DocumentClass
500
     *
501
     * @return string DocumentClass
502
     */
503 2
    public function getDocumentClass()
504
    {
505 2
        return $this->documentClass;
506
    }
507
508
    /**
509
     * set DocumentClass
510
     *
511
     * @param string $documentClass documentClass
512
     *
513
     * @return void
514
     */
515 2
    public function setDocumentClass($documentClass)
516
    {
517 2
        $this->documentClass = $documentClass;
518 2
    }
519
520
    /**
521
     * set items
522
     *
523
     * @param Schema $items items schema
524
     *
525
     * @return void
526
     */
527 2
    public function setItems($items)
528
    {
529 2
        $this->items = $items;
530 2
    }
531
532
    /**
533
     * get items
534
     *
535
     * @return Schema
536
     */
537 2
    public function getItems()
538
    {
539 2
        return $this->items;
540
    }
541
542
    /**
543
     * add a property
544
     *
545
     * @param string $name     property name
546
     * @param Schema $property property
547
     *
548
     * @return void
549
     */
550 2
    public function addProperty($name, $property)
551
    {
552 2
        $this->properties->set($name, $property);
553 2
    }
554
555
    /**
556
     * removes a property
557
     *
558
     * @param string $name property name
559
     *
560
     * @return void
561
     */
562 2
    public function removeProperty($name)
563
    {
564 2
        if (!$this->properties->containsKey($name)) {
565
            $this->properties->remove($this->properties->get($name));
566
        }
567 2
    }
568
569
    /**
570
     * returns a property
571
     *
572
     * @param string $name property name
573
     *
574
     * @return void|Schema property
575
     */
576
    public function getProperty($name)
577
    {
578
        return $this->properties->get($name);
579
    }
580
581
    /**
582
     * get properties
583
     *
584
     * @return ArrayCollection|null
585
     */
586 2
    public function getProperties()
587
    {
588 2
        if ($this->properties->isEmpty()) {
589 2
            return null;
590
        } else {
591 2
            return $this->properties;
592
        }
593
    }
594
595
    /**
596
     * set additionalProperties on schema
597
     *
598
     * @param SchemaAdditionalProperties $additionalProperties additional properties
599
     *
600
     * @return void
601
     */
602 2
    public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties)
603
    {
604 2
        $this->additionalProperties = $additionalProperties;
605 2
    }
606
607
    /**
608
     * get addtionalProperties for schema
609
     *
610
     * @return SchemaAdditionalProperties
611
     */
612 2
    public function getAdditionalProperties()
613
    {
614 2
        return $this->additionalProperties;
615
    }
616
617
    /**
618
     * set required variables
619
     *
620
     * @param string[] $required array of required fields
621
     *
622
     * @return void
623
     */
624 2
    public function setRequired(array $required)
625
    {
626
        // needed as indexes could we off and we want to enforce an array after json_encode
627 2
        $this->required = array_values($required);
628 2
    }
629
630
    /**
631
     * get required fields
632
     *
633
     * @return string[]|null
634
     */
635 2
    public function getRequired()
636
    {
637 2
        $required = $this->required;
638 2
        if (empty($required)) {
639 2
            $required = null;
640 1
        }
641
642 2
        return $required;
643
    }
644
645
    /**
646
     * set translatable flag
647
     *
648
     * This flag is a local extension to json schema.
649
     *
650
     * @param boolean $translatable translatable flag
651
     *
652
     * @return void
653
     */
654
    public function setTranslatable($translatable)
655
    {
656
        if ($translatable === true) {
657
            $this->setType('translatable');
658
        } else {
659
            $this->setType('string');
660
        }
661
    }
662
663
    /**
664
     * get translatable flag
665
     *
666
     * @return boolean
667
     */
668
    public function isTranslatable()
669
    {
670
        $ret = false;
671
        if ($this->getFormat() == 'translatable') {
672
            $ret = true;
673
        }
674
675
        return $ret;
676
    }
677
678
    /**
679
     * set a array of urls that can extref refer to
680
     *
681
     * @param array $refCollection urls
682
     *
683
     * @return void
684
     */
685
    public function setRefCollection(array $refCollection)
686
    {
687
        $this->refCollection = $refCollection;
688
    }
689
690
    /**
691
     * get a collection of urls that can extref refer to
692
     *
693
     * @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...
694
     */
695 2
    public function getRefCollection()
696
    {
697 2
        $collection = $this->refCollection;
698 2
        if (empty($collection)) {
699 2
            $collection = null;
700 1
        }
701
702 2
        return $collection;
703
    }
704
705
    /**
706
     * set an array of possible event names
707
     *
708
     * @param array $eventNames event names
709
     *
710
     * @return void
711
     */
712 2
    public function setEventNames(array $eventNames)
713
    {
714 2
        $this->eventNames = array_values($eventNames);
715 2
    }
716
717
    /**
718
     * get a collection of possible event names
719
     *
720
     * @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...
721
     */
722 2
    public function getEventNames()
723
    {
724 2
        $collection = $this->eventNames;
725 2
        if (empty($collection)) {
726 2
            $collection = null;
727 1
        }
728
729 2
        return $collection;
730
    }
731
732
    /**
733
     * Set the readOnly flag
734
     *
735
     * @param bool $readOnly ReadOnly flag
736
     *
737
     * @return void
738
     */
739 2
    public function setReadOnly($readOnly)
740
    {
741 2
        $this->readOnly = (bool) $readOnly;
742 2
    }
743
744
    /**
745
     * Get the readOnly flag.
746
     * Returns null if the flag is set to false so the serializer will ignore it.
747
     *
748
     * @return bool|null true if readOnly isset to true or null if not
749
     */
750 2
    public function getReadOnly()
751
    {
752 2
        return $this->readOnly ? true : null;
753
    }
754
755
    /**
756
     * set searchable variables
757
     *
758
     * @param string[] $searchable arary of searchable fields
759
     *
760
     * @return void
761
     */
762 2
    public function setSearchable($searchable)
763
    {
764 2
        $this->searchable = $searchable;
765 2
    }
766
767
    /**
768
     * get searchable fields
769
     *
770
     * @return string[]|null
771
     */
772 2
    public function getSearchable()
773
    {
774 2
        $searchable = $this->searchable;
775 2
        if (empty($searchable)) {
776 2
            $searchable = null;
777 1
        }
778
779 2
        return $searchable;
780
    }
781
}
782