Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php declare(strict_types=1);
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
class AssociativeArrayResolver implements ParameterResolver
16
{
17
    public function getParameters(
18
        ReflectionFunctionAbstract $reflection,
19
        array $providedParameters,
20
        array $resolvedParameters
21
    ): array {
22
        $parameters = $reflection->getParameters();
23
 
24
        // Skip parameters already resolved
25
        if (! empty($resolvedParameters)) {
26
            $parameters = array_diff_key($parameters, $resolvedParameters);
27
        }
28
 
29
        foreach ($parameters as $index => $parameter) {
30
            if (array_key_exists($parameter->name, $providedParameters)) {
31
                $resolvedParameters[$index] = $providedParameters[$parameter->name];
32
            }
33
        }
34
 
35
        return $resolvedParameters;
36
    }
37
}