From cf547dee95fccb39281535a2064d31faa1d9203c Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Wed, 12 Jul 2023 18:39:39 +0200 Subject: [PATCH] Add EnumValidator --- .../Validation/Validators/EnumValidator.php | 62 +++++++++++++++++++ tests/Commands/Hello.php | 18 +++++- tests/Lang.php | 9 +++ 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 src/CliProgram/Validation/Validators/EnumValidator.php create mode 100644 tests/Lang.php diff --git a/src/CliProgram/Validation/Validators/EnumValidator.php b/src/CliProgram/Validation/Validators/EnumValidator.php new file mode 100644 index 0000000..8974f35 --- /dev/null +++ b/src/CliProgram/Validation/Validators/EnumValidator.php @@ -0,0 +1,62 @@ + + * @template-implements TInternalValueValidator + */ +class EnumValidator extends BasedValidator { + /** + * @var ReflectionEnum The enumeration + */ + private ReflectionEnum $enum; + + /** + * Create a validator + * + * @param class-string $enumClass The enumeration class + * + * @throws ReflectionException If the enumeration class doesn't exist + */ + public function __construct (string $enumClass) { + $this->enum = new ReflectionEnum($enumClass); + + $enumCases = new Collection(); + foreach ($this->enum->getCases() as $enumCase) { + $enumCases->add($enumCase->getName()); + } + + parent::__construct(new ListValidator($enumCases)); + } + + /** + * @inheritDoc + * + * @throws ReflectionException + */ + public function getValidDefault (mixed $default): string|bool|int|float|array { + if ($default instanceof UnitEnum) { + $default = $default->name; + } + return $default; + } + + /** + * @inheritDoc + * + * @throws ReflectionException + */ + public function getValue (): UnitEnum { + $enumCase = $this->getInternalValidator()->getValue(); + return $this->enum->getCase($enumCase)->getValue(); + } +} \ No newline at end of file diff --git a/tests/Commands/Hello.php b/tests/Commands/Hello.php index 17c0844..a447edd 100644 --- a/tests/Commands/Hello.php +++ b/tests/Commands/Hello.php @@ -4,7 +4,9 @@ namespace jrosset\Tests\Commands; use jrosset\CliProgram\Monolog\ConsoleOutputWithMonolog; use jrosset\CliProgram\Validation\CommandWithValidation; +use jrosset\CliProgram\Validation\Validators\EnumValidator; use jrosset\CliProgram\Validation\Validators\IntegerValidator; +use jrosset\Tests\Lang; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -30,7 +32,15 @@ class Hello extends CommandWithValidation { parent::configure(); $this->addArgument( + 'lang', + InputArgument::OPTIONAL, + 'The lang', + Lang::English, + new EnumValidator(Lang::class) + ); + $this->addOption( 'repeat', + 'r', InputArgument::OPTIONAL, 'The number of repeat', 1, @@ -43,10 +53,12 @@ class Hello extends CommandWithValidation { */ protected function execute (InputInterface $input, OutputInterface $output): int { $output->writeln('Command : ' . __CLASS__, OutputInterface::VERBOSITY_DEBUG); - $repeat = $input->getArgument('repeat'); - var_dump($repeat); + + /** @var Lang $lang */ + $lang = $input->getArgument('lang'); + $repeat = $input->getOption('repeat'); for ($curr = 0; $curr < $repeat; $curr++) { - $output->writeln('Hello !'); + $output->writeln($lang->value); } $output->writeln('FIN', ConsoleOutputWithMonolog::OPTION_SKIP_MONOLOG); return Command::SUCCESS; diff --git a/tests/Lang.php b/tests/Lang.php new file mode 100644 index 0000000..2c733e1 --- /dev/null +++ b/tests/Lang.php @@ -0,0 +1,9 @@ +