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/DecimalParser.class.php

139 lines
3.6 KiB
PHP

<?php
/**
* Déclaration de la classe CommandLine\Argument\Parser\DecimalParser
*/
namespace CommandLine\Argument\Parser;
use InvalidArgumentException;
use RangeException;
/**
* Parseur vers un réel
*
* @package CommandLine\Argument\Parser
*/
class DecimalParser implements IValueParser {
/**
* @var double|null La valeur minimum autorisée. Null si pas de limite.
*/
protected $_valueMin = null;
/**
* @var double|null La valeur maximum autorisée. Null si pas de limite
*/
protected $_valueMax = null;
/**
* Crée un nouveau parseur
*
* @param double|null $valueMin La valeur minimum autorisée
* @param double|null $valueMax La valeur maximum autorisée
*/
public function __construct ($valueMin = null, $valueMax = null) {
$this->setValueMin($valueMin);
$this->setValueMax($valueMax);
}
/**
* @inheritDoc
*/
public function parseValue ($arg) {
if (!$this->_isDecimal($arg))
throw new RangeException('La valeur n\'est pas un réel valide : ' . $arg);
$int = (int)$arg;
if ($this->hasValueMin()) {
if ($int < $this->getValueMin())
throw new RangeException('La valeur est inférieure au minimum (' . $this->getValueMin().') : ' . $arg);
}
if ($this->hasValueMax()) {
if ($int > $this->getValueMax())
throw new RangeException($int, 'La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $arg);
}
return $int;
}
/**
* @inheritDoc
*/
protected function _isDecimal($val) {
if(!is_numeric($val))
return false;
return true;
}
/**
* @inheritDoc
*/
public function getValueDescription () {
return ($this->hasValueMin() ? $this->getValueMin().' <= ' : '').'decimal'.($this->hasValueMax() ? ' <= '.$this->getValueMax() : '');
}
/**
* La valeur minimumm autorisée
*
* @return double|null La valeur minimum.
*
* @see $_valueMin
*/
public function getValueMin() {
return $this->_valueMin;
}
/**
* Définit la valeur minimum autorisée.
*
* @param double|null $valueMin La valeur minimum.
*
* @return $this
*/
public function setValueMin($valueMin = null) {
if(!is_null($valueMin) && !$this->_isDecimal($valueMin))
throw new InvalidArgumentException('La valeur n\'est pas un entier ou null' . $valueMin);
$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 double|null La valeur maximum.
*
* @see $_valueMax
*/
public function getValueMax() {
return $this->_valueMax;
}
/**
* Définit la valeur maximum autorisée.
*
* @param double|null $valueMax La valeur maximum.
*
* @return $this
*/
public function setValueMax($valueMax = null) {
if(!is_null($valueMax) && !$this->_isDecimal($valueMax))
throw new InvalidArgumentException('La valeur n\'est pas un entier ou null : ' . $valueMax);
$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());
}
}