Completed
Push — feature/other-validation ( ca1c27...da0ad1 )
by Narcotic
141:14 queued 76:14
created

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