Completed
Push — feature/other-validation ( 221a48...aebe33 )
by Narcotic
191:53 queued 126:57
created

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