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\Loader;
14 use Symfony\Component\Validator\Exception\MappingException;
15 use Symfony\Component\Validator\Mapping\ClassMetadata;
18 * Loads validation metadata by calling a static method on the loaded class.
20 * @author Bernhard Schussek <bschussek@gmail.com>
22 class StaticMethodLoader implements LoaderInterface
25 * The name of the method to call.
29 protected $methodName;
32 * Creates a new loader.
34 * @param string $methodName The name of the static method to call
36 public function __construct($methodName = 'loadValidatorMetadata')
38 $this->methodName = $methodName;
44 public function loadClassMetadata(ClassMetadata $metadata)
46 /** @var \ReflectionClass $reflClass */
47 $reflClass = $metadata->getReflectionClass();
49 if (!$reflClass->isInterface() && $reflClass->hasMethod($this->methodName)) {
50 $reflMethod = $reflClass->getMethod($this->methodName);
52 if ($reflMethod->isAbstract()) {
56 if (!$reflMethod->isStatic()) {
57 throw new MappingException(sprintf('The method %s::%s should be static', $reflClass->name, $this->methodName));
60 if ($reflMethod->getDeclaringClass()->name != $reflClass->name) {
64 $reflMethod->invoke(null, $metadata);