1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Ray.WebFormModule package. |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
6
|
|
|
*/ |
7
|
|
|
namespace Ray\WebFormModule; |
8
|
|
|
|
9
|
|
|
use Doctrine\Common\Annotations\Reader; |
10
|
|
|
use Ray\Aop\MethodInterceptor; |
11
|
|
|
use Ray\Aop\MethodInvocation; |
12
|
|
|
use Ray\WebFormModule\Annotation\AbstractValidation; |
13
|
|
|
use Ray\WebFormModule\Annotation\FormValidation; |
14
|
|
|
use Ray\WebFormModule\Exception\InvalidArgumentException; |
15
|
|
|
use Ray\WebFormModule\Exception\InvalidFormPropertyException; |
16
|
|
|
|
17
|
|
|
class AuraInputInterceptor implements MethodInterceptor |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Reader |
21
|
|
|
*/ |
22
|
|
|
protected $reader; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var FailureHandlerInterface |
26
|
|
|
*/ |
27
|
|
|
protected $failureHandler; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Reader $reader |
31
|
|
|
* @param FailureHandlerInterface $handler |
32
|
|
|
*/ |
33
|
13 |
|
public function __construct(Reader $reader, FailureHandlerInterface $handler) |
34
|
|
|
{ |
35
|
13 |
|
$this->reader = $reader; |
36
|
13 |
|
$this->failureHandler = $handler; |
37
|
13 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
* |
42
|
|
|
* @throws InvalidArgumentException |
43
|
|
|
*/ |
44
|
12 |
|
public function invoke(MethodInvocation $invocation) |
45
|
|
|
{ |
46
|
12 |
|
$object = $invocation->getThis(); |
47
|
|
|
/* @var $formValidation FormValidation */ |
48
|
12 |
|
$method = $invocation->getMethod(); |
49
|
11 |
|
$formValidation = $this->reader->getMethodAnnotation($method, AbstractValidation::class); |
50
|
11 |
|
$form = $this->getFormProperty($formValidation, $object); |
|
|
|
|
51
|
7 |
|
$data = $form instanceof SubmitInterface ? $form->submit() : $this->getNamedArguments($invocation); |
52
|
7 |
|
$isValid = $this->isValid($data, $form); |
53
|
7 |
|
if ($isValid === true) { |
54
|
|
|
// validation success |
55
|
1 |
|
return $invocation->proceed(); |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
return $this->failureHandler->handle($formValidation, $invocation, $form); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $submit |
63
|
|
|
* @param AbstractForm $form |
64
|
|
|
* |
65
|
|
|
* @throws Exception\CsrfViolationException |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
7 |
|
public function isValid(array $submit, AbstractForm $form) |
70
|
|
|
{ |
71
|
7 |
|
$isValid = $form->apply($submit); |
72
|
|
|
|
73
|
7 |
|
return $isValid; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return arguments as named arguments. |
78
|
|
|
* |
79
|
|
|
* @param MethodInvocation $invocation |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
7 |
|
private function getNamedArguments(MethodInvocation $invocation) |
|
|
|
|
84
|
|
|
{ |
85
|
7 |
|
$submit = []; |
86
|
7 |
|
$params = $invocation->getMethod()->getParameters(); |
87
|
7 |
|
$args = $invocation->getArguments()->getArrayCopy(); |
88
|
7 |
|
foreach ($params as $param) { |
89
|
7 |
|
$arg = array_shift($args); |
90
|
7 |
|
$submit[$param->getName()] = $arg; |
91
|
|
|
} |
92
|
|
|
// has token ? |
93
|
7 |
|
if (isset($_POST[AntiCsrf::TOKEN_KEY])) { |
94
|
|
|
$submit[AntiCsrf::TOKEN_KEY] = $_POST[AntiCsrf::TOKEN_KEY]; |
95
|
|
|
} |
96
|
|
|
|
97
|
7 |
|
return $submit; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return form property |
102
|
|
|
* |
103
|
|
|
* @param AbstractValidation $formValidation |
104
|
|
|
* @param object $object |
105
|
|
|
* |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
11 |
|
private function getFormProperty(AbstractValidation $formValidation, $object) |
109
|
|
|
{ |
110
|
11 |
|
if (! property_exists($object, $formValidation->form)) { |
111
|
3 |
|
throw new InvalidFormPropertyException($formValidation->form); |
112
|
|
|
} |
113
|
8 |
|
$prop = (new \ReflectionClass($object))->getProperty($formValidation->form); |
114
|
8 |
|
$prop->setAccessible(true); |
115
|
8 |
|
$form = $prop->getValue($object); |
116
|
8 |
|
if (! $form instanceof AbstractForm) { |
117
|
1 |
|
throw new InvalidFormPropertyException($formValidation->form); |
118
|
|
|
} |
119
|
|
|
|
120
|
7 |
|
return $form; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: