Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace DI\Definition\ObjectDefinition;
6
 
7
use DI\Definition\Definition;
8
 
9
/**
10
 * Describe an injection in an object method.
11
 *
12
 * @author Matthieu Napoli <matthieu@mnapoli.fr>
13
 */
14
class MethodInjection implements Definition
15
{
16
    /**
17
     * @param mixed[] $parameters
18
     */
19
    public function __construct(
20
        private string $methodName,
21
        private array $parameters = [],
22
    ) {
23
    }
24
 
25
    public static function constructor(array $parameters = []) : self
26
    {
27
        return new self('__construct', $parameters);
28
    }
29
 
30
    public function getMethodName() : string
31
    {
32
        return $this->methodName;
33
    }
34
 
35
    /**
36
     * @return mixed[]
37
     */
38
    public function getParameters() : array
39
    {
40
        return $this->parameters;
41
    }
42
 
43
    /**
44
     * Replace the parameters of the definition by a new array of parameters.
45
     */
46
    public function replaceParameters(array $parameters) : void
47
    {
48
        $this->parameters = $parameters;
49
    }
50
 
51
    public function merge(self $definition) : void
52
    {
53
        // In case of conflicts, the current definition prevails.
54
        $this->parameters += $definition->parameters;
55
    }
56
 
57
    public function getName() : string
58
    {
59
        return '';
60
    }
61
 
62
    public function setName(string $name) : void
63
    {
64
        // The name does not matter for method injections
65
    }
66
 
67
    public function replaceNestedDefinitions(callable $replacer) : void
68
    {
69
        $this->parameters = array_map($replacer, $this->parameters);
70
    }
71
 
72
    public function __toString() : string
73
    {
74
        return sprintf('method(%s)', $this->methodName);
75
    }
76
}