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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue