LengthAnnotation.php
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* This file is part of the php-annotation framework.
*
* (c) Rasmus Schultz <rasmus@mindplay.dk>
*
* This software is licensed under the GNU LGPL license
* for more information, please see:
*
* <https://github.com/mindplay-dk/php-annotations>
*/
namespace mindplay\demo\annotations;
use mindplay\annotations\AnnotationException;
/**
* Specifies validation of a string, requiring a minimum and/or maximum length.
*
* @usage('property'=>true, 'inherited'=>true)
*/
class LengthAnnotation extends ValidationAnnotationBase
{
/**
* @var int|null Minimum string length (or null, if no minimum)
*/
public $min = null;
/**
* @var int|null Maximum string length (or null, if no maximum)
*/
public $max = null;
/**
* Initialize the annotation.
*/
public function initAnnotation(array $properties)
{
if (isset($properties[0])) {
if (isset($properties[1])) {
$this->min = $properties[0];
$this->max = $properties[1];
unset($properties[1]);
} else {
$this->max = $properties[0];
}
unset($properties[0]);
}
parent::initAnnotation($properties);
if ($this->min !== null && !is_int($this->min)) {
throw new AnnotationException('LengthAnnotation requires an (integer) min property');
}
if ($this->max !== null && !is_int($this->max)) {
throw new AnnotationException('LengthAnnotation requires an (integer) max property');
}
if ($this->min === null && $this->max === null) {
throw new AnnotationException('LengthAnnotation requires a min and/or max property');
}
}
}