This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Validator/Mapping/GetterMetadata.php

78 lines
2.1 KiB
PHP
Raw Normal View History

<?php
2010-10-02 11:42:31 +01:00
/*
* This file is part of the Symfony package.
2010-10-02 11:42:31 +01:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-10-02 11:42:31 +01:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2010-10-02 11:42:31 +01:00
*/
namespace Symfony\Component\Validator\Mapping;
use Symfony\Component\Validator\Exception\ValidatorException;
/**
* Stores all metadata needed for validating a class property via its getter
* method.
*
* A property getter is any method that is equal to the property's name,
* prefixed with either "get" or "is". That method will be used to access the
* property's value.
*
* The getter will be invoked by reflection, so the access of private and
* protected getters is supported.
*
* This class supports serialization and cloning.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @see PropertyMetadataInterface
*/
class GetterMetadata extends MemberMetadata
{
/**
* Constructor.
*
2010-10-15 22:49:09 +01:00
* @param string $class The class the getter is defined on
* @param string $property The property which the getter returns
*
* @throws ValidatorException
*/
public function __construct($class, $property)
{
$getMethod = 'get'.ucfirst($property);
$isMethod = 'is'.ucfirst($property);
$hasMethod = 'has'.ucfirst($property);
if (method_exists($class, $getMethod)) {
$method = $getMethod;
2011-12-18 13:42:59 +00:00
} elseif (method_exists($class, $isMethod)) {
$method = $isMethod;
} elseif (method_exists($class, $hasMethod)) {
$method = $hasMethod;
} else {
throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
}
parent::__construct($class, $method, $property);
}
/**
* {@inheritdoc}
*/
public function getPropertyValue($object)
{
return $this->newReflectionMember($object)->invoke($object);
}
/**
* {@inheritdoc}
*/
protected function newReflectionMember($objectOrClassName)
{
return new \ReflectionMethod($objectOrClassName, $this->getName());
}
2011-06-08 11:16:48 +01:00
}