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
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Validator\Mapping\Driver; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Validator\Assert; |
15
|
|
|
use Cubiche\Core\Validator\Mapping\ClassMetadata; |
16
|
|
|
use Cubiche\Core\Validator\Mapping\Exception\MappingException; |
17
|
|
|
use Metadata\Driver\DriverInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* StaticDriver class. |
21
|
|
|
* |
22
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class StaticDriver implements DriverInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The name of the method to call. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $methodName; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $defaultGroup; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* StaticDriver constructor. |
40
|
|
|
* |
41
|
|
|
* @param string $methodName |
42
|
|
|
* @param string $defaultGroup |
43
|
|
|
*/ |
44
|
|
|
public function __construct($methodName = 'loadValidatorMetadata', $defaultGroup = Assert::DEFAULT_GROUP) |
45
|
|
|
{ |
46
|
|
|
$this->methodName = $methodName; |
47
|
|
|
$this->defaultGroup = $defaultGroup; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function loadMetadataForClass(\ReflectionClass $reflClass) |
54
|
|
|
{ |
55
|
|
|
if (!$reflClass->isInterface() && $reflClass->hasMethod($this->methodName)) { |
56
|
|
|
$reflMethod = $reflClass->getMethod($this->methodName); |
57
|
|
|
|
58
|
|
|
if ($reflMethod->isAbstract()) { |
59
|
|
|
throw MappingException::withMessage( |
60
|
|
|
'The class %s should not be and abstract class', |
61
|
|
|
$reflClass->name, |
62
|
|
|
$this->methodName |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!$reflMethod->isStatic()) { |
67
|
|
|
throw MappingException::withMessage( |
68
|
|
|
'The method %s::%s should be static', |
69
|
|
|
$reflClass->name, |
70
|
|
|
$this->methodName |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$metadata = new ClassMetadata($reflClass->getName()); |
|
|
|
|
75
|
|
|
$metadata->defaultGroup = $this->defaultGroup; |
76
|
|
|
|
77
|
|
|
$reflMethod->invoke(null, $metadata); |
78
|
|
|
|
79
|
|
|
return $metadata; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|