Integer::isValid()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 20
rs 9.9
1
<?php
2
3
namespace Lavibi\Popoya;
4
5
class Integer extends AbstractValidator
6
{
7
    const NOT_INTEGER = 'not_integer';
8
    const NOT_INTEGER_MESSAGE = 'The given value must be integer.';
9
10
    protected $messages = [
11
        self::NOT_INTEGER => self::NOT_INTEGER_MESSAGE
12
    ];
13
14
    public function isValid($value): bool
15
    {
16
        $this->value = $this->standardValue = $value;
17
18
        if (is_string($value)) {
19
            if (!preg_match('/^[1-9][0-9]*$|^0$/', $value)) {
20
                $this->setError(static::NOT_INTEGER);
21
                return false;
22
            }
23
24
            $this->standardValue = (int) $value;
25
            return true;
26
        }
27
28
        if (is_int($value)) {
29
            return true;
30
        }
31
32
        $this->setError(self::NOT_INTEGER);
33
        return false;
34
    }
35
}
36