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