|
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
|
|
|
* @var CaptureAggregatorInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $aggregator; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $name; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param mixed $data |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
abstract protected function capture($data); |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
|
|
abstract protected function getCapturedData(); |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $name |
|
62
|
|
|
* @param RuleInterface $rule |
|
63
|
|
|
* @param CaptureAggregatorInterface $aggregator |
|
64
|
|
|
*/ |
|
65
|
4 |
|
public function __construct($name, RuleInterface $rule, CaptureAggregatorInterface $aggregator) |
|
66
|
|
|
{ |
|
67
|
4 |
|
$this->rule = $rule; |
|
68
|
4 |
|
$this->aggregator = $aggregator; |
|
69
|
4 |
|
$this->name = $name; |
|
70
|
4 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @inheritdoc |
|
74
|
|
|
*/ |
|
75
|
4 |
|
public function validate($input) |
|
76
|
|
|
{ |
|
77
|
4 |
|
$this->hadErrors = false; |
|
78
|
4 |
|
foreach ($this->getRule()->validate($input) as $error) { |
|
79
|
1 |
|
$this->hadErrors = true; |
|
80
|
1 |
|
yield $error; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
if ($this->hadErrors() === false) { |
|
84
|
3 |
|
$this->capture($input); |
|
85
|
|
|
} |
|
86
|
4 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @inheritdoc |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function isStateless() |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this->getRule()->isStateless(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritdoc |
|
98
|
|
|
*/ |
|
99
|
4 |
|
public function setParentRule(RuleInterface $parent) |
|
100
|
|
|
{ |
|
101
|
4 |
|
$this->getRule()->setParentRule($parent); |
|
102
|
4 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @inheritdoc |
|
106
|
|
|
*/ |
|
107
|
3 |
|
public function getParameterName() |
|
108
|
|
|
{ |
|
109
|
3 |
|
return $this->getRule()->getParameterName(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @inheritdoc |
|
114
|
|
|
*/ |
|
115
|
3 |
|
public function setParameterName($parameterName) |
|
116
|
|
|
{ |
|
117
|
3 |
|
$this->getRule()->setParameterName($parameterName); |
|
118
|
|
|
|
|
119
|
3 |
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritdoc |
|
124
|
|
|
*/ |
|
125
|
4 |
|
public function onFinish(ErrorAggregatorInterface $aggregator) |
|
126
|
|
|
{ |
|
127
|
4 |
|
$this->getRule()->onFinish($aggregator); |
|
128
|
|
|
|
|
129
|
4 |
|
if ($this->hadErrors() === false) { |
|
130
|
3 |
|
$this->getAggregator()->remember( |
|
131
|
3 |
|
$this->getCaptureName(), |
|
132
|
3 |
|
$this->getCapturedData() |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
4 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return CaptureAggregatorInterface |
|
139
|
|
|
*/ |
|
140
|
3 |
|
protected function getAggregator() |
|
141
|
|
|
{ |
|
142
|
3 |
|
return $this->aggregator; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return RuleInterface |
|
147
|
|
|
*/ |
|
148
|
4 |
|
protected function getRule() |
|
149
|
|
|
{ |
|
150
|
4 |
|
return $this->rule; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
3 |
|
protected function getCaptureName() |
|
157
|
|
|
{ |
|
158
|
3 |
|
return $this->name; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return boolean |
|
163
|
|
|
*/ |
|
164
|
4 |
|
protected function hadErrors() |
|
165
|
|
|
{ |
|
166
|
4 |
|
return $this->hadErrors; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|