Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / validator / Mapping / Loader / StaticMethodLoader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Mapping\Loader;
13
14 use Symfony\Component\Validator\Exception\MappingException;
15 use Symfony\Component\Validator\Mapping\ClassMetadata;
16
17 /**
18  * Loads validation metadata by calling a static method on the loaded class.
19  *
20  * @author Bernhard Schussek <bschussek@gmail.com>
21  */
22 class StaticMethodLoader implements LoaderInterface
23 {
24     protected $methodName;
25
26     /**
27      * Creates a new loader.
28      *
29      * @param string $methodName The name of the static method to call
30      */
31     public function __construct($methodName = 'loadValidatorMetadata')
32     {
33         $this->methodName = $methodName;
34     }
35
36     /**
37      * {@inheritdoc}
38      */
39     public function loadClassMetadata(ClassMetadata $metadata)
40     {
41         /** @var \ReflectionClass $reflClass */
42         $reflClass = $metadata->getReflectionClass();
43
44         if (!$reflClass->isInterface() && $reflClass->hasMethod($this->methodName)) {
45             $reflMethod = $reflClass->getMethod($this->methodName);
46
47             if ($reflMethod->isAbstract()) {
48                 return false;
49             }
50
51             if (!$reflMethod->isStatic()) {
52                 throw new MappingException(sprintf('The method %s::%s should be static', $reflClass->name, $this->methodName));
53             }
54
55             if ($reflMethod->getDeclaringClass()->name != $reflClass->name) {
56                 return false;
57             }
58
59             $reflMethod->invoke(null, $metadata);
60
61             return true;
62         }
63
64         return false;
65     }
66 }