From 6dd0d020c15970cf645874fde7c6e011afad7bcc Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Fri, 24 Apr 2026 15:33:23 +0200 Subject: [PATCH] Add StringValidator --- .../Validation/Validators/StringValidator.php | 156 ++++++++++++++++++ tests/Commands/Hello.php | 10 +- 2 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 src/CliProgram/Validation/Validators/StringValidator.php diff --git a/src/CliProgram/Validation/Validators/StringValidator.php b/src/CliProgram/Validation/Validators/StringValidator.php new file mode 100644 index 0000000..e1e2572 --- /dev/null +++ b/src/CliProgram/Validation/Validators/StringValidator.php @@ -0,0 +1,156 @@ + + */ +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; + } +} \ No newline at end of file diff --git a/tests/Commands/Hello.php b/tests/Commands/Hello.php index 03b5ed7..1874a1a 100644 --- a/tests/Commands/Hello.php +++ b/tests/Commands/Hello.php @@ -10,6 +10,7 @@ use jrosset\CliProgram\Validation\CommandWithValidation; use jrosset\CliProgram\Validation\Validators\DateValidator; use jrosset\CliProgram\Validation\Validators\EnumValidator; use jrosset\CliProgram\Validation\Validators\IntegerValidator; +use jrosset\CliProgram\Validation\Validators\StringValidator; use jrosset\Tests\Lang; use jrosset\Tests\SuccessRequirement; use Symfony\Component\Console\Command\Command; @@ -34,6 +35,13 @@ class Hello extends CommandWithValidation { protected function configure (): void { parent::configure(); + $this->addArgument( + 'name', + InputArgument::REQUIRED, + 'The name', + null, + new StringValidator() + ); $this->addArgument( 'day', InputArgument::OPTIONAL, @@ -66,7 +74,7 @@ class Hello extends CommandWithValidation { protected function execute (InputInterface $input, OutputInterface $output): int { $output->writeln('Command : ' . __CLASS__ . '', OutputInterface::VERBOSITY_DEBUG); - $text = ($input->getOption('lang') ?? Lang::English)->value; + $text = ($input->getOption('lang') ?? Lang::English)->value . ' ' . $input->getArgument('name'); $repeat = $input->getOption('repeat'); /** @var DateTimeInterface $day */