BaseValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Validation\Validator;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Validation\Captures\CaptureAggregator;
22
use Limoncello\Validation\Contracts\Captures\CaptureAggregatorInterface;
23
use Limoncello\Validation\Contracts\Errors\ErrorAggregatorInterface;
24
use Limoncello\Validation\Contracts\ValidatorInterface;
25
use Limoncello\Validation\Errors\ErrorAggregator;
26
27
/**
28
 * @package Limoncello\Validation
29
 */
30
abstract class BaseValidator implements ValidatorInterface
31
{
32
    /**
33
     * @var bool
34
     */
35
    private $areAggregatorsDirty = false;
36
37
    /**
38
     * @var CaptureAggregatorInterface
39
     */
40
    private $captures;
41
42
    /**
43
     * @var ErrorAggregatorInterface
44
     */
45
    private $errors;
46
47
    /**
48
     * Constructor.
49
     */
50 7
    public function __construct()
51
    {
52 7
        $this->resetAggregators();
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 2
    public function getCaptures(): array
59
    {
60 2
        return $this->getCaptureAggregator()->get();
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 2
    public function getErrors(): array
67
    {
68 2
        return $this->getErrorAggregator()->get();
69
    }
70
71
    /**
72
     * @return CaptureAggregatorInterface
73
     */
74 7
    protected function getCaptureAggregator(): CaptureAggregatorInterface
75
    {
76 7
        return $this->captures;
77
    }
78
79
    /**
80
     * @return ErrorAggregatorInterface
81
     */
82 7
    protected function getErrorAggregator(): ErrorAggregatorInterface
83
    {
84 7
        return $this->errors;
85
    }
86
87
    /**
88
     * @return CaptureAggregatorInterface
89
     */
90 7
    protected function createCaptureAggregator(): CaptureAggregatorInterface
91
    {
92 7
        return new CaptureAggregator();
93
    }
94
95
    /**
96
     * @return ErrorAggregatorInterface
97
     */
98 7
    protected function createErrorAggregator(): ErrorAggregatorInterface
99
    {
100 7
        return new ErrorAggregator();
101
    }
102
103
    /**
104
     * @return bool
105
     */
106 7
    protected function areAggregatorsDirty(): bool
107
    {
108 7
        return $this->areAggregatorsDirty;
109
    }
110
111
    /**
112
     * @return self
113
     */
114 7
    protected function markAggregatorsAsDirty(): self
115
    {
116 7
        $this->areAggregatorsDirty = true;
117
118 7
        return $this;
119
    }
120
121
    /**
122
     * @return self
123
     */
124 7
    protected function resetAggregators(): self
125
    {
126 7
        $this->captures = $this->createCaptureAggregator();
127 7
        $this->errors   = $this->createErrorAggregator();
128
129 7
        $this->areAggregatorsDirty = false;
130
131 7
        return $this;
132
    }
133
}
134