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

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 * Constructor.
37 *
38 * @param string $class The class the getter is defined on
39 * @param string $property The property which the getter returns
40 * @param string|null $method The method that is called to retrieve the value being validated (null for auto-detection)
41 *
42 * @throws ValidatorException
43 */
44 public function __construct($class, $property, $method = null)
45 {
46 if (null === $method) {
47 $getMethod = 'get'.ucfirst($property);
48 $isMethod = 'is'.ucfirst($property);
49 $hasMethod = 'has'.ucfirst($property);
50
51 if (method_exists($class, $getMethod)) {
52 $method = $getMethod;
53 } elseif (method_exists($class, $isMethod)) {
54 $method = $isMethod;
55 } elseif (method_exists($class, $hasMethod)) {
56 $method = $hasMethod;
57 } else {
58 throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
59 }
60 } elseif (!method_exists($class, $method)) {
61 throw new ValidatorException(sprintf('The %s() method does not exist in class %s.', $method, $class));
62 }
63
64 parent::__construct($class, $method, $property);
65 }
66
67 /**
68 * {@inheritdoc}
69 */
70 public function getPropertyValue($object)
71 {
72 return $this->newReflectionMember($object)->invoke($object);
73 }
74
75 /**
76 * {@inheritdoc}
77 */
78 protected function newReflectionMember($objectOrClassName)
79 {
80 return new \ReflectionMethod($objectOrClassName, $this->getName());
81 }
82 }