IsSame::against()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 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 IsSame extends ValidatorImpl
17
{
18
    public const ERROR_NO_MATCH = "No value to compare. Use 'against' function.";
19
    /** @var mixed */
20
    protected $against;
21
22
    /**
23
     * @param mixed $value
24
     * @return $this
25
     */
26
    public function against($value): IsSame
27
    {
28
        $this->against = $value;
29
30
        return $this;
31
    }
32
33
    /**
34
     * @param mixed $value
35
     * @return bool
36
     * @throws ValidationException
37
     */
38
    public function check($value = null): bool
39
    {
40
        if (is_null($value) && $this->nullable) {
41
            return true;
42
        }
43
44
        if (!isset($this->against))
45
            throw new ValidationException(self::ERROR_NO_MATCH);
46
47
        if ($value === $this->against) {
48
            return true;
49
        }
50
51
        return false;
52
    }
53
}
54