Passed
Push — master ( c9351b...a8a6df )
by Smoren
02:20
created

IntegerRule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 41
ccs 19
cts 19
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A even() 0 6 1
A odd() 0 6 1
A __construct() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Rules;
6
7
use Smoren\Validator\Checks\Check;
8
use Smoren\Validator\Interfaces\IntegerRuleInterface;
9
use Smoren\Validator\Structs\CheckErrorName;
10
use Smoren\Validator\Structs\CheckName;
11
12
class IntegerRule extends NumericRule implements IntegerRuleInterface
13
{
14
    /**
15
     * @param string $name
16
     */
17 57
    public function __construct(string $name)
18
    {
19 57
        Rule::__construct($name);
20 57
        $this->check(new Check(
21 57
            CheckName::INTEGER,
22 57
            CheckErrorName::NOT_INTEGER,
23 57
            fn ($value) => is_int($value),
24 57
            []
25 57
        ), true);
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     *
31
     * @return static
32
     */
33 11
    public function even(): self
34
    {
35 11
        return $this->check(new Check(
36 11
            CheckName::EVEN,
37 11
            CheckErrorName::NOT_EVEN,
38 11
            fn ($value) => $value % 2 === 0,
39 11
        ));
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     *
45
     * @return static
46
     */
47 12
    public function odd(): self
48
    {
49 12
        return $this->check(new Check(
50 12
            CheckName::ODD,
51 12
            CheckErrorName::NOT_ODD,
52 12
            fn ($value) => $value % 2 !== 0
53 12
        ));
54
    }
55
}
56