|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Cubiche package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Cubiche |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Cubiche\Core\Validator\Mapping\Driver; |
|
12
|
|
|
|
|
13
|
|
|
use Cubiche\Core\Validator\Assert; |
|
14
|
|
|
use Cubiche\Core\Validator\Mapping\ClassMetadata; |
|
15
|
|
|
use Cubiche\Core\Validator\Mapping\Exception\MappingException; |
|
16
|
|
|
use Metadata\Driver\DriverInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* StaticDriver class. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class StaticDriver implements DriverInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The name of the method to call. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $methodName; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $defaultGroup; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* StaticDriver constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $methodName |
|
41
|
|
|
* @param string $defaultGroup |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct($methodName = 'loadValidatorMetadata', $defaultGroup = Assert::DEFAULT_GROUP) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->methodName = $methodName; |
|
46
|
|
|
$this->defaultGroup = $defaultGroup; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function loadMetadataForClass(\ReflectionClass $reflClass) |
|
53
|
|
|
{ |
|
54
|
|
|
if (!$reflClass->isInterface() && $reflClass->hasMethod($this->methodName)) { |
|
55
|
|
|
$reflMethod = $reflClass->getMethod($this->methodName); |
|
56
|
|
|
|
|
57
|
|
|
if ($reflMethod->isAbstract()) { |
|
58
|
|
|
throw MappingException::withMessage( |
|
59
|
|
|
'The class %s should not be and abstract class', |
|
60
|
|
|
$reflClass->name, |
|
61
|
|
|
$this->methodName |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (!$reflMethod->isStatic()) { |
|
66
|
|
|
throw MappingException::withMessage( |
|
67
|
|
|
'The method %s::%s should be static', |
|
68
|
|
|
$reflClass->name, |
|
69
|
|
|
$this->methodName |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ($reflMethod->getDeclaringClass()->name != $reflClass->name) { |
|
74
|
|
|
throw MappingException::withMessage( |
|
75
|
|
|
'The method %s should be declared in %s class', |
|
76
|
|
|
$this->methodName, |
|
77
|
|
|
$reflClass->name |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$metadata = new ClassMetadata($reflClass->getName()); |
|
|
|
|
|
|
82
|
|
|
$metadata->defaultGroup = $this->defaultGroup; |
|
83
|
|
|
|
|
84
|
|
|
$reflMethod->invoke(null, $metadata); |
|
85
|
|
|
|
|
86
|
|
|
return $metadata; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|