Add StringValidator

master 3.16.0
Julien Rosset 6 days ago
parent eec4c1f0c1
commit 6dd0d020c1

@ -0,0 +1,156 @@
<?php
namespace jrosset\CliProgram\Validation\Validators;
use jrosset\MbstringExtended;
/**
* An argument/option value validator for a string
*
* @implements IValidator<string>
*/
class StringValidator implements IValidator {
use TIdenticalValidDefaultValidator;
use TInternalValueValidator;
/**
* @var bool Trim the string ?
*/
private bool $trim;
/**
* @var bool Allow empty string ?
*/
private bool $allowEmpty;
/**
* @var int|null The minimal length of the string; Null if none
*/
private ?int $minLength = null;
/**
* @var int|null The maximal length of the string; Null if none
*/
private ?int $maxLength = null;
/**
* Initialization
*
* @param bool $trim Trim the string ?
* @param bool $allowEmpty Allow empty string ?
*/
public function __construct (bool $trim = true, bool $allowEmpty = false) {
$this->trim = $trim;
$this->allowEmpty = $allowEmpty;
}
/**
* @inheritDoc
*/
public function validate (mixed $value): bool {
if ($value === null) {
if (!$this->allowEmpty) {
throw new InvalidValueException('The value must not be null (empty string are not allowed)');
}
$this->setValue(null);
return true;
}
$value = (string)$value;
if ($this->trim) {
$value = MbstringExtended::trim($value);
}
if ($this->minLength !== null && mb_strlen($value) < $this->minLength) {
throw new InvalidValueException('The value is too short');
}
if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) {
throw new InvalidValueException('The value is too long');
}
if ($value === '') {
if (!$this->allowEmpty) {
throw new InvalidValueException('The value must not be empty (empty string are not allowed)');
}
$value = null;
}
$this->setValue($value);
return true;
}
/**
* Trim the string ?
*
* @return bool Trim the string ?
*/
public function isTrim (): bool {
return $this->trim;
}
/**
* Set if trim the string.
*
* @param bool $trim Trim the string ?
*
* @return $this
*/
public function setTrim (bool $trim): self {
$this->trim = $trim;
return $this;
}
/**
* Allow empty string ?
*
* @return bool Allow empty string ?
*/
public function isAllowEmpty (): bool {
return $this->allowEmpty;
}
/**
* Set if allow empty string.
*
* @param bool $allowEmpty Allow empty string ?
*
* @return $this
*/
public function setAllowEmpty (bool $allowEmpty): self {
$this->allowEmpty = $allowEmpty;
return $this;
}
/**
* The minimal length of the string
*
* @return int|null The minimal length of the string; Null if none
*/
public function getMinLength (): ?int {
return $this->minLength;
}
/**
* Set the minimal length of the string
*
* @param int|null $minLength The minimal length of the string; Null if none
*
* @return $this
*/
public function setMinLength (?int $minLength): self {
$this->minLength = $minLength;
return $this;
}
/**
* The maximal length of the string
*
* @return int|null The maximal length of the string; Null if none
*/
public function getMaxLength (): ?int {
return $this->maxLength;
}
/**
* Set the maximal length of the string
*
* @param int|null $maxLength The maximal length of the string; Null if none
*
* @return $this
*/
public function setMaxLength (?int $maxLength): self {
$this->maxLength = $maxLength;
return $this;
}
}

@ -10,6 +10,7 @@ use jrosset\CliProgram\Validation\CommandWithValidation;
use jrosset\CliProgram\Validation\Validators\DateValidator; use jrosset\CliProgram\Validation\Validators\DateValidator;
use jrosset\CliProgram\Validation\Validators\EnumValidator; use jrosset\CliProgram\Validation\Validators\EnumValidator;
use jrosset\CliProgram\Validation\Validators\IntegerValidator; use jrosset\CliProgram\Validation\Validators\IntegerValidator;
use jrosset\CliProgram\Validation\Validators\StringValidator;
use jrosset\Tests\Lang; use jrosset\Tests\Lang;
use jrosset\Tests\SuccessRequirement; use jrosset\Tests\SuccessRequirement;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
@ -34,6 +35,13 @@ class Hello extends CommandWithValidation {
protected function configure (): void { protected function configure (): void {
parent::configure(); parent::configure();
$this->addArgument(
'name',
InputArgument::REQUIRED,
'The name',
null,
new StringValidator()
);
$this->addArgument( $this->addArgument(
'day', 'day',
InputArgument::OPTIONAL, InputArgument::OPTIONAL,
@ -66,7 +74,7 @@ class Hello extends CommandWithValidation {
protected function execute (InputInterface $input, OutputInterface $output): int { protected function execute (InputInterface $input, OutputInterface $output): int {
$output->writeln('<info>Command : ' . __CLASS__ . '</info>', OutputInterface::VERBOSITY_DEBUG); $output->writeln('<info>Command : ' . __CLASS__ . '</info>', OutputInterface::VERBOSITY_DEBUG);
$text = ($input->getOption('lang') ?? Lang::English)->value; $text = ($input->getOption('lang') ?? Lang::English)->value . ' ' . $input->getArgument('name');
$repeat = $input->getOption('repeat'); $repeat = $input->getOption('repeat');
/** @var DateTimeInterface $day */ /** @var DateTimeInterface $day */

Loading…
Cancel
Save