Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Validator\Mapping;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Validator\Exception\ValidatorException;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Stores all metadata needed for validating a class property via its getter
|
Chris@0
|
18 * method.
|
Chris@0
|
19 *
|
Chris@0
|
20 * A property getter is any method that is equal to the property's name,
|
Chris@0
|
21 * prefixed with either "get" or "is". That method will be used to access the
|
Chris@0
|
22 * property's value.
|
Chris@0
|
23 *
|
Chris@0
|
24 * The getter will be invoked by reflection, so the access of private and
|
Chris@0
|
25 * protected getters is supported.
|
Chris@0
|
26 *
|
Chris@0
|
27 * This class supports serialization and cloning.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
30 *
|
Chris@0
|
31 * @see PropertyMetadataInterface
|
Chris@0
|
32 */
|
Chris@0
|
33 class GetterMetadata extends MemberMetadata
|
Chris@0
|
34 {
|
Chris@0
|
35 /**
|
Chris@0
|
36 * @param string $class The class the getter is defined on
|
Chris@0
|
37 * @param string $property The property which the getter returns
|
Chris@0
|
38 * @param string|null $method The method that is called to retrieve the value being validated (null for auto-detection)
|
Chris@0
|
39 *
|
Chris@0
|
40 * @throws ValidatorException
|
Chris@0
|
41 */
|
Chris@0
|
42 public function __construct($class, $property, $method = null)
|
Chris@0
|
43 {
|
Chris@0
|
44 if (null === $method) {
|
Chris@0
|
45 $getMethod = 'get'.ucfirst($property);
|
Chris@0
|
46 $isMethod = 'is'.ucfirst($property);
|
Chris@0
|
47 $hasMethod = 'has'.ucfirst($property);
|
Chris@0
|
48
|
Chris@0
|
49 if (method_exists($class, $getMethod)) {
|
Chris@0
|
50 $method = $getMethod;
|
Chris@0
|
51 } elseif (method_exists($class, $isMethod)) {
|
Chris@0
|
52 $method = $isMethod;
|
Chris@0
|
53 } elseif (method_exists($class, $hasMethod)) {
|
Chris@0
|
54 $method = $hasMethod;
|
Chris@0
|
55 } else {
|
Chris@0
|
56 throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
|
Chris@0
|
57 }
|
Chris@0
|
58 } elseif (!method_exists($class, $method)) {
|
Chris@0
|
59 throw new ValidatorException(sprintf('The %s() method does not exist in class %s.', $method, $class));
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 parent::__construct($class, $method, $property);
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * {@inheritdoc}
|
Chris@0
|
67 */
|
Chris@0
|
68 public function getPropertyValue($object)
|
Chris@0
|
69 {
|
Chris@0
|
70 return $this->newReflectionMember($object)->invoke($object);
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * {@inheritdoc}
|
Chris@0
|
75 */
|
Chris@0
|
76 protected function newReflectionMember($objectOrClassName)
|
Chris@0
|
77 {
|
Chris@0
|
78 return new \ReflectionMethod($objectOrClassName, $this->getName());
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|