IsInstanceOf   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 4
b 0
f 0
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 11 4
A class() 0 5 1
1
<?php
2
/**
3
 * @author    Nurlan Mukhanov <[email protected]>
4
 * @copyright 2022 Nurlan Mukhanov
5
 * @license   https://en.wikipedia.org/wiki/MIT_License MIT License
6
 * @link      https://github.com/Falseclock/service-layer
7
 */
8
9
declare(strict_types=1);
10
11
namespace Falseclock\Service\Validation\Validators;
12
13
use Falseclock\Service\Validation\ValidationException;
14
use Falseclock\Service\Validation\ValidatorImpl;
15
16
class IsInstanceOf extends ValidatorImpl
17
{
18
    public const ERROR_NOT_CLASS_DEFINED = "No class defined";
19
20
    /** @var string */
21
    protected $class;
22
23
    /**
24
     * @param $value
25
     * @return bool
26
     * @throws ValidationException
27
     */
28
    public function check($value = null): bool
29
    {
30
        if (is_null($value) && $this->nullable) {
31
            return true;
32
        }
33
34
        if (is_null($this->class)) {
0 ignored issues
show
introduced by
The condition is_null($this->class) is always false.
Loading history...
35
            throw new ValidationException(self::ERROR_NOT_CLASS_DEFINED);
36
        }
37
38
        return $value instanceof $this->class;
39
    }
40
41
    /**
42
     * @param string $className
43
     * @return $this
44
     */
45
    public function class(string $className): IsInstanceOf
46
    {
47
        $this->class = $className;
48
49
        return $this;
50
    }
51
}
52