| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WPDeveloper\BetterDocs\Dependencies\DI\Definition\ObjectDefinition; |
| 6 |
|
| 7 |
use WPDeveloper\BetterDocs\Dependencies\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 |
* @var string |
| 18 |
*/ |
| 19 |
private $methodName; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var mixed[] |
| 23 |
*/ |
| 24 |
private $parameters = []; |
| 25 |
|
| 26 |
public function __construct(string $methodName, array $parameters = []) |
| 27 |
{ |
| 28 |
$this->methodName = (string) $methodName; |
| 29 |
$this->parameters = $parameters; |
| 30 |
} |
| 31 |
|
| 32 |
public static function constructor(array $parameters = []) : self |
| 33 |
{ |
| 34 |
return new self('__construct', $parameters); |
| 35 |
} |
| 36 |
|
| 37 |
public function getMethodName() : string |
| 38 |
{ |
| 39 |
return $this->methodName; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @return mixed[] |
| 44 |
*/ |
| 45 |
public function getParameters() : array |
| 46 |
{ |
| 47 |
return $this->parameters; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Replace the parameters of the definition by a new array of parameters. |
| 52 |
*/ |
| 53 |
public function replaceParameters(array $parameters) |
| 54 |
{ |
| 55 |
$this->parameters = $parameters; |
| 56 |
} |
| 57 |
|
| 58 |
public function merge(self $definition) |
| 59 |
{ |
| 60 |
// In case of conflicts, the current definition prevails. |
| 61 |
$this->parameters = $this->parameters + $definition->parameters; |
| 62 |
} |
| 63 |
|
| 64 |
public function getName() : string |
| 65 |
{ |
| 66 |
return ''; |
| 67 |
} |
| 68 |
|
| 69 |
public function setName(string $name) |
| 70 |
{ |
| 71 |
// The name does not matter for method injections |
| 72 |
} |
| 73 |
|
| 74 |
public function replaceNestedDefinitions(callable $replacer) |
| 75 |
{ |
| 76 |
$this->parameters = array_map($replacer, $this->parameters); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* {@inheritdoc} |
| 81 |
*/ |
| 82 |
public function __toString() |
| 83 |
{ |
| 84 |
return sprintf('method(%s)', $this->methodName); |
| 85 |
} |
| 86 |
} |
| 87 |
|