Completed
Push — master ( 7f047f...6680e2 )
by Philip
07:06
created

OrCombine   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkParameters() 0 8 3
A isValid() 0 14 3
1
<?php
2
3
/*
4
 * This file is part of the Valdi package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
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 Valdi\Validator;
13
14
use Valdi\Validator;
15
use Valdi\ValidationException;
16
17
/**
18
 * Validator to combine other validators with a logical "or".
19
 */
20
class OrCombine implements ValidatorInterface {
21
22
    /**
23
     * Checks whether the given parameters fullfil:
24
     * - At least three given
25
     * - The first one is a Validator or a subclass of it
26
     *
27
     * @param array $parameters
28
     * the validation parameters
29
     */
30
    protected function checkParameters($parameters) {
31
        if (count($parameters) < 3) {
32
            throw new ValidationException('"or" expects at least 3 parameters.');
33
        }
34
        if (!($parameters[0] instanceof Validator)) {
35
            throw new ValidationException('"or" expects the first parameter to be a Validator or a subclass of it.');
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function isValid($value, array $parameters) {
43
44
        $this->checkParameters($parameters);
45
46
        $validator = array_shift($parameters);
47
        foreach ($parameters as $rules) {
48
            $result = $validator->isValidValue(array($rules), $value);
49
            if (empty($result)) {
50
                return true;
51
            }
52
        }
53
54
		return false;
55
	}
56
57
}
58