Passed
Push — master ( dd9897...8de557 )
by Smoren
02:06
created

BaseRule::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
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Smoren\Validator\Rules;
4
5
use Smoren\Validator\Exceptions\ValidationSuccessException;
6
use Smoren\Validator\Exceptions\ValidationError;
7
use Smoren\Validator\Interfaces\BaseRuleInterface;
8
9
abstract class BaseRule 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 ValidationSuccessException
33
     */
34 50
    public function validate($value): void
35
    {
36 50
        if ($value === null) {
37 8
            if ($this->isNullable) {
38 5
                throw new ValidationSuccessException();
39
            }
40
41 3
            throw new ValidationError($value, [[self::ERROR_NULL, []]]);
42
        }
43
    }
44
}
45