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