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

Comparator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
compare() 0 1 ?
A allNumeric() 0 8 3
A __construct() 0 3 1
A validate() 0 7 2
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