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 via its getter Chris@0: * method. Chris@0: * Chris@0: * A property getter is any method that is equal to the property's name, Chris@0: * prefixed with either "get" or "is". That method will be used to access the Chris@0: * property's value. Chris@0: * Chris@0: * The getter will be invoked by reflection, so the access of private and Chris@0: * protected getters 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 GetterMetadata extends MemberMetadata Chris@0: { Chris@0: /** Chris@0: * @param string $class The class the getter is defined on Chris@0: * @param string $property The property which the getter returns Chris@0: * @param string|null $method The method that is called to retrieve the value being validated (null for auto-detection) Chris@0: * Chris@0: * @throws ValidatorException Chris@0: */ Chris@0: public function __construct($class, $property, $method = null) Chris@0: { Chris@0: if (null === $method) { Chris@0: $getMethod = 'get'.ucfirst($property); Chris@0: $isMethod = 'is'.ucfirst($property); Chris@0: $hasMethod = 'has'.ucfirst($property); Chris@0: Chris@0: if (method_exists($class, $getMethod)) { Chris@0: $method = $getMethod; Chris@0: } elseif (method_exists($class, $isMethod)) { Chris@0: $method = $isMethod; Chris@0: } elseif (method_exists($class, $hasMethod)) { Chris@0: $method = $hasMethod; Chris@0: } else { Chris@0: throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod)); Chris@0: } Chris@0: } elseif (!method_exists($class, $method)) { Chris@0: throw new ValidatorException(sprintf('The %s() method does not exist in class %s.', $method, $class)); Chris@0: } Chris@0: Chris@0: parent::__construct($class, $method, $property); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getPropertyValue($object) Chris@0: { Chris@0: return $this->newReflectionMember($object)->invoke($object); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function newReflectionMember($objectOrClassName) Chris@0: { Chris@0: return new \ReflectionMethod($objectOrClassName, $this->getName()); Chris@0: } Chris@0: }