Passed
Push — master ( cdfa93...96b6af )
by Smoren
02:36
created

IntegerRule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 14
c 1
b 0
f 0
dl 0
loc 39
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A even() 0 5 1
A odd() 0 5 1
1
<?php
2
3
namespace Smoren\Validator\Rules;
4
5
use Smoren\Validator\Interfaces\IntegerRuleInterface;
6
use Smoren\Validator\Interfaces\NumberRuleInterface;
7
use Smoren\Validator\Structs\Check;
8
9
class IntegerRule extends NumberRule implements IntegerRuleInterface
10
{
11
    public const ERROR_NOT_INTEGER = 'not_integer';
12
    public const ERROR_NOT_EVEN = 'not_even';
13
    public const ERROR_NOT_ODD = 'not_odd';
14
15 40
    public function __construct()
16
    {
17 40
        $this->add(new Check(
18 40
            self::ERROR_NOT_INTEGER,
19 40
            fn ($value) => is_int($value),
20 40
            [],
21 40
            true
22 40
        ));
23
    }
24
25
    /**
26
     * {@inheritDoc}
27
     *
28
     * @return static
29
     */
30 7
    public function even(): self
31
    {
32 7
        return $this->add(new Check(
33 7
            self::ERROR_NOT_EVEN,
34 7
            fn ($value) => $value % 2 === 0,
35 7
        ));
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     *
41
     * @return static
42
     */
43 10
    public function odd(): self
44
    {
45 10
        return $this->add(new Check(
46 10
            self::ERROR_NOT_ODD,
47 10
            fn ($value) => $value % 2 !== 0
48 10
        ));
49
    }
50
}
51