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 Error; |
17
|
|
|
use Respect\Validation\Exceptions\ValidationException; |
18
|
|
|
use Respect\Validation\Validatable; |
19
|
|
|
use function call_user_func; |
20
|
|
|
use function restore_error_handler; |
21
|
|
|
use function set_error_handler; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Validates the return of a callable for a given input. |
25
|
|
|
* |
26
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
27
|
|
|
* @author Emmerson Siqueira <[email protected]> |
28
|
|
|
* @author Henrique Moody <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class Call extends AbstractRule |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var callable |
34
|
|
|
*/ |
35
|
|
|
private $callable; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Validatable |
39
|
|
|
*/ |
40
|
|
|
private $rule; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Initializes the rule with the callable to be executed after the input is passed. |
44
|
|
|
* |
45
|
|
|
* @param callable $callable |
46
|
|
|
* @param Validatable $rule |
47
|
|
|
*/ |
48
|
11 |
|
public function __construct(callable $callable, Validatable $rule) |
49
|
|
|
{ |
50
|
11 |
|
$this->callable = $callable; |
51
|
11 |
|
$this->rule = $rule; |
52
|
11 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
6 |
|
public function assert($input): void |
58
|
|
|
{ |
59
|
6 |
|
$this->setErrorHandler($input); |
60
|
|
|
|
61
|
|
|
try { |
62
|
6 |
|
$this->rule->assert(call_user_func($this->callable, $input)); |
63
|
5 |
|
} catch (Error $error) { |
64
|
|
|
throw $this->reportError($input); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
restore_error_handler(); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
6 |
|
public function check($input): void |
74
|
|
|
{ |
75
|
6 |
|
$this->setErrorHandler($input); |
76
|
|
|
|
77
|
|
|
try { |
78
|
6 |
|
$this->rule->check(call_user_func($this->callable, $input)); |
79
|
4 |
|
} catch (Error $error) { |
80
|
|
|
throw $this->reportError($input); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
restore_error_handler(); |
84
|
3 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
4 |
|
public function validate($input): bool |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
4 |
|
$this->check($input); |
93
|
2 |
|
} catch (ValidationException $exception) { |
94
|
2 |
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function setErrorHandler($input): void |
101
|
|
|
{ |
102
|
11 |
|
set_error_handler(function () use ($input): void { |
103
|
4 |
|
throw $this->reportError($input); |
104
|
11 |
|
}); |
105
|
11 |
|
} |
106
|
|
|
} |
107
|
|
|
|