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