Completed
Push — master ( 5cfdc9...6f3eaf )
by Philip
02:18
created

Comparator::allNumeric()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 5
nc 3
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
/**
15
 * Validator for comparing values.
16
 */
17
abstract class Comparator extends ParametrizedValidator {
18
19
    /**
20
     * Holds the amount of parameters.
21
     */
22
    protected $amountOfParameters;
23
24
    /**
25
     * Performs the comparison.
26
     *
27
     * @param mixed $a
28
     * the first value to compare
29
     * @param mixed $parameters
30
     * the values to compare
31
     *
32
     * @return boolean
33
     * true if a compares to the values
34
     */
35
    abstract protected function compare($a, $parameters);
36
37
    /**
38
     * Checks whether all given parameters are numeric.
39
     *
40
     * @return boolean
41
     * true if all values are numeric
42
     */
43
    protected function allNumeric() {
44
        foreach (func_get_args() as $value) {
45
            if (!is_numeric($value)) {
46
                return false;
47
            }
48
        }
49
        return true;
50
    }
51
52
    /**
53
     * Constructor.
54
     */
55
    public function __construct() {
56
        $this->amountOfParameters = 1;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function validate($value, array $parameters) {
63
64
        $this->validateParameterCount('max', $this->amountOfParameters, $parameters);
65
66
        return in_array($value, array('', null), true) ||
67
            $this->compare($value, $parameters);
68
    }
69
}
70