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\Constraint;
|
Chris@0
|
15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Stores all metadata needed for validating a class property.
|
Chris@0
|
19 *
|
Chris@0
|
20 * The method of accessing the property's value must be specified by subclasses
|
Chris@0
|
21 * by implementing the {@link newReflectionMember()} method.
|
Chris@0
|
22 *
|
Chris@0
|
23 * This class supports serialization and cloning.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
26 *
|
Chris@0
|
27 * @see PropertyMetadataInterface
|
Chris@0
|
28 */
|
Chris@0
|
29 abstract class MemberMetadata extends GenericMetadata implements PropertyMetadataInterface
|
Chris@0
|
30 {
|
Chris@0
|
31 /**
|
Chris@0
|
32 * @internal This property is public in order to reduce the size of the
|
Chris@0
|
33 * class' serialized representation. Do not access it. Use
|
Chris@0
|
34 * {@link getClassName()} instead.
|
Chris@0
|
35 */
|
Chris@0
|
36 public $class;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * @internal This property is public in order to reduce the size of the
|
Chris@0
|
40 * class' serialized representation. Do not access it. Use
|
Chris@0
|
41 * {@link getName()} instead.
|
Chris@0
|
42 */
|
Chris@0
|
43 public $name;
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * @internal This property is public in order to reduce the size of the
|
Chris@0
|
47 * class' serialized representation. Do not access it. Use
|
Chris@0
|
48 * {@link getPropertyName()} instead.
|
Chris@0
|
49 */
|
Chris@0
|
50 public $property;
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * @var \ReflectionMethod[]|\ReflectionProperty[]
|
Chris@0
|
54 */
|
Chris@17
|
55 private $reflMember = [];
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * @param string $class The name of the class this member is defined on
|
Chris@0
|
59 * @param string $name The name of the member
|
Chris@0
|
60 * @param string $property The property the member belongs to
|
Chris@0
|
61 */
|
Chris@0
|
62 public function __construct($class, $name, $property)
|
Chris@0
|
63 {
|
Chris@0
|
64 $this->class = $class;
|
Chris@0
|
65 $this->name = $name;
|
Chris@0
|
66 $this->property = $property;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * {@inheritdoc}
|
Chris@0
|
71 */
|
Chris@0
|
72 public function addConstraint(Constraint $constraint)
|
Chris@0
|
73 {
|
Chris@17
|
74 if (!\in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets())) {
|
Chris@17
|
75 throw new ConstraintDefinitionException(sprintf('The constraint %s cannot be put on properties or getters', \get_class($constraint)));
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 parent::addConstraint($constraint);
|
Chris@0
|
79
|
Chris@0
|
80 return $this;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * {@inheritdoc}
|
Chris@0
|
85 */
|
Chris@0
|
86 public function __sleep()
|
Chris@0
|
87 {
|
Chris@17
|
88 return array_merge(parent::__sleep(), [
|
Chris@0
|
89 'class',
|
Chris@0
|
90 'name',
|
Chris@0
|
91 'property',
|
Chris@17
|
92 ]);
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 /**
|
Chris@0
|
96 * Returns the name of the member.
|
Chris@0
|
97 *
|
Chris@0
|
98 * @return string
|
Chris@0
|
99 */
|
Chris@0
|
100 public function getName()
|
Chris@0
|
101 {
|
Chris@0
|
102 return $this->name;
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * {@inheritdoc}
|
Chris@0
|
107 */
|
Chris@0
|
108 public function getClassName()
|
Chris@0
|
109 {
|
Chris@0
|
110 return $this->class;
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 /**
|
Chris@0
|
114 * {@inheritdoc}
|
Chris@0
|
115 */
|
Chris@0
|
116 public function getPropertyName()
|
Chris@0
|
117 {
|
Chris@0
|
118 return $this->property;
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 /**
|
Chris@0
|
122 * Returns whether this member is public.
|
Chris@0
|
123 *
|
Chris@0
|
124 * @param object|string $objectOrClassName The object or the class name
|
Chris@0
|
125 *
|
Chris@0
|
126 * @return bool
|
Chris@0
|
127 */
|
Chris@0
|
128 public function isPublic($objectOrClassName)
|
Chris@0
|
129 {
|
Chris@0
|
130 return $this->getReflectionMember($objectOrClassName)->isPublic();
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * Returns whether this member is protected.
|
Chris@0
|
135 *
|
Chris@0
|
136 * @param object|string $objectOrClassName The object or the class name
|
Chris@0
|
137 *
|
Chris@0
|
138 * @return bool
|
Chris@0
|
139 */
|
Chris@0
|
140 public function isProtected($objectOrClassName)
|
Chris@0
|
141 {
|
Chris@0
|
142 return $this->getReflectionMember($objectOrClassName)->isProtected();
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 /**
|
Chris@0
|
146 * Returns whether this member is private.
|
Chris@0
|
147 *
|
Chris@0
|
148 * @param object|string $objectOrClassName The object or the class name
|
Chris@0
|
149 *
|
Chris@0
|
150 * @return bool
|
Chris@0
|
151 */
|
Chris@0
|
152 public function isPrivate($objectOrClassName)
|
Chris@0
|
153 {
|
Chris@0
|
154 return $this->getReflectionMember($objectOrClassName)->isPrivate();
|
Chris@0
|
155 }
|
Chris@0
|
156
|
Chris@0
|
157 /**
|
Chris@0
|
158 * Returns the reflection instance for accessing the member's value.
|
Chris@0
|
159 *
|
Chris@0
|
160 * @param object|string $objectOrClassName The object or the class name
|
Chris@0
|
161 *
|
Chris@0
|
162 * @return \ReflectionMethod|\ReflectionProperty The reflection instance
|
Chris@0
|
163 */
|
Chris@0
|
164 public function getReflectionMember($objectOrClassName)
|
Chris@0
|
165 {
|
Chris@17
|
166 $className = \is_string($objectOrClassName) ? $objectOrClassName : \get_class($objectOrClassName);
|
Chris@0
|
167 if (!isset($this->reflMember[$className])) {
|
Chris@0
|
168 $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 return $this->reflMember[$className];
|
Chris@0
|
172 }
|
Chris@0
|
173
|
Chris@0
|
174 /**
|
Chris@0
|
175 * Creates a new reflection instance for accessing the member's value.
|
Chris@0
|
176 *
|
Chris@0
|
177 * Must be implemented by subclasses.
|
Chris@0
|
178 *
|
Chris@0
|
179 * @param object|string $objectOrClassName The object or the class name
|
Chris@0
|
180 *
|
Chris@0
|
181 * @return \ReflectionMethod|\ReflectionProperty The reflection instance
|
Chris@0
|
182 */
|
Chris@0
|
183 abstract protected function newReflectionMember($objectOrClassName);
|
Chris@0
|
184 }
|