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\Exception\ValidatorException; Chris@0: Chris@0: /** Chris@0: * Stores all metadata needed for validating a class property. Chris@0: * Chris@0: * The value of the property is obtained by directly accessing the property. Chris@0: * The property will be accessed by reflection, so the access of private and Chris@0: * protected properties is supported. 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: class PropertyMetadata extends MemberMetadata Chris@0: { Chris@0: /** Chris@0: * @param string $class The class this property is defined on Chris@0: * @param string $name The name of this property Chris@0: * Chris@0: * @throws ValidatorException Chris@0: */ Chris@0: public function __construct($class, $name) Chris@0: { Chris@0: if (!property_exists($class, $name)) { Chris@0: throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s"', $name, $class)); Chris@0: } Chris@0: Chris@0: parent::__construct($class, $name, $name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getPropertyValue($object) Chris@0: { Chris@0: return $this->getReflectionMember($object)->getValue($object); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function newReflectionMember($objectOrClassName) Chris@0: { Chris@17: $originalClass = \is_string($objectOrClassName) ? $objectOrClassName : \get_class($objectOrClassName); Chris@0: Chris@0: while (!property_exists($objectOrClassName, $this->getName())) { Chris@0: $objectOrClassName = get_parent_class($objectOrClassName); Chris@0: Chris@0: if (false === $objectOrClassName) { Chris@0: throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s".', $this->getName(), $originalClass)); Chris@0: } Chris@0: } Chris@0: Chris@0: $member = new \ReflectionProperty($objectOrClassName, $this->getName()); Chris@0: $member->setAccessible(true); Chris@0: Chris@0: return $member; Chris@0: } Chris@0: }