You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
4.0 KiB
PHP
156 lines
4.0 KiB
PHP
<?php
|
|
/**
|
|
* Déclaration de la classe CommandLine\Argument\Parser\IntegerParser
|
|
*/
|
|
|
|
namespace jrosset\CommandLine\Argument\Parser;
|
|
|
|
use InvalidArgumentException;
|
|
use RangeException;
|
|
|
|
/**
|
|
* Parseur vers un entier
|
|
*
|
|
* @package CommandLine\Argument\Parser
|
|
*/
|
|
class IntegerParser implements IValueParser {
|
|
/**
|
|
* @var int|null La valeur minimum autorisée. Null si pas de limite.
|
|
*/
|
|
protected ?int $valueMin = null;
|
|
/**
|
|
* @var int|null La valeur maximum autorisée. Null si pas de limite
|
|
*/
|
|
protected ?int $valueMax = null;
|
|
|
|
/**
|
|
* Crée un nouveau parseur
|
|
*
|
|
* @param int|null $valueMin La valeur minimum autorisée
|
|
* @param int|null $valueMax La valeur maximum autorisée
|
|
*/
|
|
public function __construct (?int $valueMin = null, ?int $valueMax = null) {
|
|
$this->setValueMin($valueMin);
|
|
$this->setValueMax($valueMax);
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function parseValue (string $val) {
|
|
if (!$this->isInt($val)) {
|
|
throw new RangeException('La valeur n\'est pas un entier valide : ' . $val);
|
|
}
|
|
|
|
$int = (int)$val;
|
|
|
|
if ($this->hasValueMin()) {
|
|
if ($int < $this->getValueMin()) {
|
|
throw new RangeException('La valeur est inférieure au minimum (' . $this->getValueMin() . ') :' . $int);
|
|
}
|
|
}
|
|
if ($this->hasValueMax()) {
|
|
if ($int > $this->getValueMax()) {
|
|
throw new RangeException('La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $int);
|
|
}
|
|
}
|
|
|
|
return $int;
|
|
}
|
|
|
|
/**
|
|
* Vérifie si la valeur est bien un nombre entier ?
|
|
*
|
|
* @return bool Est-ce que la valeur est bien un nombre entier ?
|
|
*/
|
|
protected function isInt ($val): bool {
|
|
if (!is_numeric($val)) {
|
|
return false;
|
|
}
|
|
|
|
if (floor($val) != $val) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getValueDescription (): string {
|
|
return ($this->hasValueMin() ? $this->getValueMin() . ' <= ' : '') . 'integer' . ($this->hasValueMax() ? ' <= ' . $this->getValueMax() : '');
|
|
}
|
|
|
|
/**
|
|
* La valeur minimumm autorisée
|
|
*
|
|
* @return int|null La valeur minimum.
|
|
*
|
|
* @see $valueMin
|
|
*/
|
|
public function getValueMin (): ?int {
|
|
return $this->valueMin;
|
|
}
|
|
/**
|
|
* Définit la valeur minimum autorisée.
|
|
*
|
|
* @param int|null $valueMin La valeur minimum.
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function setValueMin (?int $valueMin = null): self {
|
|
if (!is_null($valueMin)) {
|
|
if (!$this->isInt($valueMin)) {
|
|
throw new InvalidArgumentException('Le minimum n\'est pas un entier ou null : ' . $valueMin);
|
|
}
|
|
}
|
|
|
|
$this->valueMin = $valueMin;
|
|
return $this;
|
|
}
|
|
/**
|
|
* Est-ce qu'il existe une limite minimum ?
|
|
*
|
|
* @return boolean True s'il existe une limite, sinon False
|
|
*/
|
|
public function hasValueMin (): bool {
|
|
return !is_null($this->getValueMin());
|
|
}
|
|
|
|
/**
|
|
* La valeur maximum autorisée
|
|
*
|
|
* @return int|null La valeur maximum.
|
|
*
|
|
* @see $valueMax
|
|
*/
|
|
public function getValueMax (): ?int {
|
|
return $this->valueMax;
|
|
}
|
|
/**
|
|
* Définit la valeur maximum autorisée.
|
|
*
|
|
* @param int|null $valueMax La valeur maximum.
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function setValueMax (?int $valueMax = null): self {
|
|
if (!is_null($valueMax)) {
|
|
if (!$this->isInt($valueMax)) {
|
|
throw new InvalidArgumentException('Le maximum n\'est pas un entier ou null : ' . $valueMax);
|
|
}
|
|
}
|
|
|
|
$this->valueMax = $valueMax;
|
|
return $this;
|
|
}
|
|
/**
|
|
* Est-ce qu'il existe une limite maximum ?
|
|
*
|
|
* @return boolean True s'il existe une limite, sinon False
|
|
*/
|
|
public function hasValueMax (): bool {
|
|
return !is_null($this->getValueMax());
|
|
}
|
|
} |