Skip to content

Commit e3daeee

Browse files
committed
Merge pull request #3 from ray-di/0.1.0
0.1.0
2 parents e1d88d0 + 2ec0f67 commit e3daeee

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

src/ValidateInterceptor.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ private function getOnValidate(\ReflectionMethod $method)
7373
* @param \ReflectionMethod $onFailure
7474
*
7575
* @return bool|mixed|InvalidArgumentException
76+
*
7677
* @throws \Exception
7778
*/
7879
private function validate(MethodInvocation $invocation, \ReflectionMethod $onValidate, \ReflectionMethod $onFailure = null)
@@ -139,19 +140,42 @@ private function findOnMethods(\ReflectionClass $class, Valid $valid)
139140
$onValidateMethod = $onFailureMethod = null;
140141
foreach ($class->getMethods() as $method) {
141142
$annotations = $this->reader->getMethodAnnotations($method);
142-
foreach ($annotations as $annotation) {
143-
if ($this->isOnValidateFound($annotation, $valid, $onValidateMethod)) {
144-
$onValidateMethod = $method;
145-
}
146-
if ($this->isOnFailureFound($annotation, $valid, $onFailureMethod)) {
147-
$onFailureMethod = $method;
148-
}
149-
}
143+
list($onValidateMethod, $onFailureMethod) = $this->scanAnnotation($valid, $annotations, $method,
144+
$onValidateMethod, $onFailureMethod
145+
);
150146
}
151147

152148
return [$onValidateMethod, $onFailureMethod];
153149
}
154150

151+
/**
152+
* @param Valid $valid
153+
* @param array $annotations
154+
* @param \ReflectionMethod $method
155+
* @param \ReflectionMethod $onValidateMethod
156+
* @param \ReflectionMethod $onFailureMethod
157+
*
158+
* @return array
159+
*/
160+
private function scanAnnotation(
161+
Valid $valid,
162+
array $annotations,
163+
\ReflectionMethod $method,
164+
\ReflectionMethod $onValidateMethod = null,
165+
\ReflectionMethod $onFailureMethod = null
166+
) {
167+
foreach ($annotations as $annotation) {
168+
if ($this->isOnValidateFound($annotation, $valid, $onValidateMethod)) {
169+
$onValidateMethod = $method;
170+
}
171+
if ($this->isOnFailureFound($annotation, $valid, $onFailureMethod)) {
172+
$onFailureMethod = $method;
173+
}
174+
}
175+
176+
return [$onValidateMethod ,$onFailureMethod];
177+
}
178+
155179
/**
156180
* @param object $annotation
157181
* @param Valid $valid

src/Validation.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@
1010

1111
class Validation implements ValidationInterface
1212
{
13+
/**
14+
* @var array [$name =>]
15+
*/
1316
private $failure = [];
1417

18+
/**
19+
* @var MethodInvocation
20+
*/
1521
private $invocation;
1622

1723
public function __construct(array $failure = [])
@@ -24,16 +30,26 @@ public function setInvocation(MethodInvocation $invocation)
2430
$this->invocation = $invocation;
2531
}
2632

33+
/**
34+
* @param string $name error target name
35+
* @param string $message message
36+
*/
2737
public function addError($name, $message)
2838
{
2939
$this->failure[$name][] = $message;
3040
}
3141

42+
/**
43+
* @return array
44+
*/
3245
public function getMessages()
3346
{
3447
return $this->failure;
3548
}
3649

50+
/**
51+
* @return MethodInvocation
52+
*/
3753
public function getInvocation()
3854
{
3955
return $this->invocation;

0 commit comments

Comments
 (0)