annotate vendor/symfony/validator/Mapping/MemberMetadata.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
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@0 55 private $reflMember = array();
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@0 74 if (!in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets())) {
Chris@0 75 throw new ConstraintDefinitionException(sprintf(
Chris@0 76 'The constraint %s cannot be put on properties or getters',
Chris@0 77 get_class($constraint)
Chris@0 78 ));
Chris@0 79 }
Chris@0 80
Chris@0 81 parent::addConstraint($constraint);
Chris@0 82
Chris@0 83 return $this;
Chris@0 84 }
Chris@0 85
Chris@0 86 /**
Chris@0 87 * {@inheritdoc}
Chris@0 88 */
Chris@0 89 public function __sleep()
Chris@0 90 {
Chris@0 91 return array_merge(parent::__sleep(), array(
Chris@0 92 'class',
Chris@0 93 'name',
Chris@0 94 'property',
Chris@0 95 ));
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Returns the name of the member.
Chris@0 100 *
Chris@0 101 * @return string
Chris@0 102 */
Chris@0 103 public function getName()
Chris@0 104 {
Chris@0 105 return $this->name;
Chris@0 106 }
Chris@0 107
Chris@0 108 /**
Chris@0 109 * {@inheritdoc}
Chris@0 110 */
Chris@0 111 public function getClassName()
Chris@0 112 {
Chris@0 113 return $this->class;
Chris@0 114 }
Chris@0 115
Chris@0 116 /**
Chris@0 117 * {@inheritdoc}
Chris@0 118 */
Chris@0 119 public function getPropertyName()
Chris@0 120 {
Chris@0 121 return $this->property;
Chris@0 122 }
Chris@0 123
Chris@0 124 /**
Chris@0 125 * Returns whether this member is public.
Chris@0 126 *
Chris@0 127 * @param object|string $objectOrClassName The object or the class name
Chris@0 128 *
Chris@0 129 * @return bool
Chris@0 130 */
Chris@0 131 public function isPublic($objectOrClassName)
Chris@0 132 {
Chris@0 133 return $this->getReflectionMember($objectOrClassName)->isPublic();
Chris@0 134 }
Chris@0 135
Chris@0 136 /**
Chris@0 137 * Returns whether this member is protected.
Chris@0 138 *
Chris@0 139 * @param object|string $objectOrClassName The object or the class name
Chris@0 140 *
Chris@0 141 * @return bool
Chris@0 142 */
Chris@0 143 public function isProtected($objectOrClassName)
Chris@0 144 {
Chris@0 145 return $this->getReflectionMember($objectOrClassName)->isProtected();
Chris@0 146 }
Chris@0 147
Chris@0 148 /**
Chris@0 149 * Returns whether this member is private.
Chris@0 150 *
Chris@0 151 * @param object|string $objectOrClassName The object or the class name
Chris@0 152 *
Chris@0 153 * @return bool
Chris@0 154 */
Chris@0 155 public function isPrivate($objectOrClassName)
Chris@0 156 {
Chris@0 157 return $this->getReflectionMember($objectOrClassName)->isPrivate();
Chris@0 158 }
Chris@0 159
Chris@0 160 /**
Chris@0 161 * Returns the reflection instance for accessing the member's value.
Chris@0 162 *
Chris@0 163 * @param object|string $objectOrClassName The object or the class name
Chris@0 164 *
Chris@0 165 * @return \ReflectionMethod|\ReflectionProperty The reflection instance
Chris@0 166 */
Chris@0 167 public function getReflectionMember($objectOrClassName)
Chris@0 168 {
Chris@0 169 $className = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);
Chris@0 170 if (!isset($this->reflMember[$className])) {
Chris@0 171 $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
Chris@0 172 }
Chris@0 173
Chris@0 174 return $this->reflMember[$className];
Chris@0 175 }
Chris@0 176
Chris@0 177 /**
Chris@0 178 * Creates a new reflection instance for accessing the member's value.
Chris@0 179 *
Chris@0 180 * Must be implemented by subclasses.
Chris@0 181 *
Chris@0 182 * @param object|string $objectOrClassName The object or the class name
Chris@0 183 *
Chris@0 184 * @return \ReflectionMethod|\ReflectionProperty The reflection instance
Chris@0 185 */
Chris@0 186 abstract protected function newReflectionMember($objectOrClassName);
Chris@0 187 }