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

IntegerRule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1.0019

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
ccs 7
cts 8
cp 0.875
crap 1.0019
rs 10
c 1
b 0
f 0
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