1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mbright\Validation\Spec; |
4
|
|
|
|
5
|
|
|
use Mbright\Validation\Rule\Validate\ValidateRuleInterface; |
6
|
|
|
|
7
|
|
|
class ValidateSpec extends AbstractSpec |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Flag to indicate if the negated value of the rule should be returned. |
11
|
|
|
* |
12
|
|
|
* @var bool |
13
|
|
|
*/ |
14
|
|
|
protected $negated = false; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Failure mode for the rule. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $failureMode = 'SOFT_FAILURE'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Invokes the rule that this spec is configured for. |
25
|
|
|
* |
26
|
|
|
* @param object $subject |
27
|
|
|
* |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
45 |
|
public function __invoke($subject): bool |
31
|
|
|
{ |
32
|
45 |
|
$isBlank = $this->subjectFieldIsBlank($subject); |
33
|
|
|
|
34
|
45 |
|
if ($isBlank && $this->allowBlanks) { |
35
|
9 |
|
return true; |
36
|
|
|
} |
37
|
|
|
|
38
|
36 |
|
if ($isBlank && !$this->allowBlanks) { |
39
|
12 |
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
24 |
|
$result = parent::__invoke($subject); |
43
|
|
|
|
44
|
24 |
|
return $this->negated ? !$result : $result; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Sets a validation rule and its arguments. |
49
|
|
|
* |
50
|
|
|
* @param ValidateRuleInterface $rule |
51
|
|
|
* |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
51 |
|
public function is(ValidateRuleInterface $rule): self |
55
|
|
|
{ |
56
|
51 |
|
$this->rule = $rule; |
57
|
51 |
|
$this->ruleClass = get_class($rule); |
58
|
|
|
|
59
|
51 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets a negated validation rule and its arguments. |
64
|
|
|
* |
65
|
|
|
* @param ValidateRuleInterface $ruleClass |
66
|
|
|
* |
67
|
|
|
* @return AbstractSpec |
68
|
|
|
*/ |
69
|
6 |
|
public function isNot(ValidateRuleInterface $rule): self |
70
|
|
|
{ |
71
|
6 |
|
$this->negated = true; |
72
|
6 |
|
$this->is($rule); |
73
|
|
|
|
74
|
6 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Allow blank values to pass validation. |
79
|
|
|
* |
80
|
|
|
* @param array $allowedBlanks |
81
|
|
|
* |
82
|
|
|
* @return ValidateSpec |
83
|
|
|
*/ |
84
|
12 |
|
public function allowBlank(): self |
85
|
|
|
{ |
86
|
12 |
|
$this->allowBlanks = true; |
87
|
|
|
|
88
|
12 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Indicates if the spec accepts blank values. |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function acceptBlanks(): bool |
97
|
|
|
{ |
98
|
|
|
return $this->allowBlanks; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the default failure message for this rule specification. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
15 |
|
protected function getDefaultMessage(): string |
107
|
|
|
{ |
108
|
15 |
|
$message = $this->field . ' should'; |
109
|
15 |
|
if (!$this->allowBlanks) { |
110
|
15 |
|
$message .= ' not be blank and'; |
111
|
|
|
} |
112
|
|
|
|
113
|
15 |
|
if ($this->allowBlanks) { |
114
|
|
|
$message .= ' have been blank or'; |
115
|
|
|
} |
116
|
|
|
|
117
|
15 |
|
if ($this->negated) { |
118
|
|
|
$message .= ' not'; |
119
|
|
|
} |
120
|
|
|
|
121
|
15 |
|
return "{$message} validated as " . parent::getDefaultMessage(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|