Integer::validate()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 4
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 6
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace FMUP\Import\Config\Field\Validator;
3
4
use FMUP\Import\Config\Field\Validator;
5
6
class Integer implements Validator
7
{
8
    private $empty;
9
10 3
    public function __construct($empty = false)
11
    {
12 3
        $this->setCanEmpty($empty);
13 3
    }
14
15 3
    public function setCanEmpty($empty = false)
16
    {
17 3
        $this->empty = (bool)$empty;
18 3
        return $this;
19
    }
20
21 2
    public function getCanEmpty()
22
    {
23 2
        return (bool)$this->empty;
24
    }
25
26 1
    public function validate($value)
27
    {
28 1
        if ($this->getCanEmpty() && $value == '') {
29 1
            return true;
30
        }
31 1
        if (is_float($value) && ((int)$value === 0)) {
32 1
            return false;
33
        }
34 1
        $valueTest = (int) $value;
35 1
        return ($valueTest === 0) ? is_numeric($value) : ($valueTest == $value);
36
    }
37
38 1
    public function getErrorMessage()
39
    {
40 1
        return "Le champ reçu n'est pas un nombre entier";
41
    }
42
}
43