-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(console): accept
BackedEnum
as command arguments (#722)
- Loading branch information
Showing
8 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/Tempest/Console/src/Exceptions/InvalidEnumArgument.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Console\Exceptions; | ||
|
||
use function array_map; | ||
use BackedEnum; | ||
use function gettype; | ||
use function implode; | ||
use function is_string; | ||
use Tempest\Console\Console; | ||
|
||
final class InvalidEnumArgument extends ConsoleException | ||
{ | ||
/** | ||
* @param class-string<BackedEnum> $argumentType | ||
*/ | ||
public function __construct( | ||
private string $argumentName, | ||
private string $argumentType, | ||
private mixed $value, | ||
) { | ||
} | ||
|
||
public function render(Console $console): void | ||
{ | ||
if (is_string($this->value) || is_numeric($this->value)) { | ||
$value = "`{$this->value}`"; | ||
} else { | ||
$value = 'of type `' . gettype($this->value) . '`'; | ||
} | ||
|
||
$cases = array_map( | ||
callback: fn (BackedEnum $case) => $case->value, | ||
array: $this->argumentType::cases(), | ||
); | ||
$console->error("Invalid argument {$value} for `{$this->argumentName}` argument, valid values are: " . implode(', ', $cases)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Integration\Console\Fixtures; | ||
|
||
enum TestStringEnum: string | ||
{ | ||
case A = 'a'; | ||
case B = 'b'; | ||
case C = 'c'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters