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\Loader; Chris@0: Chris@0: use Symfony\Component\Validator\Exception\MappingException; Chris@0: use Symfony\Component\Validator\Mapping\ClassMetadata; Chris@0: Chris@0: /** Chris@0: * Loads validation metadata by calling a static method on the loaded class. Chris@0: * Chris@0: * @author Bernhard Schussek Chris@0: */ Chris@0: class StaticMethodLoader implements LoaderInterface Chris@0: { Chris@0: protected $methodName; Chris@0: Chris@0: /** Chris@0: * Creates a new loader. Chris@0: * Chris@0: * @param string $methodName The name of the static method to call Chris@0: */ Chris@0: public function __construct($methodName = 'loadValidatorMetadata') Chris@0: { Chris@0: $this->methodName = $methodName; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function loadClassMetadata(ClassMetadata $metadata) Chris@0: { Chris@0: /** @var \ReflectionClass $reflClass */ Chris@0: $reflClass = $metadata->getReflectionClass(); Chris@0: Chris@0: if (!$reflClass->isInterface() && $reflClass->hasMethod($this->methodName)) { Chris@0: $reflMethod = $reflClass->getMethod($this->methodName); Chris@0: Chris@0: if ($reflMethod->isAbstract()) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: if (!$reflMethod->isStatic()) { Chris@0: throw new MappingException(sprintf('The method %s::%s should be static', $reflClass->name, $this->methodName)); Chris@0: } Chris@0: Chris@0: if ($reflMethod->getDeclaringClass()->name != $reflClass->name) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: $reflMethod->invoke(null, $metadata); Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: }