|
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; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
18
|
|
|
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface; |
|
19
|
|
|
use Maslosoft\Mangan\Meta\ManganMeta; |
|
20
|
|
|
use Maslosoft\Mangan\Validators\Traits\AllowEmpty; |
|
21
|
|
|
use Maslosoft\Mangan\Validators\Traits\Messages; |
|
22
|
|
|
use Maslosoft\Mangan\Validators\Traits\OnScenario; |
|
23
|
|
|
use Maslosoft\Mangan\Validators\Traits\Safe; |
|
24
|
|
|
use Maslosoft\Mangan\Validators\Traits\SkipOnError; |
|
25
|
|
|
use Maslosoft\Mangan\Validators\Traits\Strict; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* RangeValidator |
|
29
|
|
|
* |
|
30
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
31
|
|
|
*/ |
|
32
|
|
|
class RangeValidator implements ValidatorInterface |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
use AllowEmpty, |
|
36
|
|
|
Messages, |
|
37
|
|
|
Strict, |
|
38
|
|
|
OnScenario, |
|
39
|
|
|
Safe, |
|
40
|
|
|
SkipOnError; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array list of valid values that the attribute value should be among |
|
44
|
|
|
*/ |
|
45
|
|
|
public $range; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var boolean whether to invert the validation logic. Defaults to false. If set to true, |
|
49
|
|
|
* the attribute value should NOT be among the list of values defined via {@link range}. |
|
50
|
|
|
* @since 1.1.5 |
|
51
|
|
|
* */ |
|
52
|
|
|
public $not = false; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @Label('{attribute} is not in the list') |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
public $msgIsNot = ''; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @Label('{attribute} is in the list') |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
public $msgIs = ''; |
|
65
|
|
|
|
|
66
|
6 |
|
public function isValid(AnnotatedInterface $model, $attribute) |
|
67
|
|
|
{ |
|
68
|
6 |
|
$value = $model->$attribute; |
|
69
|
6 |
|
if ($this->allowEmpty && empty($value)) |
|
70
|
|
|
{ |
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
6 |
|
if (!is_array($this->range)) |
|
74
|
|
|
{ |
|
75
|
|
|
$msg = sprintf('The "range" property must be specified with a list of values on attribute `%s` of model `%s`', $attribute, get_class($model)); |
|
76
|
|
|
throw new InvalidArgumentException($msg); |
|
77
|
|
|
} |
|
78
|
6 |
|
$result = false; |
|
79
|
6 |
|
if ($this->strict) |
|
80
|
|
|
{ |
|
81
|
2 |
|
$result = in_array($value, $this->range, true); |
|
82
|
|
|
} |
|
83
|
|
|
else |
|
84
|
|
|
{ |
|
85
|
4 |
|
foreach ($this->range as $r) |
|
86
|
|
|
{ |
|
87
|
4 |
|
$result = $r === '' || $value === '' ? $r === $value : $r == $value; |
|
88
|
4 |
|
if ($result) |
|
89
|
|
|
{ |
|
90
|
2 |
|
break; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
6 |
|
$label = ManganMeta::create($model)->field($attribute)->label; |
|
95
|
6 |
|
if (!$this->not && !$result) |
|
96
|
|
|
{ |
|
97
|
2 |
|
$this->addError('msgIsNot', ['{attribute}' => $label]); |
|
98
|
2 |
|
return false; |
|
99
|
|
|
} |
|
100
|
4 |
|
elseif ($this->not && $result) |
|
101
|
|
|
{ |
|
102
|
1 |
|
$this->addError('msgIs', ['{attribute}' => $label]); |
|
103
|
1 |
|
return false; |
|
104
|
|
|
} |
|
105
|
3 |
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|