1
|
|
|
<?php namespace Limoncello\Validation\Validator; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
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\Captures\CaptureAggregator; |
20
|
|
|
use Limoncello\Validation\Contracts\Captures\CaptureAggregatorInterface; |
21
|
|
|
use Limoncello\Validation\Contracts\Errors\ErrorAggregatorInterface; |
22
|
|
|
use Limoncello\Validation\Contracts\ValidatorInterface; |
23
|
|
|
use Limoncello\Validation\Errors\ErrorAggregator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @package Limoncello\Validation |
27
|
|
|
*/ |
28
|
|
|
abstract class BaseValidator implements ValidatorInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $areAggregatorsDirty = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var CaptureAggregatorInterface |
37
|
|
|
*/ |
38
|
|
|
private $captures; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ErrorAggregatorInterface |
42
|
|
|
*/ |
43
|
|
|
private $errors; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor. |
47
|
|
|
*/ |
48
|
5 |
|
public function __construct() |
49
|
|
|
{ |
50
|
5 |
|
$this->resetAggregators(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
2 |
|
public function getCaptures(): array |
57
|
|
|
{ |
58
|
2 |
|
return $this->getCaptureAggregator()->get(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
2 |
|
public function getErrors(): array |
65
|
|
|
{ |
66
|
2 |
|
return $this->getErrorAggregator()->get(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return CaptureAggregatorInterface |
71
|
|
|
*/ |
72
|
5 |
|
protected function getCaptureAggregator(): CaptureAggregatorInterface |
73
|
|
|
{ |
74
|
5 |
|
return $this->captures; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return ErrorAggregatorInterface |
79
|
|
|
*/ |
80
|
5 |
|
protected function getErrorAggregator(): ErrorAggregatorInterface |
81
|
|
|
{ |
82
|
5 |
|
return $this->errors; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return CaptureAggregatorInterface |
87
|
|
|
*/ |
88
|
5 |
|
protected function createCaptureAggregator(): CaptureAggregatorInterface |
89
|
|
|
{ |
90
|
5 |
|
return new CaptureAggregator(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return ErrorAggregatorInterface |
95
|
|
|
*/ |
96
|
5 |
|
protected function createErrorAggregator(): ErrorAggregatorInterface |
97
|
|
|
{ |
98
|
5 |
|
return new ErrorAggregator(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
5 |
|
protected function areAggregatorsDirty(): bool |
105
|
|
|
{ |
106
|
5 |
|
return $this->areAggregatorsDirty; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return self |
111
|
|
|
*/ |
112
|
5 |
|
protected function markAggregatorsAsDirty(): self |
113
|
|
|
{ |
114
|
5 |
|
$this->areAggregatorsDirty = true; |
115
|
|
|
|
116
|
5 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return self |
121
|
|
|
*/ |
122
|
5 |
|
protected function resetAggregators(): self |
123
|
|
|
{ |
124
|
5 |
|
$this->captures = $this->createCaptureAggregator(); |
125
|
5 |
|
$this->errors = $this->createErrorAggregator(); |
126
|
|
|
|
127
|
5 |
|
$this->areAggregatorsDirty = false; |
128
|
|
|
|
129
|
5 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|