Completed
Push — master ( d26f0c...86a533 )
by WEBEWEB
01:55
created

AbstractValidationRuleSet::getRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Validation\RuleSet;
13
14
use WBW\Library\Core\Validation\API\ValidationRuleInterface;
15
use WBW\Library\Core\Validation\API\ValidationRuleSetInterface;
16
17
/**
18
 * Abstract validation rule set.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Validation\RuleSet
22
 * @abstract
23
 */
24
abstract class AbstractValidationRuleSet implements ValidationRuleSetInterface {
25
26
    /**
27
     * Rules.
28
     *
29
     * @var ValidationRuleInterface[]
30
     */
31
    private $rules;
32
33
    /**
34
     * Constructor.
35
     */
36
    protected function __construct() {
37
        $this->setRules([]);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function addRule(ValidationRuleInterface $rule) {
44
        $this->rules[] = $rule;
45
        return $this;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getRules() {
52
        return $this->rules;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function isValid($object) {
59
        $result = [];
60
        foreach ($this->rules as $current) {
61
            $result [] = $current->isValid($object);
62
        }
63
        return $result;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function removeRule(ValidationRuleInterface $rule) {
70
        for ($i = count($this->rules) - 1; 0 <= $i; --$i) {
71
            if ($rule !== $this->rules[$i]) {
72
                continue;
73
            }
74
            unset($this->rules[$i]);
75
        }
76
        return $this;
77
    }
78
79
    /**
80
     * Set the rules.
81
     *
82
     * @param ValidationRuleInterface[] $rules The rules.
83
     * @return ValidatuionRuleSetInterface Returns this validation rule set.
84
     */
85
    public function setRules(array $rules) {
86
        $this->rules = $rules;
87
        return $this;
88
    }
89
90
}
91