SingleValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Validation;
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\Contracts\Execution\ContextStorageInterface;
22
use Limoncello\Validation\Contracts\Rules\RuleInterface;
23
use Limoncello\Validation\Contracts\ValidatorInterface;
24
use Limoncello\Validation\Execution\ContextStorage;
25
use Limoncello\Validation\Validator\BaseValidator;
26
use Limoncello\Validation\Validator\SingleValidation;
27
use Psr\Container\ContainerInterface;
28
29
/**
30
 * @package Limoncello\Validation
31
 */
32
class SingleValidator extends BaseValidator
33
{
34
    use SingleValidation;
35
36
    /**
37
     * @var ContainerInterface|null
38
     */
39
    private $container;
40
41
    /**
42
     * @param RuleInterface           $rule
43
     * @param ContainerInterface|null $container
44
     */
45 4
    public function __construct(RuleInterface $rule, ContainerInterface $container = null)
46
    {
47 4
        parent::__construct();
48
49 4
        $this->setRule($rule);
50
51 4
        $this->container = $container;
52
    }
53
54
    /**
55
     * @param RuleInterface           $rule
56
     * @param ContainerInterface|null $container
57
     *
58
     * @return ValidatorInterface
59
     */
60 4
    public static function validator(RuleInterface $rule, ContainerInterface $container = null): ValidatorInterface
61
    {
62 4
        $validator = new static($rule, $container);
63
64 4
        return $validator;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     *
70
     * @SuppressWarnings(PHPMD.StaticAccess)
71
     */
72 4
    public function validate($input): bool
73
    {
74 4
        if ($this->areAggregatorsDirty() === true) {
75 3
            $this->resetAggregators();
76
        }
77
78 4
        $this->validateSingleImplementation($input, $this->getCaptureAggregator(), $this->getErrorAggregator());
79 4
        $this->markAggregatorsAsDirty();
80
81 4
        $noErrors = $this->getErrorAggregator()->count() <= 0;
82
83 4
        return $noErrors;
84
    }
85
86
    /**
87
     * @return ContainerInterface|null
88
     */
89 4
    protected function getContainer(): ?ContainerInterface
90
    {
91 4
        return $this->container;
92
    }
93
94
    /**
95
     * During validation you can pass to rules your custom context which might have any additional
96
     * resources needed by your rules (extra properties, database connection settings, container, and etc).
97
     *
98
     * @param array $blocks
99
     *
100
     * @return ContextStorageInterface
101
     */
102 4
    protected function createContextStorageFromBlocks(array $blocks): ContextStorageInterface
103
    {
104 4
        return new ContextStorage($blocks, $this->getContainer());
105
    }
106
}
107