Completed
Push — master ( ceaad2...5caa52 )
by Neomerx
04:27 queued 01:12
created

IterateRules::onFinish()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php namespace Limoncello\Validation\Expressions;
2
3
/**
4
 * Copyright 2015-2016 [email protected] (www.neomerx.com)
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\ErrorAggregatorInterface;
20
use Limoncello\Validation\Contracts\RuleInterface;
21
use Limoncello\Validation\Rules;
22
23
/**
24
 * @package Limoncello\Validation
25
 */
26
abstract class IterateRules extends BaseExpression
27
{
28
    /**
29
     * @var RuleInterface[]
30
     */
31
    private $rules;
32
33
    /**
34
     * @var null|bool
35
     */
36
    private $isStateless = null;
37
38
    /**
39
     * @param array $rules
40
     */
41 12
    public function __construct(array $rules)
42
    {
43 12
        $this->rules = $rules;
44 12
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 2
    public function isStateless()
50
    {
51 2
        if ($this->isStateless !== null) {
52 1
            return $this->isStateless;
53
        }
54
55 2
        foreach ($this->getRules() as $rule) {
56 2
            if ($rule->isStateless() === false) {
57 2
                return $this->isStateless = false;
58
            }
59
        }
60
61 1
        return $this->isStateless = true;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67 12
    public function onFinish(ErrorAggregatorInterface $aggregator)
68
    {
69 12
        foreach ($this->getRules() as $key => $rule) {
70 12
            $this->setParameterName($key);
71 12
            $rule->setParentRule($this);
72 12
            $rule->onFinish($aggregator);
73
        }
74 12
    }
75
76
    /**
77
     * @return RuleInterface[]
78
     */
79 12
    protected function getRules()
80
    {
81 12
        return $this->rules;
82
    }
83
}
84