Passed
Push — master ( 2b74c9...8099cd )
by Smoren
02:17
created

Rule::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Checks\RetrospectiveCheck;
9
use Smoren\Validator\Exceptions\CheckError;
10
use Smoren\Validator\Exceptions\ValidationError;
11
use Smoren\Validator\Interfaces\CheckInterface;
12
use Smoren\Validator\Interfaces\CheckWrapperInterface;
13
use Smoren\Validator\Interfaces\RuleInterface;
14
use Smoren\Validator\Interfaces\ValidationResultInterface;
15
use Smoren\Validator\Structs\CheckWrapper;
16
use Smoren\Validator\Structs\ValidationSuccessResult;
17
18
class Rule extends BaseRule implements RuleInterface
19
{
20
    public const ERROR_NULL = 'null';
21
    public const ERROR_NOT_TRUTHY = 'not_truthy';
22
    public const ERROR_NOT_FALSY = 'not_falsy';
23
    public const ERROR_NOT_EQUEAL = 'equal';
24
    public const ERROR_NOT_SAME = 'same';
25
26
    /**
27
     * @var array<CheckWrapperInterface>
28
     */
29
    protected array $checks = [];
30
    /**
31
     * @var bool
32
     */
33
    protected bool $isNullable = false;
34
35
    /**
36
     * {@inheritDoc}
37
     *
38
     * @return static
39
     */
40
    public function nullable(): self
41
    {
42
        $this->isNullable = true;
43
        return $this;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     *
49
     * @return static
50
     */
51
    public function truthy(): self
52
    {
53
        return $this->check(new Check(
54
            self::ERROR_NOT_TRUTHY,
55
            fn ($value) => boolval($value),
56
        ));
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     *
62
     * @return static
63
     */
64
    public function falsy(): self
65
    {
66
        return $this->check(new Check(
67
            self::ERROR_NOT_FALSY,
68
            fn ($value) => !boolval($value),
69
        ));
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     *
75
     * @return static
76
     */
77
    public function equal($values): self
78
    {
79
        return $this->check(new Check(
80
            self::ERROR_NOT_EQUEAL,
81
            fn ($value) => $value == $values,
82
            ['number' => $values]
83
        ));
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     *
89
     * @return static
90
     */
91
    public function same($value): self
92
    {
93
        return $this->check(new Check(
94
            self::ERROR_NOT_SAME,
95
            fn ($value) => $value === $value,
96
            ['number' => $value]
97
        ));
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     *
103
     * @return static
104
     */
105
    public function check(CheckInterface $check, bool $isInterrupting = false): self
106
    {
107
        $this->checks[] = new CheckWrapper($check, $isInterrupting);
108
        return $this;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     *
114
     * @return static
115
     */
116
    public function stopOnViolation(): self
117
    {
118
        return $this->check(new RetrospectiveCheck());
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     *
124
     * @return static
125
     */
126
    public function stopOnAnyPriorViolation(): self
127
    {
128
        foreach ($this->checks as $check) {
129
            $check->setInterrupting();
130
        }
131
        return $this;
132
    }
133
134
    /**
135
     * {@inheritDoc}
136
     */
137 68
    public function validate($value): void
138
    {
139 68
        $this->execute($value);
140
    }
141
142
    /**
143
     * {@inheritDoc}
144
     */
145
    public function isValid($value): bool
146
    {
147
        try {
148
            $this->validate($value);
149
            return true;
150
        } catch (ValidationError $e) {
151
            return false;
152
        }
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158 68
    protected function execute($value): ValidationResultInterface
159
    {
160 68
        $result = parent::execute($value);
161 68
        if ($result->preventNextChecks()) {
162
            return $result;
163
        }
164
165 68
        if ($value === null) {
166 8
            if ($this->isNullable) {
167 5
                return new ValidationSuccessResult(true);
168
            }
169
170 3
            throw new ValidationError($value, [[self::ERROR_NULL, []]]);
171
        }
172
173 65
        $errors = [];
174
175 65
        foreach ($this->checks as $check) {
176
            try {
177 64
                $check->getCheck()->execute($value, $errors);
178 39
            } catch (CheckError $e) {
179 39
                $errors[] = $e;
180 39
                if ($check->isInterrupting()) {
181 12
                    throw ValidationError::fromCheckErrors($value, $errors);
182
                }
183
            }
184
        }
185
186 57
        if (\count($errors) > 0) {
187 27
            throw ValidationError::fromCheckErrors($value, $errors);
188
        }
189
190 33
        return new ValidationSuccessResult(false);
191
    }
192
}
193