1
|
|
|
<?php namespace Limoncello\Validation\Captures; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2016 [email protected] (www.neomerx.com) |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Validation\Contracts\CaptureAggregatorInterface; |
20
|
|
|
use Limoncello\Validation\Contracts\ErrorAggregatorInterface; |
21
|
|
|
use Limoncello\Validation\Contracts\RuleInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @package Limoncello\Validation |
25
|
|
|
*/ |
26
|
|
|
abstract class BaseCapture implements RuleInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var RuleInterface |
30
|
|
|
*/ |
31
|
|
|
private $rule; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $hadErrors = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* If capture actually had some input data and did validation. |
40
|
|
|
* |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
private $isValidated = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var CaptureAggregatorInterface |
47
|
|
|
*/ |
48
|
|
|
private $aggregator; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $name; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param mixed $data |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
abstract protected function capture($data); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
abstract protected function getCapturedData(); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* @param RuleInterface $rule |
70
|
|
|
* @param CaptureAggregatorInterface $aggregator |
71
|
|
|
*/ |
72
|
5 |
|
public function __construct($name, RuleInterface $rule, CaptureAggregatorInterface $aggregator) |
73
|
|
|
{ |
74
|
5 |
|
$this->rule = $rule; |
75
|
5 |
|
$this->aggregator = $aggregator; |
76
|
5 |
|
$this->name = $name; |
77
|
5 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
5 |
|
public function validate($input) |
83
|
|
|
{ |
84
|
5 |
|
$this->hadErrors = false; |
85
|
5 |
|
foreach ($this->getRule()->validate($input) as $error) { |
86
|
1 |
|
$this->hadErrors = true; |
87
|
1 |
|
yield $error; |
88
|
|
|
} |
89
|
|
|
|
90
|
5 |
|
if ($this->hadErrors() === false) { |
91
|
4 |
|
$this->capture($input); |
92
|
|
|
} |
93
|
|
|
|
94
|
5 |
|
$this->isValidated = true; |
95
|
5 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
1 |
|
public function isStateless() |
101
|
|
|
{ |
102
|
1 |
|
return $this->getRule()->isStateless(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritdoc |
107
|
|
|
*/ |
108
|
5 |
|
public function setParentRule(RuleInterface $parent) |
109
|
|
|
{ |
110
|
5 |
|
$this->getRule()->setParentRule($parent); |
111
|
5 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritdoc |
115
|
|
|
*/ |
116
|
3 |
|
public function getParameterName() |
117
|
|
|
{ |
118
|
3 |
|
return $this->getRule()->getParameterName(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritdoc |
123
|
|
|
*/ |
124
|
3 |
|
public function setParameterName($parameterName) |
125
|
|
|
{ |
126
|
3 |
|
$this->getRule()->setParameterName($parameterName); |
127
|
|
|
|
128
|
3 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritdoc |
133
|
|
|
*/ |
134
|
5 |
|
public function onFinish(ErrorAggregatorInterface $aggregator) |
135
|
|
|
{ |
136
|
5 |
|
$this->getRule()->onFinish($aggregator); |
137
|
|
|
|
138
|
5 |
|
if ($this->isValidated() === true && $this->hadErrors() === false) { |
139
|
4 |
|
$this->getAggregator()->remember( |
140
|
4 |
|
$this->getCaptureName(), |
141
|
4 |
|
$this->getCapturedData() |
142
|
|
|
); |
143
|
|
|
} |
144
|
5 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return CaptureAggregatorInterface |
148
|
|
|
*/ |
149
|
4 |
|
protected function getAggregator() |
150
|
|
|
{ |
151
|
4 |
|
return $this->aggregator; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return RuleInterface |
156
|
|
|
*/ |
157
|
5 |
|
protected function getRule() |
158
|
|
|
{ |
159
|
5 |
|
return $this->rule; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
4 |
|
protected function getCaptureName() |
166
|
|
|
{ |
167
|
4 |
|
return $this->name; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return boolean |
172
|
|
|
*/ |
173
|
5 |
|
protected function hadErrors() |
174
|
|
|
{ |
175
|
5 |
|
return $this->hadErrors; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return boolean |
180
|
|
|
*/ |
181
|
5 |
|
protected function isValidated() |
182
|
|
|
{ |
183
|
5 |
|
return $this->isValidated; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|