| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WPDeveloper\BetterDocs\Dependencies\DI\Definition; |
| 6 |
|
| 7 |
use WPDeveloper\BetterDocs\Dependencies\Psr\Container\ContainerInterface; |
| 8 |
|
| 9 |
/** |
| 10 |
* Represents a reference to another entry. |
| 11 |
* |
| 12 |
* @author Matthieu Napoli <matthieu@mnapoli.fr> |
| 13 |
*/ |
| 14 |
class Reference implements Definition, SelfResolvingDefinition |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Entry name. |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
private $name = ''; |
| 21 |
|
| 22 |
/** |
| 23 |
* Name of the target entry. |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $targetEntryName; |
| 27 |
|
| 28 |
/** |
| 29 |
* @param string $targetEntryName Name of the target entry |
| 30 |
*/ |
| 31 |
public function __construct(string $targetEntryName) |
| 32 |
{ |
| 33 |
$this->targetEntryName = $targetEntryName; |
| 34 |
} |
| 35 |
|
| 36 |
public function getName() : string |
| 37 |
{ |
| 38 |
return $this->name; |
| 39 |
} |
| 40 |
|
| 41 |
public function setName(string $name) |
| 42 |
{ |
| 43 |
$this->name = $name; |
| 44 |
} |
| 45 |
|
| 46 |
public function getTargetEntryName() : string |
| 47 |
{ |
| 48 |
return $this->targetEntryName; |
| 49 |
} |
| 50 |
|
| 51 |
public function resolve(ContainerInterface $container) |
| 52 |
{ |
| 53 |
return $container->get($this->getTargetEntryName()); |
| 54 |
} |
| 55 |
|
| 56 |
public function isResolvable(ContainerInterface $container) : bool |
| 57 |
{ |
| 58 |
return $container->has($this->getTargetEntryName()); |
| 59 |
} |
| 60 |
|
| 61 |
public function replaceNestedDefinitions(callable $replacer) |
| 62 |
{ |
| 63 |
// no nested definitions |
| 64 |
} |
| 65 |
|
| 66 |
public function __toString() |
| 67 |
{ |
| 68 |
return sprintf( |
| 69 |
'get(%s)', |
| 70 |
$this->targetEntryName |
| 71 |
); |
| 72 |
} |
| 73 |
} |
| 74 |
|