Completed
Pull Request — develop (#430)
by Narcotic
15:11 queued 09:56
created

Schema::isTranslatable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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