Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Validator\Mapping; Chris@0: Chris@0: use Symfony\Component\Validator\Constraint; Chris@0: use Symfony\Component\Validator\Exception\ConstraintDefinitionException; Chris@0: Chris@0: /** Chris@0: * Stores all metadata needed for validating a class property. Chris@0: * Chris@0: * The method of accessing the property's value must be specified by subclasses Chris@0: * by implementing the {@link newReflectionMember()} method. Chris@0: * Chris@0: * This class supports serialization and cloning. Chris@0: * Chris@0: * @author Bernhard Schussek Chris@0: * Chris@0: * @see PropertyMetadataInterface Chris@0: */ Chris@0: abstract class MemberMetadata extends GenericMetadata implements PropertyMetadataInterface Chris@0: { Chris@0: /** Chris@0: * @internal This property is public in order to reduce the size of the Chris@0: * class' serialized representation. Do not access it. Use Chris@0: * {@link getClassName()} instead. Chris@0: */ Chris@0: public $class; Chris@0: Chris@0: /** Chris@0: * @internal This property is public in order to reduce the size of the Chris@0: * class' serialized representation. Do not access it. Use Chris@0: * {@link getName()} instead. Chris@0: */ Chris@0: public $name; Chris@0: Chris@0: /** Chris@0: * @internal This property is public in order to reduce the size of the Chris@0: * class' serialized representation. Do not access it. Use Chris@0: * {@link getPropertyName()} instead. Chris@0: */ Chris@0: public $property; Chris@0: Chris@0: /** Chris@0: * @var \ReflectionMethod[]|\ReflectionProperty[] Chris@0: */ Chris@17: private $reflMember = []; Chris@0: Chris@0: /** Chris@0: * @param string $class The name of the class this member is defined on Chris@0: * @param string $name The name of the member Chris@0: * @param string $property The property the member belongs to Chris@0: */ Chris@0: public function __construct($class, $name, $property) Chris@0: { Chris@0: $this->class = $class; Chris@0: $this->name = $name; Chris@0: $this->property = $property; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function addConstraint(Constraint $constraint) Chris@0: { Chris@17: if (!\in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets())) { Chris@17: throw new ConstraintDefinitionException(sprintf('The constraint %s cannot be put on properties or getters', \get_class($constraint))); Chris@0: } Chris@0: Chris@0: parent::addConstraint($constraint); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function __sleep() Chris@0: { Chris@17: return array_merge(parent::__sleep(), [ Chris@0: 'class', Chris@0: 'name', Chris@0: 'property', Chris@17: ]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the name of the member. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getName() Chris@0: { Chris@0: return $this->name; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getClassName() Chris@0: { Chris@0: return $this->class; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getPropertyName() Chris@0: { Chris@0: return $this->property; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns whether this member is public. Chris@0: * Chris@0: * @param object|string $objectOrClassName The object or the class name Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isPublic($objectOrClassName) Chris@0: { Chris@0: return $this->getReflectionMember($objectOrClassName)->isPublic(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns whether this member is protected. Chris@0: * Chris@0: * @param object|string $objectOrClassName The object or the class name Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isProtected($objectOrClassName) Chris@0: { Chris@0: return $this->getReflectionMember($objectOrClassName)->isProtected(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns whether this member is private. Chris@0: * Chris@0: * @param object|string $objectOrClassName The object or the class name Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isPrivate($objectOrClassName) Chris@0: { Chris@0: return $this->getReflectionMember($objectOrClassName)->isPrivate(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the reflection instance for accessing the member's value. Chris@0: * Chris@0: * @param object|string $objectOrClassName The object or the class name Chris@0: * Chris@0: * @return \ReflectionMethod|\ReflectionProperty The reflection instance Chris@0: */ Chris@0: public function getReflectionMember($objectOrClassName) Chris@0: { Chris@17: $className = \is_string($objectOrClassName) ? $objectOrClassName : \get_class($objectOrClassName); Chris@0: if (!isset($this->reflMember[$className])) { Chris@0: $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName); Chris@0: } Chris@0: Chris@0: return $this->reflMember[$className]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a new reflection instance for accessing the member's value. Chris@0: * Chris@0: * Must be implemented by subclasses. Chris@0: * Chris@0: * @param object|string $objectOrClassName The object or the class name Chris@0: * Chris@0: * @return \ReflectionMethod|\ReflectionProperty The reflection instance Chris@0: */ Chris@0: abstract protected function newReflectionMember($objectOrClassName); Chris@0: }