Passed
Push — master ( 151935...bf98f6 )
by Smoren
02:25
created

Check   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 68
ccs 5
cts 15
cp 0.3333
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 4 2
A isInterrupting() 0 3 1
A __construct() 0 6 1
A setInterrupting() 0 4 1
A getName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Structs;
6
7
use Smoren\Validator\Exceptions\CheckError;
8
use Smoren\Validator\Interfaces\CheckInterface;
9
10
class Check implements CheckInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    protected string $name;
16
    /**
17
     * @var callable
18
     */
19
    protected $predicate;
20
    /**
21
     * @var array<string, mixed>
22
     */
23
    protected array $params;
24
    /**
25
     * @var bool
26
     */
27
    protected bool $isInterrupting;
28
29
    /**
30
     * @param string $name
31
     * @param callable $predicate
32
     * @param array<string, mixed> $params
33
     * @param bool $isInterrupting
34
     */
35
    public function __construct(string $name, callable $predicate, array $params = [], bool $isInterrupting = false)
36
    {
37
        $this->name = $name;
38
        $this->predicate = $predicate;
39
        $this->params = $params;
40
        $this->isInterrupting = $isInterrupting;
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46 57
    public function execute($value, array $previousErrors): void
47
    {
48 57
        if (($this->predicate)($value, ...array_values($this->params)) === false) {
49 35
            throw new CheckError($this->name, $value, $this->params);
50
        }
51
    }
52
53
    /**
54
     * @return bool
55
     */
56 35
    public function isInterrupting(): bool
57
    {
58 35
        return $this->isInterrupting;
59
    }
60
61
    /**
62
     * @param bool $value
63
     *
64
     * @return static
65
     */
66
    public function setInterrupting(bool $value = true): CheckInterface
67
    {
68
        $this->isInterrupting = $value;
69
        return $this;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function getName(): string
76
    {
77
        return $this->name;
78
    }
79
}
80