1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kontrolio\Rules; |
4
|
|
|
|
5
|
|
|
use UnexpectedValueException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Wrapper for callback validation rules. |
9
|
|
|
* |
10
|
|
|
* @package Kontrolio\Rules |
11
|
|
|
*/ |
12
|
|
|
class CallableRuleWrapper extends AbstractRule |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Original rule. |
16
|
|
|
* |
17
|
|
|
* @var callable |
18
|
|
|
*/ |
19
|
|
|
private $rule; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Value that must be validated. |
23
|
|
|
* |
24
|
|
|
* @var mixed |
25
|
|
|
*/ |
26
|
|
|
private $value; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Validation rule identifier. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $name; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Valid/invalid state. |
37
|
|
|
* |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
private $valid = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Skipping state. |
44
|
|
|
* |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
private $skip = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Validation rule constructor. |
51
|
|
|
* |
52
|
|
|
* @param callable|array $rule |
53
|
|
|
* @param mixed $value |
54
|
|
|
*/ |
55
|
|
|
public function __construct($rule, $value = null) |
56
|
|
|
{ |
57
|
|
|
is_array($rule) |
58
|
|
|
? $this->setDefaultsFromArray($rule) |
59
|
|
|
: $this->setDefaults($rule, $value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets default values for wrapper's properties. |
64
|
|
|
* |
65
|
|
|
* @param callable $rule |
66
|
|
|
* @param mixed $value |
67
|
|
|
*/ |
68
|
|
|
private function setDefaults(callable $rule, $value) |
69
|
|
|
{ |
70
|
|
|
$this->rule = $rule; |
71
|
|
|
$this->value = $value; |
72
|
|
|
$this->emptyAllowed = false; |
73
|
|
|
$this->skip = false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Sets properties coming from resulting array of the callback validation rule. |
78
|
|
|
* |
79
|
|
|
* @param array $attributes |
80
|
|
|
* @throws UnexpectedValueException |
81
|
|
|
*/ |
82
|
|
|
private function setDefaultsFromArray(array $attributes) |
83
|
|
|
{ |
84
|
|
|
if (!isset($attributes['valid'])) { |
85
|
|
|
throw new UnexpectedValueException('Validation check missing.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (isset($attributes['name'])) { |
89
|
|
|
$this->name = $attributes['name']; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->valid = (bool) $attributes['valid']; |
93
|
|
|
$this->emptyAllowed = isset($attributes['empty_allowed']) |
94
|
|
|
? (bool) $attributes['empty_allowed'] |
95
|
|
|
: false; |
96
|
|
|
|
97
|
|
|
$this->skip = isset($attributes['skip']) ? (bool) $attributes['skip'] : false; |
98
|
|
|
$this->violations = isset($attributes['violations']) ? $attributes['violations'] : []; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns validation rule identifier. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getName() |
107
|
|
|
{ |
108
|
|
|
if (isset($this->name)) { |
109
|
|
|
return $this->name; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return uniqid(parent::getName() . '_', true); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Validates input. |
117
|
|
|
* |
118
|
|
|
* @param mixed $input |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function isValid($input = null) |
123
|
|
|
{ |
124
|
|
|
$rule = $this->rule; |
125
|
|
|
|
126
|
|
|
return is_callable($rule) ? $rule($this->value) : $this->valid; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* When simply true or some conditions return true, informs validator service that validation can be skipped. |
131
|
|
|
* |
132
|
|
|
* @param mixed $input |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function canSkipValidation($input = null) |
137
|
|
|
{ |
138
|
|
|
return $this->skip; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|