Completed
Push — master ( 984d76...8152ca )
by WEBEWEB
04:38
created

AbstractValidationRuleSet   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 75
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addRule() 0 4 1
A getRules() 0 3 1
A removeRule() 0 9 3
A setRules() 0 4 1
A validate() 0 16 3
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\Validator\RuleSet;
13
14
use WBW\Library\Core\Validator\API\ValidationRuleInterface;
15
use WBW\Library\Core\Validator\API\ValidationRuleSetInterface;
16
17
/**
18
 * Abstract validation rule set.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Validator\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 removeRule(ValidationRuleInterface $rule) {
59
        for ($i = count($this->rules) - 1; 0 <= $i; --$i) {
60
            if ($rule !== $this->rules[$i]) {
61
                continue;
62
            }
63
            unset($this->rules[$i]);
64
        }
65
        return $this;
66
    }
67
68
    /**
69
     * Set the rules.
70
     *
71
     * @param ValidationRuleInterface[] $rules The rules.
72
     * @return ValidationRuleSetInterface Returns this validation rule set.
73
     */
74
    public function setRules(array $rules) {
75
        $this->rules = $rules;
76
        return $this;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function validate($object) {
83
84
        $result = [];
85
86
        foreach ($this->rules as $current) {
87
88
            $status = $current->validate($object);
89
            if (null === $status->getRuleName()) {
90
                $status->setRuleName($current->getName());
91
            }
92
93
            $result [] = $status;
94
        }
95
96
        return $result;
97
    }
98
}
99