2010-12-06 22:00:28 +01:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
2019-05-23 16:21:34 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE file
|
|
|
|
* that was distributed with this source code.
|
2015-06-08 17:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2010-12-06 22:00:28 +01:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2018-08-18 15:24:41 +02:00
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
|
|
|
use Respect\Validation\Validatable;
|
2018-08-21 10:39:43 +02:00
|
|
|
use Throwable;
|
2018-08-18 15:24:41 +02:00
|
|
|
use function call_user_func;
|
|
|
|
use function restore_error_handler;
|
|
|
|
use function set_error_handler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates the return of a callable for a given input.
|
|
|
|
*
|
|
|
|
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
*/
|
|
|
|
final class Call extends AbstractRule
|
2010-12-06 22:00:28 +01:00
|
|
|
{
|
2018-08-18 15:24:41 +02:00
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $callable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Validatable
|
|
|
|
*/
|
|
|
|
private $rule;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the rule with the callable to be executed after the input is passed.
|
|
|
|
*/
|
|
|
|
public function __construct(callable $callable, Validatable $rule)
|
2010-12-06 22:00:28 +01:00
|
|
|
{
|
2018-08-18 15:24:41 +02:00
|
|
|
$this->callable = $callable;
|
|
|
|
$this->rule = $rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2018-08-18 15:24:41 +02:00
|
|
|
*/
|
|
|
|
public function assert($input): void
|
|
|
|
{
|
|
|
|
$this->setErrorHandler($input);
|
|
|
|
|
2018-08-21 10:39:43 +02:00
|
|
|
try {
|
|
|
|
$this->rule->assert(call_user_func($this->callable, $input));
|
|
|
|
} catch (ValidationException $exception) {
|
|
|
|
throw $exception;
|
|
|
|
} catch (Throwable $throwable) {
|
|
|
|
throw $this->reportError($input);
|
2020-04-29 02:21:31 +02:00
|
|
|
} finally {
|
|
|
|
restore_error_handler();
|
2018-08-21 10:39:43 +02:00
|
|
|
}
|
2018-08-18 15:24:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2018-08-18 15:24:41 +02:00
|
|
|
*/
|
|
|
|
public function check($input): void
|
|
|
|
{
|
|
|
|
$this->setErrorHandler($input);
|
|
|
|
|
2018-08-21 10:39:43 +02:00
|
|
|
try {
|
|
|
|
$this->rule->check(call_user_func($this->callable, $input));
|
|
|
|
} catch (ValidationException $exception) {
|
|
|
|
throw $exception;
|
|
|
|
} catch (Throwable $throwable) {
|
|
|
|
throw $this->reportError($input);
|
2020-04-29 02:21:31 +02:00
|
|
|
} finally {
|
|
|
|
restore_error_handler();
|
2018-08-21 10:39:43 +02:00
|
|
|
}
|
2018-08-18 15:24:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2018-08-18 15:24:41 +02:00
|
|
|
*/
|
|
|
|
public function validate($input): bool
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->check($input);
|
|
|
|
} catch (ValidationException $exception) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-12-06 22:00:28 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $input
|
|
|
|
*/
|
2018-08-18 15:24:41 +02:00
|
|
|
private function setErrorHandler($input): void
|
2010-12-06 22:00:28 +01:00
|
|
|
{
|
2018-08-18 15:24:41 +02:00
|
|
|
set_error_handler(function () use ($input): void {
|
|
|
|
throw $this->reportError($input);
|
|
|
|
});
|
2010-12-06 22:00:28 +01:00
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|