Completed
Push — master ( f283ed...875b97 )
by Neomerx
05:05
created

SingleValidator::validator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 6
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php namespace Limoncello\Validation;
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\Contracts\Execution\ContextStorageInterface;
20
use Limoncello\Validation\Contracts\Rules\RuleInterface;
21
use Limoncello\Validation\Contracts\ValidatorInterface;
22
use Limoncello\Validation\Execution\ContextStorage;
23
use Limoncello\Validation\Validator\BaseValidator;
24
use Limoncello\Validation\Validator\SingleValidation;
25
26
/**
27
 * @package Limoncello\Validation
28
 */
29 View Code Duplication
class SingleValidator extends BaseValidator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
{
31
    use SingleValidation;
32
33
    /**
34
     * @param RuleInterface $rule
35
     */
36 4
    public function __construct(RuleInterface $rule)
37
    {
38 4
        parent::__construct();
39
40 4
        $this->setRule($rule);
41
    }
42
43
    /**
44
     * @param RuleInterface $rule
45
     *
46
     * @return ValidatorInterface
47
     */
48 4
    public static function validator(RuleInterface $rule): ValidatorInterface
49
    {
50 4
        $validator = new static ($rule);
51
52 4
        return $validator;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     *
58
     * @SuppressWarnings(PHPMD.StaticAccess)
59
     */
60 4
    public function validate($input): bool
61
    {
62 4
        if ($this->areAggregatorsDirty() === true) {
63 3
            $this->resetAggregators();
64
        }
65
66 4
        $this->validateSingleImplementation($input, $this->getCaptures(), $this->getErrors());
67 4
        $this->markAggregatorsAsDirty();
68
69 4
        $noErrors = $this->getErrors()->count() <= 0;
70
71 4
        return $noErrors;
72
    }
73
74
    /**
75
     * During validation you can pass to rules your custom context which might have any additional
76
     * resources needed by your rules (extra properties, database connection settings, container, and etc).
77
     *
78
     * @param array $blocks
79
     *
80
     * @return ContextStorageInterface
81
     */
82 4
    protected function createContextStorageFromBlocks(array $blocks): ContextStorageInterface
83
    {
84 4
        return new ContextStorage($blocks);
85
    }
86
}
87