2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\Validator\Mapping;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Exception\ValidatorException;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class GetterMetadata extends MemberMetadata
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param string $class The class the getter is defined on
|
|
|
|
* @param string $property The property which the getter returns
|
|
|
|
*/
|
|
|
|
public function __construct($class, $property)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$getMethod = 'get'.ucfirst($property);
|
|
|
|
$isMethod = 'is'.ucfirst($property);
|
|
|
|
|
|
|
|
if (method_exists($class, $getMethod)) {
|
|
|
|
$method = $getMethod;
|
|
|
|
} else if (method_exists($class, $isMethod)) {
|
|
|
|
$method = $isMethod;
|
|
|
|
} else {
|
|
|
|
throw new ValidatorException(sprintf('Neither method %s nor %s exists in class %s', $getMethod, $isMethod, $class));
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($class, $method, $property);
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getValue($object)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
return $this->getReflectionMember()->invoke($object);
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
protected function newReflectionMember()
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
return new \ReflectionMethod($this->getClassName(), $this->getName());
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
}
|