1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Respect/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Respect\Validation\Rules; |
15
|
|
|
|
16
|
|
|
use Respect\Validation\Exceptions\ValidationException; |
17
|
|
|
use Respect\Validation\Validatable; |
18
|
|
|
use function call_user_func; |
19
|
|
|
use function restore_error_handler; |
20
|
|
|
use function set_error_handler; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
24
|
|
|
* @author Emmerson Siqueira <[email protected]> |
25
|
|
|
* @author Henrique Moody <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class Call extends AbstractRule |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var callable |
31
|
|
|
*/ |
32
|
|
|
private $callable; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Validatable |
36
|
|
|
*/ |
37
|
|
|
private $rule; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Initializes the rule with the callable to be executed after the input is passed. |
41
|
|
|
* |
42
|
|
|
* @param callable $callable |
43
|
|
|
* @param Validatable $rule |
44
|
|
|
*/ |
45
|
11 |
|
public function __construct(callable $callable, Validatable $rule) |
46
|
|
|
{ |
47
|
11 |
|
$this->callable = $callable; |
48
|
11 |
|
$this->rule = $rule; |
49
|
11 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
6 |
|
public function assert($input): void |
55
|
|
|
{ |
56
|
6 |
|
$this->setErrorHandler($input); |
57
|
|
|
|
58
|
6 |
|
$this->rule->assert(call_user_func($this->callable, $input)); |
59
|
|
|
|
60
|
1 |
|
restore_error_handler(); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
6 |
|
public function check($input): void |
67
|
|
|
{ |
68
|
6 |
|
$this->setErrorHandler($input); |
69
|
|
|
|
70
|
6 |
|
$this->rule->check(call_user_func($this->callable, $input)); |
71
|
|
|
|
72
|
3 |
|
restore_error_handler(); |
73
|
3 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
4 |
|
public function validate($input): bool |
79
|
|
|
{ |
80
|
|
|
try { |
81
|
4 |
|
$this->check($input); |
82
|
2 |
|
} catch (ValidationException $exception) { |
83
|
2 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function setErrorHandler($input): void |
90
|
|
|
{ |
91
|
11 |
|
set_error_handler(function () use ($input): void { |
92
|
4 |
|
throw $this->reportError($input); |
93
|
11 |
|
}); |
94
|
11 |
|
} |
95
|
|
|
} |
96
|
|
|
|