Passed
Push — master ( 5369ce...cf86e2 )
by Smoren
02:17
created

NullableRule::nullable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Smoren\Validator\Rules;
4
5
use Smoren\Validator\Exceptions\StopValidationException;
6
use Smoren\Validator\Exceptions\ValidationError;
7
use Smoren\Validator\Interfaces\BaseRuleInterface;
8
9
abstract class NullableRule implements BaseRuleInterface
10
{
11
    public const ERROR_NULL = 'null';
12
13
    /**
14
     * @var bool
15
     */
16
    protected bool $isNullable = false;
17
18
    /**
19
     * {@inheritDoc}
20
     *
21
     * @return static
22
     */
23
    public function nullable(): self
24
    {
25
        $this->isNullable = true;
26
        return $this;
27
    }
28
29
    /**
30
     * {@inheritDoc}
31
     *
32
     * @throws StopValidationException
33
     */
34 38
    public function validate($value): void
35
    {
36 38
        if ($value === null) {
37 4
            if ($this->isNullable) {
38 3
                throw new StopValidationException();
39
            }
40
41 1
            throw new ValidationError($value, [[self::ERROR_NULL, []]]);
42
        }
43
    }
44
}
45