parent
ede43fb01c
commit
2981e01945
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace jrosset\CliProgram\Validation\Validators;
|
||||
|
||||
/**
|
||||
* An argument/option value validator expecting an email address
|
||||
*/
|
||||
class EmailValidator implements IValidator {
|
||||
use TInternalValueValidator;
|
||||
use TIdenticalValidDefaultValidator;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function validate (mixed $value): bool {
|
||||
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->setValue($value);
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace jrosset\Tests\Commands;
|
||||
|
||||
use jrosset\CliProgram\Validation\CommandWithValidation;
|
||||
use jrosset\CliProgram\Validation\Validators\EmailValidator;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Email extends CommandWithValidation {
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function configure () {
|
||||
parent::configure();
|
||||
|
||||
$this->addArgument(
|
||||
'email',
|
||||
InputArgument::REQUIRED,
|
||||
'The email address',
|
||||
null,
|
||||
new EmailValidator()
|
||||
);
|
||||
}
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function execute (InputInterface $input, OutputInterface $output): int {
|
||||
$output->writeln('<info>' . $input->getArgument('email') . '</info>');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue