|
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 http://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Annotations\Validators; |
|
15
|
|
|
|
|
16
|
|
|
use Maslosoft\Addendum\Helpers\ParamsExpander; |
|
17
|
|
|
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation; |
|
18
|
|
|
use Maslosoft\Mangan\Meta\ValidatorMeta; |
|
19
|
|
|
use Maslosoft\Mangan\Validators\Proxy\ClassValidatorProxy; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Base class for validator annotations |
|
23
|
|
|
* |
|
24
|
|
|
* @author Piotr |
|
25
|
|
|
*/ |
|
26
|
|
|
class ValidatorAnnotation extends ManganPropertyAnnotation |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
const Ns = __NAMESPACE__; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string the user-defined error message. Different validators may define various |
|
33
|
|
|
* placeholders in the message that are to be replaced with actual values. All validators |
|
34
|
|
|
* recognize "{attribute}" placeholder, which will be replaced with the label of the attribute. |
|
35
|
|
|
*/ |
|
36
|
|
|
public $message; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var boolean whether this validation rule should be skipped when there is already a validation |
|
40
|
|
|
* error for the current attribute. Defaults to false. |
|
41
|
|
|
* @since 1.1.1 |
|
42
|
|
|
*/ |
|
43
|
|
|
public $skipOnError = false; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array list of scenarios that the validator should be applied. |
|
47
|
|
|
* Each array value refers to a scenario name with the same name as its array key. |
|
48
|
|
|
*/ |
|
49
|
|
|
public $on; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var boolean whether attributes listed with this validator should be considered safe for massive assignment. |
|
53
|
|
|
* Defaults to true. |
|
54
|
|
|
* @since 1.1.4 |
|
55
|
|
|
*/ |
|
56
|
|
|
public $safe = true; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var array list of scenarios that the validator should not be applied to. |
|
60
|
|
|
* Each array value refers to a scenario name with the same name as its array key. |
|
61
|
|
|
* @since 1.1.11 |
|
62
|
|
|
*/ |
|
63
|
|
|
public $except; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Validator proxy class |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
public $proxy = ''; |
|
70
|
|
|
public $class = ''; |
|
71
|
|
|
public $value = ''; |
|
72
|
|
|
|
|
73
|
3 |
|
public function init() |
|
74
|
|
|
{ |
|
75
|
|
|
$params = [ |
|
76
|
|
|
'class' |
|
77
|
3 |
|
]; |
|
78
|
3 |
|
if (is_string($this->value)) |
|
79
|
3 |
|
{ |
|
80
|
2 |
|
$this->class = $this->value; |
|
81
|
2 |
|
} |
|
82
|
1 |
|
elseif (is_array($this->value)) |
|
83
|
|
|
{ |
|
84
|
1 |
|
foreach (array_keys($this->value) as $key) |
|
85
|
|
|
{ |
|
86
|
1 |
|
if (!is_numeric($key)) |
|
87
|
1 |
|
{ |
|
88
|
1 |
|
$params[] = $key; |
|
89
|
1 |
|
} |
|
90
|
1 |
|
} |
|
91
|
1 |
|
} |
|
92
|
|
|
else |
|
93
|
|
|
{ |
|
94
|
|
|
throw new \UnexpectedValueException(sprintf("Expected class name (for @Validator) for field `%s` of model `%s`, got: `%s`", $this->_entity->name, $this->_meta->type()->name, $this->value)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
3 |
|
$this->proxy = ClassValidatorProxy::class; |
|
99
|
3 |
|
$this->_entity->validators[] = new ValidatorMeta(ParamsExpander::expand($this, $params)); |
|
100
|
3 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|