Completed
Push — master ( 5ad706...418f81 )
by Sebastian
03:53
created

Number::concreteValidate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Linna Filter
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types = 1);
11
12
namespace Linna\Filter\Rules;
13
14
/**
15
 * Check if provided value is a number.
16
 */
17
class Number extends AbstractNumber implements RuleSanitizeInterface, RuleValidateInterface
18
{
19
    /**
20
     * @var array Rule properties
21
     */
22
    public static $config = [
23
        'class' => 'Number',
24
        'full_class' => __CLASS__,
25
        'alias' => ['number', 'num', 'n'],
26
        'args_count' => 0,
27
        'args_type' => [],
28
        'has_validate' => true,
29
        //'has_sanitize' => true
30
    ];
31
32
    /**
33
     * @var string Error message
34
     */
35
    private $message = '';
36
37
    /**
38
     * Validate.
39
     *
40
     * @return bool
41
     */
42 19
    public function validate(): bool
43
    {
44 19
        $args = func_get_args();
45
46 19
        return $this->concreteValidate($args[0]);
47
    }
48
49
    /**
50
     * Concrete validate.
51
     *
52
     * @param int|float $received
53
     *
54
     * @return bool
55
     */
56 19
    private function concreteValidate($received): bool
57
    {
58 19
        if (!is_numeric($received)) {
0 ignored issues
show
introduced by
The condition is_numeric($received) is always true.
Loading history...
59 9
            $this->message = "Received value is not a number";
60 9
            return true;
61
        }
62
63 10
        return false;
64
    }
65
66
    /**
67
     * Return error message.
68
     *
69
     * @return string Error message
70
     */
71 1
    public function getMessage(): string
72
    {
73 1
        return $this->message;
74
    }
75
}
76