Falseclock /
service-layer
| 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
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 |