d51f2c7d050150b7ca016a537786064cbcaac9d7
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 namespace Invoker\ParameterResolver;
4
5 use ReflectionFunctionAbstract;
6
7 /**
8 * Tries to map an associative array (string-indexed) to the parameter names.
9 *
10 * E.g. `->call($callable, ['foo' => 'bar'])` will inject the string `'bar'`
11 * in the parameter named `$foo`.
12 *
13 * Parameters that are not indexed by a string are ignored.
14 *
15 * @author Matthieu Napoli <matthieu@mnapoli.fr>
16 */
17 class AssociativeArrayResolver implements ParameterResolver
18 {
19 public function getParameters(
20 ReflectionFunctionAbstract $reflection,
21 array $providedParameters,
22 array $resolvedParameters
23 ) {
24 $parameters = $reflection->getParameters();
25
26 // Skip parameters already resolved
27 if (! empty($resolvedParameters)) {
28 $parameters = array_diff_key($parameters, $resolvedParameters);
29 }
30
31 foreach ($parameters as $index => $parameter) {
32 if (array_key_exists($parameter->name, $providedParameters)) {
33 $resolvedParameters[$index] = $providedParameters[$parameter->name];
34 }
35 }
36
37 return $resolvedParameters;
38 }
39 }