Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / league / container / src / Argument / ArgumentResolverTrait.php
1 <?php
2
3 namespace League\Container\Argument;
4
5 use League\Container\Exception\NotFoundException;
6 use League\Container\ReflectionContainer;
7 use ReflectionFunctionAbstract;
8 use ReflectionParameter;
9
10 trait ArgumentResolverTrait
11 {
12     /**
13      * {@inheritdoc}
14      */
15     public function resolveArguments(array $arguments)
16     {
17         foreach ($arguments as &$arg) {
18             if ($arg instanceof RawArgumentInterface) {
19                 $arg = $arg->getValue();
20                 continue;
21             }
22
23             if (! is_string($arg)) {
24                  continue;
25             }
26
27             $container = $this->getContainer();
28
29             if (is_null($container) && $this instanceof ReflectionContainer) {
30                 $container = $this;
31             }
32
33             if (! is_null($container) && $container->has($arg)) {
34                 $arg = $container->get($arg);
35
36                 if ($arg instanceof RawArgumentInterface) {
37                     $arg = $arg->getValue();
38                 }
39
40                 continue;
41             }
42         }
43
44         return $arguments;
45     }
46
47     /**
48      * {@inheritdoc}
49      */
50     public function reflectArguments(ReflectionFunctionAbstract $method, array $args = [])
51     {
52         $arguments = array_map(function (ReflectionParameter $param) use ($method, $args) {
53             $name  = $param->getName();
54             $class = $param->getClass();
55
56             if (array_key_exists($name, $args)) {
57                 return $args[$name];
58             }
59
60             if (! is_null($class)) {
61                 return $class->getName();
62             }
63
64             if ($param->isDefaultValueAvailable()) {
65                 return $param->getDefaultValue();
66             }
67
68             throw new NotFoundException(sprintf(
69                 'Unable to resolve a value for parameter (%s) in the function/method (%s)',
70                 $name,
71                 $method->getName()
72             ));
73         }, $method->getParameters());
74
75         return $this->resolveArguments($arguments);
76     }
77
78     /**
79      * @return \League\Container\ContainerInterface
80      */
81     abstract public function getContainer();
82 }