Completed
Push — feature/other-validation ( 7c1fa1...b6df1c )
by Narcotic
62:33
created

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