|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
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 Symfony\Component\Validator\Mapping; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Validator\Exception\ValidatorException; |
|
|
15 |
|
|
|
16 |
class GetterMetadata extends MemberMetadata |
|
|
17 |
{ |
|
|
18 |
/** |
|
|
19 |
* Constructor. |
|
|
20 |
* |
|
|
21 |
* @param string $class The class the getter is defined on |
|
|
22 |
* @param string $property The property which the getter returns |
|
|
23 |
*/ |
|
|
24 |
public function __construct($class, $property) |
|
|
25 |
{ |
|
|
26 |
$getMethod = 'get'.ucfirst($property); |
|
|
27 |
$isMethod = 'is'.ucfirst($property); |
|
|
28 |
|
|
|
29 |
if (method_exists($class, $getMethod)) { |
|
|
30 |
$method = $getMethod; |
|
|
31 |
} else if (method_exists($class, $isMethod)) { |
|
|
32 |
$method = $isMethod; |
|
|
33 |
} else { |
|
|
34 |
throw new ValidatorException(sprintf('Neither method %s nor %s exists in class %s', $getMethod, $isMethod, $class)); |
|
|
35 |
} |
|
|
36 |
|
|
|
37 |
parent::__construct($class, $method, $property); |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
/** |
|
|
41 |
* {@inheritDoc} |
|
|
42 |
*/ |
|
|
43 |
public function getValue($object) |
|
|
44 |
{ |
|
|
45 |
return $this->getReflectionMember()->invoke($object); |
|
|
46 |
} |
|
|
47 |
|
|
|
48 |
/** |
|
|
49 |
* {@inheritDoc} |
|
|
50 |
*/ |
|
|
51 |
protected function newReflectionMember() |
|
|
52 |
{ |
|
|
53 |
return new \ReflectionMethod($this->getClassName(), $this->getName()); |
|
|
54 |
} |
|
|
55 |
} |