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

ArrayValidator::__construct()   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\Execution\ContextStorage;
22
use Limoncello\Validation\Validator\ArrayValidation;
23
use Limoncello\Validation\Validator\BaseValidator;
24
25
/**
26
 * @package Limoncello\Validation
27
 */
28 View Code Duplication
class ArrayValidator 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...
29
{
30
    use ArrayValidation;
31
32
    /**
33
     * @param RuleInterface[] $rules
34
     */
35 1
    public function __construct(array $rules)
36
    {
37 1
        parent::__construct();
38
39 1
        $this->setRules($rules);
40
    }
41
42
    /**
43
     * @param RuleInterface[] $rules
44
     *
45
     * @return self
46
     */
47 1
    public static function validator(array $rules): self
48
    {
49 1
        $validator = new static ($rules);
50
51 1
        return $validator;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 1
    public function validate($input): bool
58
    {
59 1
        if ($this->areAggregatorsDirty() === true) {
60 1
            $this->resetAggregators();
61
        }
62
63 1
        $this->validateArrayImplementation($input, $this->getCaptures(), $this->getErrors());
64 1
        $this->markAggregatorsAsDirty();
65
66 1
        $isOk = $this->getErrors()->count() <= 0;
67
68 1
        return $isOk;
69
    }
70
71
    /**
72
     * During validation you can pass to rules your custom context which might have any additional
73
     * resources needed by your rules (extra properties, database connection settings, container, and etc).
74
     *
75
     * @param array $blocks
76
     *
77
     * @return ContextStorageInterface
78
     */
79 1
    protected function createContextStorageFromBlocks(array $blocks): ContextStorageInterface
80
    {
81 1
        return new ContextStorage($blocks);
82
    }
83
}
84