Completed
Push — master ( 2df407...de4379 )
by Neomerx
05:05
created

ArrayValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 75
rs 10
c 0
b 0
f 0
ccs 19
cts 19
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A validator() 0 6 1
A validate() 0 13 2
A getContainer() 0 4 1
A createContextStorageFromBlocks() 0 4 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\Execution\ContextStorage;
22
use Limoncello\Validation\Validator\ArrayValidation;
23
use Limoncello\Validation\Validator\BaseValidator;
24
use Psr\Container\ContainerInterface;
25
26
/**
27
 * @package Limoncello\Validation
28
 */
29
class ArrayValidator extends BaseValidator
30
{
31
    use ArrayValidation;
32
33
    /**
34
     * @var ContainerInterface|null
35
     */
36
    private $container;
37
38
    /**
39
     * @param RuleInterface[]         $rules
40
     * @param ContainerInterface|null $container
41
     */
42 3
    public function __construct(array $rules, ContainerInterface $container = null)
43
    {
44 3
        parent::__construct();
45
46 3
        if (empty($rules) === false) {
47 3
            $this->setRules($rules);
48
        }
49
50 3
        $this->container = $container;
51
    }
52
53
    /**
54
     * @param RuleInterface[]    $rules
55
     * @param ContainerInterface $container
0 ignored issues
show
Documentation introduced by
Should the type for parameter $container not be null|ContainerInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
56
     *
57
     * @return self
58
     */
59 3
    public static function validator(array $rules = [], ContainerInterface $container = null): self
60
    {
61 3
        $validator = new static ($rules, $container);
62
63 3
        return $validator;
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 3
    public function validate($input): bool
70
    {
71 3
        if ($this->areAggregatorsDirty() === true) {
72 2
            $this->resetAggregators();
73
        }
74
75 3
        $this->validateArrayImplementation($input, $this->getCaptureAggregator(), $this->getErrorAggregator());
76 3
        $this->markAggregatorsAsDirty();
77
78 3
        $isOk = $this->getErrorAggregator()->count() <= 0;
79
80 3
        return $isOk;
81
    }
82
83
    /**
84
     * @return ContainerInterface|null
85
     */
86 3
    protected function getContainer(): ?ContainerInterface
87
    {
88 3
        return $this->container;
89
    }
90
91
    /**
92
     * During validation you can pass to rules your custom context which might have any additional
93
     * resources needed by your rules (extra properties, database connection settings, container, and etc).
94
     *
95
     * @param array $blocks
96
     *
97
     * @return ContextStorageInterface
98
     */
99 3
    protected function createContextStorageFromBlocks(array $blocks): ContextStorageInterface
100
    {
101 3
        return new ContextStorage($blocks, $this->getContainer());
102
    }
103
}
104