| 1 |
<?php |
| 2 |
// phpcs:ignoreFile -- Bundled third-party (Mozart) dependency; exempt from plugin coding standards. |
| 3 |
|
| 4 |
declare(strict_types=1); |
| 5 |
|
| 6 |
namespace WPDeveloper\BetterDocs\Dependencies\DI\Definition; |
| 7 |
|
| 8 |
/** |
| 9 |
* Defines a reference to an environment variable, with fallback to a default |
| 10 |
* value if the environment variable is not defined. |
| 11 |
* |
| 12 |
* @author James Harris <james.harris@icecave.com.au> |
| 13 |
*/ |
| 14 |
class EnvironmentVariableDefinition implements Definition |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Entry name. |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
private $name = ''; |
| 21 |
|
| 22 |
/** |
| 23 |
* The name of the environment variable. |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $variableName; |
| 27 |
|
| 28 |
/** |
| 29 |
* Whether or not the environment variable definition is optional. |
| 30 |
* |
| 31 |
* If true and the environment variable given by $variableName has not been |
| 32 |
* defined, $defaultValue is used. |
| 33 |
* |
| 34 |
* @var bool |
| 35 |
*/ |
| 36 |
private $isOptional; |
| 37 |
|
| 38 |
/** |
| 39 |
* The default value to use if the environment variable is optional and not provided. |
| 40 |
* @var mixed |
| 41 |
*/ |
| 42 |
private $defaultValue; |
| 43 |
|
| 44 |
/** |
| 45 |
* @param string $variableName The name of the environment variable |
| 46 |
* @param bool $isOptional Whether or not the environment variable definition is optional |
| 47 |
* @param mixed $defaultValue The default value to use if the environment variable is optional and not provided |
| 48 |
*/ |
| 49 |
public function __construct(string $variableName, bool $isOptional = false, $defaultValue = null) |
| 50 |
{ |
| 51 |
$this->variableName = $variableName; |
| 52 |
$this->isOptional = $isOptional; |
| 53 |
$this->defaultValue = $defaultValue; |
| 54 |
} |
| 55 |
|
| 56 |
public function getName() : string |
| 57 |
{ |
| 58 |
return $this->name; |
| 59 |
} |
| 60 |
|
| 61 |
public function setName(string $name) |
| 62 |
{ |
| 63 |
$this->name = $name; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @return string The name of the environment variable |
| 68 |
*/ |
| 69 |
public function getVariableName() : string |
| 70 |
{ |
| 71 |
return $this->variableName; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @return bool Whether or not the environment variable definition is optional |
| 76 |
*/ |
| 77 |
public function isOptional() : bool |
| 78 |
{ |
| 79 |
return $this->isOptional; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @return mixed The default value to use if the environment variable is optional and not provided |
| 84 |
*/ |
| 85 |
public function getDefaultValue() |
| 86 |
{ |
| 87 |
return $this->defaultValue; |
| 88 |
} |
| 89 |
|
| 90 |
public function replaceNestedDefinitions(callable $replacer) |
| 91 |
{ |
| 92 |
$this->defaultValue = $replacer($this->defaultValue); |
| 93 |
} |
| 94 |
|
| 95 |
public function __toString() |
| 96 |
{ |
| 97 |
$str = ' variable = ' . $this->variableName . PHP_EOL |
| 98 |
. ' optional = ' . ($this->isOptional ? 'yes' : 'no'); |
| 99 |
|
| 100 |
if ($this->isOptional) { |
| 101 |
if ($this->defaultValue instanceof Definition) { |
| 102 |
$nestedDefinition = (string) $this->defaultValue; |
| 103 |
$defaultValueStr = str_replace(PHP_EOL, PHP_EOL . ' ', $nestedDefinition); |
| 104 |
} else { |
| 105 |
$defaultValueStr = var_export($this->defaultValue, true); |
| 106 |
} |
| 107 |
|
| 108 |
$str .= PHP_EOL . ' default = ' . $defaultValueStr; |
| 109 |
} |
| 110 |
|
| 111 |
return sprintf('Environment variable (' . PHP_EOL . '%s' . PHP_EOL . ')', $str); |
| 112 |
} |
| 113 |
} |
| 114 |
|