Completed
Push — master ( 666e0b...f8cd2e )
by Philip
02:25
created

OrCombine::getInvalidDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     * Holds the invalid validators.
24
     */
25
    protected $invalid;
26
27
    /**
28
     * Checks whether the given parameters fullfil:
29
     * - At least three given
30
     * - The first one is a Validator or a subclass of it
31
     *
32
     * @param array $parameters
33
     * the validation parameters
34
     */
35
    protected function checkParameters($parameters) {
36
        if (count($parameters) < 3) {
37
            throw new ValidationException('"or" expects at least 3 parameters.');
38
        }
39
        if (!($parameters[0] instanceof Validator)) {
40
            throw new ValidationException('"or" expects the first parameter to be a Validator or a subclass of it.');
41
        }
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function isValid($value, array $parameters) {
48
49
        $this->checkParameters($parameters);
50
51
        $validator = array_shift($parameters);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
52
        $this->invalidDetails = array();
0 ignored issues
show
Bug introduced by
The property invalidDetails does not seem to exist. Did you mean invalid?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53
        $valid = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 16 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54
        foreach ($parameters as $rules) {
55
            $failedValidations = $validator->isValidValue(array($rules), $value);
56
            foreach ($failedValidations as $failedValidation) {
57
                $this->invalidDetails[] = $failedValidation;
0 ignored issues
show
Bug introduced by
The property invalidDetails does not seem to exist. Did you mean invalid?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
58
            }
59
            $valid = $valid || empty($failedValidations);
60
        }
61
62
        return $valid;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getInvalidDetails() {
69
        return array('or' => $this->invalidDetails);
0 ignored issues
show
Bug introduced by
The property invalidDetails does not seem to exist. Did you mean invalid?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
70
    }
71
72
}
73