4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Mapping;
14 use Symfony\Component\Validator\Exception\ValidatorException;
17 * Stores all metadata needed for validating a class property via its getter
20 * A property getter is any method that is equal to the property's name,
21 * prefixed with either "get" or "is". That method will be used to access the
24 * The getter will be invoked by reflection, so the access of private and
25 * protected getters is supported.
27 * This class supports serialization and cloning.
29 * @author Bernhard Schussek <bschussek@gmail.com>
31 * @see PropertyMetadataInterface
33 class GetterMetadata extends MemberMetadata
36 * @param string $class The class the getter is defined on
37 * @param string $property The property which the getter returns
38 * @param string|null $method The method that is called to retrieve the value being validated (null for auto-detection)
40 * @throws ValidatorException
42 public function __construct($class, $property, $method = null)
44 if (null === $method) {
45 $getMethod = 'get'.ucfirst($property);
46 $isMethod = 'is'.ucfirst($property);
47 $hasMethod = 'has'.ucfirst($property);
49 if (method_exists($class, $getMethod)) {
51 } elseif (method_exists($class, $isMethod)) {
53 } elseif (method_exists($class, $hasMethod)) {
56 throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
58 } elseif (!method_exists($class, $method)) {
59 throw new ValidatorException(sprintf('The %s() method does not exist in class %s.', $method, $class));
62 parent::__construct($class, $method, $property);
68 public function getPropertyValue($object)
70 return $this->newReflectionMember($object)->invoke($object);
76 protected function newReflectionMember($objectOrClassName)
78 return new \ReflectionMethod($objectOrClassName, $this->getName());