AbstractComparator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 11
dl 0
loc 56
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isAllNumeric() 0 8 3
A isValid() 0 7 2
A getInvalidDetails() 0 3 1
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 AbstractComparator extends AbstractParametrizedValidator
18
{
19
20
    /**
21
     * Holds the amount of parameters.
22
     */
23
    protected $amountOfParameters = 1;
24
25
    /**
26
     * Holds the type of the validator.
27
     */
28
    protected $type;
29
30
    /**
31
     * Performs the comparison.
32
     *
33
     * @param mixed $value the first value to compare
34
     * @param mixed $parameters the values to compare
35
     *
36
     * @return boolean - true if value compares to the parameters
37
     */
38
    abstract protected function isValidComparison($value, $parameters);
39
40
    /**
41
     * Checks whether all given parameters are numeric.
42
     *
43
     * @return boolean - true if all values are numeric
44
     */
45 7
    protected function isAllNumeric()
46
    {
47 7
        foreach (func_get_args() as $value) {
48 7
            if (!is_numeric($value)) {
49 7
                return false;
50
            }
51
        }
52 7
        return true;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 15
    public function isValid($value, array $parameters)
59
    {
60
61 15
        $this->validateParameterCount($this->type, $this->amountOfParameters, $parameters);
62
63 15
        return in_array($value, ['', null], true) ||
64 15
            $this->isValidComparison($value, $parameters);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3
    public function getInvalidDetails()
71
    {
72 3
        return $this->type;
73
    }
74
}
75