Integer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 29
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 20 4
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