comparison vendor/symfony/validator/Mapping/GetterMetadata.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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 /**
17 * Stores all metadata needed for validating a class property via its getter
18 * method.
19 *
20 * A property getter is any method that is equal to the property's name,
21 * prefixed with either "get" or "is". That method will be used to access the
22 * property's value.
23 *
24 * The getter will be invoked by reflection, so the access of private and
25 * protected getters is supported.
26 *
27 * This class supports serialization and cloning.
28 *
29 * @author Bernhard Schussek <bschussek@gmail.com>
30 *
31 * @see PropertyMetadataInterface
32 */
33 class GetterMetadata extends MemberMetadata
34 {
35 /**
36 * @param string $class The class the getter is defined on
37 * @param string $property The property which the getter returns
38 * @param string|null $method The method that is called to retrieve the value being validated (null for auto-detection)
39 *
40 * @throws ValidatorException
41 */
42 public function __construct($class, $property, $method = null)
43 {
44 if (null === $method) {
45 $getMethod = 'get'.ucfirst($property);
46 $isMethod = 'is'.ucfirst($property);
47 $hasMethod = 'has'.ucfirst($property);
48
49 if (method_exists($class, $getMethod)) {
50 $method = $getMethod;
51 } elseif (method_exists($class, $isMethod)) {
52 $method = $isMethod;
53 } elseif (method_exists($class, $hasMethod)) {
54 $method = $hasMethod;
55 } else {
56 throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
57 }
58 } elseif (!method_exists($class, $method)) {
59 throw new ValidatorException(sprintf('The %s() method does not exist in class %s.', $method, $class));
60 }
61
62 parent::__construct($class, $method, $property);
63 }
64
65 /**
66 * {@inheritdoc}
67 */
68 public function getPropertyValue($object)
69 {
70 return $this->newReflectionMember($object)->invoke($object);
71 }
72
73 /**
74 * {@inheritdoc}
75 */
76 protected function newReflectionMember($objectOrClassName)
77 {
78 return new \ReflectionMethod($objectOrClassName, $this->getName());
79 }
80 }