|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maslosoft\Mangan\Validators\BuiltIn\Base; |
|
4
|
|
|
|
|
5
|
|
|
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface; |
|
6
|
|
|
use Maslosoft\Mangan\Meta\ManganMeta; |
|
7
|
|
|
use Maslosoft\Mangan\Validators\BuiltIn\CountValidator; |
|
8
|
|
|
use Maslosoft\Mangan\Validators\BuiltIn\StringValidator; |
|
9
|
|
|
use Maslosoft\Mangan\Validators\Traits\AllowEmpty; |
|
10
|
|
|
use Maslosoft\Mangan\Validators\Traits\Messages; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Base class for size validators. |
|
14
|
|
|
* |
|
15
|
|
|
* This can be used as a base for validators checking sizes: |
|
16
|
|
|
* |
|
17
|
|
|
* * String Length |
|
18
|
|
|
* * Number of elements |
|
19
|
|
|
* * File size |
|
20
|
|
|
* |
|
21
|
|
|
* Override msg* attributes with custom `Label` annotations to |
|
22
|
|
|
* provide proper error messages. |
|
23
|
|
|
* |
|
24
|
|
|
* @see StringValidator |
|
25
|
|
|
* @see CountValidator |
|
26
|
|
|
* @see ValidatorInterface |
|
27
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class SizeValidator |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
use AllowEmpty, |
|
33
|
|
|
Messages; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var integer maximum length. Defaults to null, meaning no maximum limit. |
|
37
|
|
|
*/ |
|
38
|
|
|
public $max; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var integer minimum length. Defaults to null, meaning no minimum limit. |
|
42
|
|
|
*/ |
|
43
|
|
|
public $min; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var integer exact length. Defaults to null, meaning no exact length limit. |
|
47
|
|
|
*/ |
|
48
|
|
|
public $is; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string user-defined error message used when the value is too short. |
|
52
|
|
|
* @deprecated use `msgTooShort` instead |
|
53
|
|
|
*/ |
|
54
|
|
|
public $tooShort; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var string user-defined error message used when the value is too long. |
|
58
|
|
|
* @deprecated use `msgTooLong` instead |
|
59
|
|
|
*/ |
|
60
|
|
|
public $tooLong; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @Label('{attribute} is invalid') |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
public $msgInvalid = ''; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @Label('{attribute} is too small') |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
public $msgTooShort = ''; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @Label('{attribute} is too large') |
|
76
|
|
|
* @var string |
|
77
|
|
|
*/ |
|
78
|
|
|
public $msgTooLong = ''; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @Label('{attribute} is of the wrong size') |
|
82
|
|
|
* @var string |
|
83
|
|
|
*/ |
|
84
|
|
|
public $msgLength = ''; |
|
85
|
|
|
|
|
86
|
6 |
|
protected function isValidValueOf($model, $attribute, $value, $label = '') |
|
87
|
|
|
{ |
|
88
|
6 |
|
if ($this->allowEmpty && empty($value)) |
|
89
|
|
|
{ |
|
90
|
|
|
return true; |
|
91
|
|
|
} |
|
92
|
6 |
|
if (empty($label)) |
|
93
|
|
|
{ |
|
94
|
|
|
$label = ManganMeta::create($model)->field($attribute)->label; |
|
95
|
|
|
} |
|
96
|
6 |
|
if (!is_int($value)) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->addError('msgInvalid', ['{attribute}' => $label]); |
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
6 |
|
if ($this->min !== null && $value < $this->min) |
|
103
|
|
|
{ |
|
104
|
1 |
|
if ($this->tooShort) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
$this->addError($this->tooShort, ['{min}' => $this->min, '{attribute}' => $label]); |
|
|
|
|
|
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
1 |
|
$this->addError('msgTooShort', array('{min}' => $this->min, '{attribute}' => $label)); |
|
110
|
1 |
|
return false; |
|
111
|
|
|
} |
|
112
|
5 |
|
if ($this->max !== null && $value > $this->max) |
|
113
|
|
|
{ |
|
114
|
1 |
|
if ($this->tooLong) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$this->addError($this->tooLong, array('{max}' => $this->max, '{attribute}' => $label)); |
|
|
|
|
|
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
1 |
|
$this->addError('msgTooLong', array('{max}' => $this->max, '{attribute}' => $label)); |
|
120
|
1 |
|
return false; |
|
121
|
|
|
} |
|
122
|
4 |
|
if ($this->is !== null && $value !== $this->is) |
|
123
|
|
|
{ |
|
124
|
1 |
|
$this->addError('msgLength', array('{length}' => $this->is, '{attribute}' => $label)); |
|
125
|
1 |
|
return false; |
|
126
|
|
|
} |
|
127
|
3 |
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.