1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of PHP Invoker. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.1 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace DivineNii\Invoker\ArgumentResolver; |
19
|
|
|
|
20
|
|
|
use DivineNii\Invoker\Interfaces\ArgumentValueResolverInterface; |
21
|
|
|
use Psr\Container\ContainerInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Tries to map an associative array (string-indexed) to the parameter names. |
25
|
|
|
* E.g. `->call($callable, ['foo' => 'bar'])` will inject the string `'bar'` |
26
|
|
|
* in the parameter named `$foo`. |
27
|
|
|
* Parameters that are not indexed by a string are ignored. |
28
|
|
|
* |
29
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
final class NamedValueResolver implements ArgumentValueResolverInterface |
32
|
|
|
{ |
33
|
|
|
/** @var ContainerInterface|null */ |
34
|
|
|
private $container; |
35
|
|
|
|
36
|
56 |
|
public function __construct(?ContainerInterface $container = null) |
37
|
|
|
{ |
38
|
56 |
|
$this->container = $container; |
39
|
56 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
32 |
|
public function resolve(\ReflectionParameter $parameter, array $providedParameters) |
45
|
|
|
{ |
46
|
32 |
|
$paramName = $parameter->getName(); |
47
|
32 |
|
$position = $parameter->getPosition(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Simply returns all the values of the $providedParameters array that are |
51
|
|
|
* indexed by the parameter position (i.e. a number). |
52
|
|
|
* E.g. `->call($callable, ['foo', 'bar'])` will simply resolve the parameters |
53
|
|
|
* to `['foo', 'bar']`. |
54
|
|
|
* Parameters that are not indexed by a number (i.e. parameter position) |
55
|
|
|
* will be ignored. |
56
|
|
|
*/ |
57
|
32 |
|
if (isset($providedParameters[$position])) { |
58
|
9 |
|
$providedParameters[$paramName] = $providedParameters[$position]; |
59
|
9 |
|
unset($providedParameters[$position]); |
60
|
|
|
} |
61
|
|
|
|
62
|
32 |
|
if (\array_key_exists($paramName, $providedParameters)) { |
63
|
17 |
|
$value = $providedParameters[$paramName]; |
64
|
17 |
|
unset($providedParameters[$paramName]); |
65
|
|
|
|
66
|
17 |
|
return $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Inject entries from a DI container using the parameter names. |
70
|
|
|
if ( |
71
|
23 |
|
null === $parameter->getType() && |
72
|
23 |
|
(null !== $this->container && $this->container->has($paramName)) |
73
|
|
|
) { |
74
|
2 |
|
return $this->container->get($paramName); |
75
|
|
|
} |
76
|
21 |
|
} |
77
|
|
|
} |
78
|
|
|
|