chrif /
cocotte
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace Cocotte\Machine; |
||
| 4 | |||
| 5 | use Assert\Assertion; |
||
| 6 | use Cocotte\Environment\FromEnvLazyFactory; |
||
| 7 | use Cocotte\Environment\LazyEnvironmentValue; |
||
| 8 | use Cocotte\Environment\LazyExportableOption; |
||
| 9 | use Cocotte\Shell\Env; |
||
| 10 | |||
| 11 | class MachineName implements LazyExportableOption, FromEnvLazyFactory |
||
| 12 | { |
||
| 13 | const DEFAULT_VALUE = 'cocotte'; |
||
| 14 | const MACHINE_NAME = 'MACHINE_NAME'; |
||
| 15 | const OPTION_NAME = 'machine-name'; |
||
| 16 | /** |
||
| 17 | * https://github.com/docker/machine/blob/v0.14.0/libmachine/host/host.go#L24 |
||
| 18 | */ |
||
| 19 | const REGEX = '/^[a-zA-Z0-9][a-zA-Z0-9\-\.]*$/'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $value; |
||
| 25 | |||
| 26 | public function __construct(string $value) |
||
| 27 | { |
||
| 28 | Assertion::notEmpty($value, "The machine name is empty"); |
||
| 29 | Assertion::regex($value, self::REGEX, "The machine name does not match ".self::REGEX); |
||
| 30 | $this->value = $value; |
||
| 31 | } |
||
| 32 | |||
| 33 | public static function fromString(string $value): self |
||
| 34 | { |
||
| 35 | return new self($value); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param Env $env |
||
| 40 | * @return LazyEnvironmentValue|self |
||
| 41 | */ |
||
| 42 | public static function fromEnv(Env $env): LazyEnvironmentValue |
||
| 43 | { |
||
| 44 | return new self($env->get(self::MACHINE_NAME, "")); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 45 | } |
||
| 46 | |||
| 47 | public static function toEnv(string $value, Env $env): void |
||
| 48 | { |
||
| 49 | $env->put(self::MACHINE_NAME, $value); |
||
| 50 | } |
||
| 51 | |||
| 52 | public static function optionName(): string |
||
| 53 | { |
||
| 54 | return self::OPTION_NAME; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function toString(): string |
||
| 58 | { |
||
| 59 | return $this->value; |
||
| 60 | } |
||
| 61 | |||
| 62 | public function __toString() |
||
| 63 | { |
||
| 64 | return $this->toString(); |
||
| 65 | } |
||
| 66 | |||
| 67 | } |