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.
phpcommandline/Argument/Parser/IntegerParser.class.php

141 lines
4.0 KiB
PHP

<?php
/**
* Déclaration de la classe CommandLine\Argument\Parser\IntegerParser
*/
namespace CommandLine\Argument\Parser;
use Fidit\v3\Exception\InvalidArgument;
/**
* Parseur vers un entier.
*
* @property int|null $valueMin {@see $_valueMin $_valueMin}
* @property int|null $valueMax {@see $_valueMax $_valueMax}
*
* @package CommandLine\Argument\Parser
*/
class IntegerParser implements IValueParser {
/**
* @var int|null La valeur minimum autorisée. Null si pas de limite.
*/
protected $_valueMin = null;
/**
* @var int|null La valeur maximum autorisée. Null si pas de limite
*/
protected $_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
*
* @throws InvalidArgument Si l'un des paramètre est invalide.
*/
public function __construct ($valueMin = null, $valueMax = null) {
$this->setValueMin($valueMin);
$this->setValueMax($valueMax);
}
public function parseValue ($arg) {
if(!$this->_isInt($arg))
throw new InvalidArgument($arg, 'La valeur n\'est pas un entier valide');
$int = (int)$arg;
if($this->hasValueMin()) {
if($int < $this->getValueMin())
throw new InvalidArgument($int, 'La valeur est inférieure au minimum : '.$this->getValueMin());
}
if($this->hasValueMax()) {
if($int > $this->getValueMax())
throw new InvalidArgument($int, 'La valeur est supérieur au maximum : '.$this->getValueMax());
}
return $int;
}
protected function _isInt($val) {
if(!is_numeric($val))
return false;
if(floor($val) != $val)
return false;
return true;
}
public function getValueDescription () {
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() {
return $this->_valueMin;
}
/**
* Définit la valeur minimum autorisée.
*
* @param int|null $valueMin La valeur minimum.
*
* @return $this
*
* @throws InvalidArgument Si la valeur minimum n'est ni null, ni un entier
*/
public function setValueMin($valueMin = null) {
if(!is_null($valueMin) && !$this->_isInt($valueMin))
throw new InvalidArgument($valueMin, 'La valeur n\'est pas un entier ou null');
$this->_valueMin = (int)$valueMin;
return $this;
}
/**
* Est-ce qu'il existe une limite minimum ?
*
* @return boolean True s'il existe une limite, sinon False
*/
public function hasValueMin() {
return !is_null($this->getValueMin());
}
/**
* La valeur maximum autorisée
*
* @return int|null La valeur maximum.
*
* @see $_valueMax
*/
public function getValueMax() {
return $this->_valueMax;
}
/**
* Définit la valeur maximum autorisée.
*
* @param int|null $valueMax La valeur maximum.
*
* @return $this
*
* @throws InvalidArgument Si la valeur maximum n'est ni null, ni un entier
*/
public function setValueMax($valueMax = null) {
if(!is_null($valueMax) && !$this->_isInt($valueMax))
throw new InvalidArgument($valueMax, 'La valeur n\'est pas un entier ou null');
$this->_valueMax = (int)$valueMax;
return $this;
}
/**
* Est-ce qu'il existe une limite maximum ?
*
* @return boolean True s'il existe une limite, sinon False
*/
public function hasValueMax() {
return !is_null($this->getValueMax());
}
}