AndOrConstraint::getConcreteFailedConstraint()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Validation\Constraint;
26
27
/**
28
 * AndOrConstraint
29
 *
30
 * Constraint to group AND and OR constraints which can be nested.
31
 * @author Celestino Diaz <[email protected]>
32
 */
33
class AndOrConstraint implements Constraint {
34
35
    /** @var array */
36
    private $constraints;
37
38
    /** @var null|\Brickoo\Component\Validation\Constraint\Constraint */
39
    private $failedConstraint;
40
41
    /**
42
     * Class constructor.
43
     * Can be called with multiple arguments of type array.
44
     * Each param array represents an AND group while
45
     * each param represents an OR group.
46
     * Example: new AndOrConstraint([constraint1, constraint2], [constraint3])
47
     * Either constraint1 AND constraint2 OR constraint3 MUST match.
48
     * @param array ...params
49
     */
50 1
    public function __construct() {
51 1
        $this->constraints = func_get_args();
52 1
    }
53
54
    /** {@inheritDoc} */
55 5
    public function matches($value) {
56 5
        $this->removeFailures();
57
58 5
        $matches = true;
59 5
        foreach ($this->constraints as $constraintGroup) {
60 5
            if (($matches = $this->doesConstraintGroupMatch($constraintGroup, $value))) {
61 3
                break;
62
            }
63 5
        }
64 5
        return $matches;
65
    }
66
67
    /**
68
     * Returns the last constraint which did not match.
69
     * @return null|\Brickoo\Component\Validation\Constraint\Constraint
70
     */
71 2
    public function getFailedConstraint() {
72 2
        return $this->failedConstraint;
73
    }
74
75
    /**
76
     * Remove the failures if any.
77
     * @return \Brickoo\Component\Validation\Constraint\AndOrConstraint
78
     */
79 3
    private function removeFailures() {
80 3
        $this->failedConstraint = null;
81 3
        return $this;
82
    }
83
84
    /**
85
     * Checks if a group of constraints do all match.
86
     * @param array $constraintGroup
87
     * @param mixed $value
88
     * @return boolean check result
89
     */
90 4
    private function doesConstraintGroupMatch(array $constraintGroup, $value) {
91 4
        $matches = true;
92 4
        foreach ($constraintGroup as $constraint) {
93 4
            if (!$constraint->matches($value)) {
94 4
                $this->failedConstraint = $this->getConcreteFailedConstraint($constraint);
95 4
                $matches = false;
96 4
                break;
97
            }
98 4
        }
99 4
        return $matches;
100
    }
101
102
    /**
103
     * Returns the concrete failed constraint.
104
     * @param \Brickoo\Component\Validation\Constraint\Constraint $constraint
105
     * @return null|\Brickoo\Component\Validation\Constraint\Constraint
106
     */
107 4
    private function getConcreteFailedConstraint(Constraint $constraint) {
108 4
        if ($constraint instanceof AndOrConstraint) {
109 1
            return $constraint->getFailedConstraint();
110
        }
111 4
        return $constraint;
112
    }
113
114
}
115