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
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* RegexValidator |
28
|
|
|
* |
29
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
30
|
|
|
*/ |
31
|
|
|
class RegexValidator implements ValidatorInterface |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
use AllowEmpty, |
35
|
|
|
Messages, |
36
|
|
|
OnScenario, |
37
|
|
|
Safe, |
38
|
|
|
SkipOnError; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string the regular expression to be matched with |
42
|
|
|
*/ |
43
|
|
|
public $pattern; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var boolean whether to invert the validation logic. Defaults to false. If set to true, |
47
|
|
|
* the regular expression defined via {@link pattern} should NOT match the attribute value. |
48
|
|
|
* @since 1.1.5 |
49
|
|
|
* */ |
50
|
|
|
public $not = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @Label('{attribute} has invalid value') |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
public $msgInvalid = ''; |
57
|
|
|
|
58
|
4 |
|
public function isValid(AnnotatedInterface $model, $attribute) |
59
|
|
|
{ |
60
|
4 |
|
$value = $model->$attribute; |
61
|
4 |
|
if ($this->allowEmpty && empty($value)) |
62
|
4 |
|
{ |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
4 |
|
if ($this->pattern === null) |
66
|
4 |
|
{ |
67
|
|
|
$msg = sprintf('The `pattern` property must be specified with a valid regular expression on attribute `%s` of model `%s`', $attribute, get_class($model)); |
68
|
|
|
throw new InvalidArgumentException($msg); |
69
|
|
|
} |
70
|
4 |
|
$label = ManganMeta::create($model)->field($attribute)->label; |
71
|
4 |
|
if (!is_scalar($value)) |
72
|
4 |
|
{ |
73
|
|
|
$this->addError('msgInvalid', ['{attribute}' => $label]); |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
4 |
|
$match = preg_match($this->pattern, $value); |
77
|
4 |
|
if ($this->not) |
78
|
4 |
|
{ |
79
|
|
|
if ($match) |
80
|
2 |
|
{ |
81
|
1 |
|
$this->addError('msgInvalid', ['{attribute}' => $label]); |
82
|
1 |
|
return false; |
83
|
|
|
} |
84
|
1 |
|
} |
85
|
|
|
else |
86
|
|
|
{ |
87
|
2 |
|
if (!$match) |
88
|
2 |
|
{ |
89
|
1 |
|
$this->addError('msgInvalid', ['{attribute}' => $label]); |
90
|
1 |
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
2 |
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|