Passed
Push — master ( bc036e...f8a9bb )
by Smoren
02:19
created

IntegerRule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A even() 0 6 1
A odd() 0 6 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 50
    public function __construct(string $name)
18
    {
19
        Rule::__construct($name);
20 50
        $this->check(new Check(
21 50
            CheckName::INTEGER,
22 50
            CheckErrorName::NOT_INTEGER,
23 50
            fn ($value) => is_int($value),
24 50
            []
25 50
        ), true);
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     *
31
     * @return static
32
     */
33 9
    public function even(): self
34
    {
35 9
        return $this->check(new Check(
36 9
            CheckName::EVEN,
37 9
            CheckErrorName::NOT_EVEN,
38 9
            fn ($value) => $value % 2 === 0,
39 9
        ));
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