Fixing InvalidArgumentException in parsers
parent
3d869f684f
commit
33d04e872c
@ -1,121 +1,125 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\AbstractArgument.
|
||||
*/
|
||||
namespace CommandLine\Argument;
|
||||
|
||||
use Fidit\v3\AutoProperty;
|
||||
use Fidit\v3\Exception\InvalidArgument;
|
||||
|
||||
/**
|
||||
* Classe abstraite pour n'importe quel type de paramètre
|
||||
*
|
||||
* @property string $varName {@see $_varName $_varName }
|
||||
* @property string $name {@see $_name $_name }
|
||||
* @property string|null $description {@see $_description $_description}
|
||||
* @property mixed|null $default {@see $_default $_default }
|
||||
*
|
||||
* @package CommandLine\Argument
|
||||
*/
|
||||
abstract class ArgumentAbstract extends AutoProperty implements IArgument {
|
||||
/**
|
||||
* @var string Le nom de la variable de retour de l'argument.
|
||||
*/
|
||||
protected $_varName;
|
||||
/**
|
||||
* @var string Le nom de l'argument.
|
||||
*/
|
||||
protected $_name;
|
||||
/**
|
||||
* @var string|null La description de l'argument.
|
||||
*/
|
||||
protected $_description;
|
||||
|
||||
/**
|
||||
* @var mixed|null La valeur par défaut. Null si aucune.
|
||||
*/
|
||||
protected $_default = null;
|
||||
|
||||
/**
|
||||
* Crée un nouvel argument.
|
||||
*
|
||||
* @param string $name Le nom de l'argument
|
||||
* @param string|null $description La description de l'argument
|
||||
*/
|
||||
protected function __construct ($name, $description) {
|
||||
$this->setName($name, true);
|
||||
$this->setDescription($description);
|
||||
}
|
||||
|
||||
public function getVarName() {
|
||||
return $this->_varName;
|
||||
}
|
||||
/**
|
||||
* Définit le nom de la variable de retour de l'argument.
|
||||
*
|
||||
* @param string $varName Le nom
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see $_name
|
||||
*/
|
||||
public function setVarName($varName) {
|
||||
$this->_varName = $varName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return $this->_name;
|
||||
}
|
||||
/**
|
||||
* Définit le nom de l'argument.
|
||||
*
|
||||
* @param string $name Le nom
|
||||
* @param boolean $replaceVarName Remplacer également le nom de la variable de retour ?
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see $_name
|
||||
*/
|
||||
public function setName($name, $replaceVarName = true) {
|
||||
$this->_name = $name;
|
||||
if($replaceVarName)
|
||||
$this->setVarName($name);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDescription() {
|
||||
return $this->_description;
|
||||
}
|
||||
/**
|
||||
* Définit la description de l'argument.
|
||||
*
|
||||
* @param string|null $description La description
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see $_description
|
||||
*/
|
||||
public function setDescription($description) {
|
||||
$this->_description = $description;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDefault() {
|
||||
return $this->_default;
|
||||
}
|
||||
/**
|
||||
* Définit le valeur par défaut
|
||||
*
|
||||
* @param mixed|null $default La valeur par défaut.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see $_default
|
||||
*/
|
||||
public function setDefault($default = null) {
|
||||
$this->_default = $default;
|
||||
return $this;
|
||||
}
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\AbstractArgument.
|
||||
*/
|
||||
namespace CommandLine\Argument;
|
||||
|
||||
/**
|
||||
* Classe abstraite pour n'importe quel type de paramètre
|
||||
*
|
||||
* @package CommandLine\Argument
|
||||
*/
|
||||
abstract class ArgumentAbstract implements IArgument {
|
||||
/**
|
||||
* @var string Le nom de la variable de retour de l'argument.
|
||||
*/
|
||||
protected $_varName;
|
||||
/**
|
||||
* @var string Le nom de l'argument.
|
||||
*/
|
||||
protected $_name;
|
||||
/**
|
||||
* @var string|null La description de l'argument.
|
||||
*/
|
||||
protected $_description;
|
||||
|
||||
/**
|
||||
* @var mixed|null La valeur par défaut. Null si aucune.
|
||||
*/
|
||||
protected $_default = null;
|
||||
|
||||
/**
|
||||
* Crée un nouvel argument.
|
||||
*
|
||||
* @param string $name Le nom de l'argument
|
||||
* @param string|null $description La description de l'argument
|
||||
*/
|
||||
protected function __construct ($name, $description) {
|
||||
$this->setName($name, true);
|
||||
$this->setDescription($description);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getVarName() {
|
||||
return $this->_varName;
|
||||
}
|
||||
/**
|
||||
* Définit le nom de la variable de retour de l'argument.
|
||||
*
|
||||
* @param string $varName Le nom
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see $_name
|
||||
*/
|
||||
public function setVarName($varName) {
|
||||
$this->_varName = $varName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getName() {
|
||||
return $this->_name;
|
||||
}
|
||||
/**
|
||||
* Définit le nom de l'argument.
|
||||
*
|
||||
* @param string $name Le nom
|
||||
* @param boolean $replaceVarName Remplacer également le nom de la variable de retour ?
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see $_name
|
||||
*/
|
||||
public function setName($name, $replaceVarName = true) {
|
||||
$this->_name = $name;
|
||||
if($replaceVarName)
|
||||
$this->setVarName($name);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getDescription() {
|
||||
return $this->_description;
|
||||
}
|
||||
/**
|
||||
* Définit la description de l'argument.
|
||||
*
|
||||
* @param string|null $description La description
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see $_description
|
||||
*/
|
||||
public function setDescription($description) {
|
||||
$this->_description = $description;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getDefault() {
|
||||
return $this->_default;
|
||||
}
|
||||
/**
|
||||
* Définit le valeur par défaut
|
||||
*
|
||||
* @param mixed|null $default La valeur par défaut.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see $_default
|
||||
*/
|
||||
public function setDefault($default = null) {
|
||||
$this->_default = $default;
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -1,85 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\ParseResult.
|
||||
*/
|
||||
namespace CommandLine\Argument;
|
||||
|
||||
use Fidit\v3\AutoProperty;
|
||||
|
||||
/**
|
||||
* Résultat du parsage d'un argument
|
||||
*
|
||||
* @property int $consume {@see $_consume $_consume}
|
||||
* @property mixed $value {@see $_value $_value }
|
||||
*
|
||||
* @package CommandLine\Argument
|
||||
*/
|
||||
class ParseResult extends AutoProperty {
|
||||
/**
|
||||
* @var int Le nombre d'argument consumé.
|
||||
*/
|
||||
protected $_consume;
|
||||
/**
|
||||
* @var mixed La valeur.
|
||||
*/
|
||||
protected $_value;
|
||||
|
||||
/**
|
||||
* ArgumentParseResult constructor.
|
||||
*
|
||||
* @param mixed $value La valeur.
|
||||
* @param int $consume Le nombre d'argument consumé.
|
||||
*/
|
||||
public function __construct ($value, $consume = 1) {
|
||||
$this->setValue($value);
|
||||
$this->setConsume($consume);
|
||||
}
|
||||
|
||||
/**
|
||||
* Le nombre d'argument consumé.
|
||||
*
|
||||
* @return int Le nombre d'argument consumé.
|
||||
*
|
||||
* @see $_consume
|
||||
*/
|
||||
public function getConsume() {
|
||||
return $this->_consume;
|
||||
}
|
||||
/**
|
||||
* Définit le nombre d'argument consumé.
|
||||
*
|
||||
* @param $consume int Le nombre d'argument consumé.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see $_consume
|
||||
*/
|
||||
public function setConsume($consume) {
|
||||
$this->_consume = $consume;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* La valeur.
|
||||
*
|
||||
* @return mixed La valeur.
|
||||
*
|
||||
* @see $_value
|
||||
*/
|
||||
public function getValue() {
|
||||
return $this->_value;
|
||||
}
|
||||
/**
|
||||
* Définit les valeurs définies.
|
||||
*
|
||||
* @param mixed $value La valeur.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see $_value
|
||||
*/
|
||||
public function setValue($value) {
|
||||
$this->_value = $value;
|
||||
return $this;
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\ParseResult.
|
||||
*/
|
||||
namespace CommandLine\Argument;
|
||||
|
||||
/**
|
||||
* Résultat du parsage d'un argument
|
||||
*
|
||||
* @package CommandLine\Argument
|
||||
*/
|
||||
class ParseResult {
|
||||
/**
|
||||
* @var int Le nombre d'argument consumé.
|
||||
*/
|
||||
protected $_consume;
|
||||
/**
|
||||
* @var mixed La valeur.
|
||||
*/
|
||||
protected $_value;
|
||||
|
||||
/**
|
||||
* ArgumentParseResult constructor.
|
||||
*
|
||||
* @param mixed $value La valeur.
|
||||
* @param int $consume Le nombre d'argument consumé.
|
||||
*/
|
||||
public function __construct ($value, $consume = 1) {
|
||||
$this->setValue($value);
|
||||
$this->setConsume($consume);
|
||||
}
|
||||
|
||||
/**
|
||||
* Le nombre d'argument consumé.
|
||||
*
|
||||
* @return int Le nombre d'argument consumé.
|
||||
*
|
||||
* @see $_consume
|
||||
*/
|
||||
public function getConsume() {
|
||||
return $this->_consume;
|
||||
}
|
||||
/**
|
||||
* Définit le nombre d'argument consumé.
|
||||
*
|
||||
* @param $consume int Le nombre d'argument consumé.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see $_consume
|
||||
*/
|
||||
public function setConsume($consume) {
|
||||
$this->_consume = $consume;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* La valeur.
|
||||
*
|
||||
* @return mixed La valeur.
|
||||
*
|
||||
* @see $_value
|
||||
*/
|
||||
public function getValue() {
|
||||
return $this->_value;
|
||||
}
|
||||
/**
|
||||
* Définit les valeurs définies.
|
||||
*
|
||||
* @param mixed $value La valeur.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see $_value
|
||||
*/
|
||||
public function setValue($value) {
|
||||
$this->_value = $value;
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -1,32 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\Parser\BooleanParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
use Fidit\v3\Exception\InvalidArgument;
|
||||
|
||||
/**
|
||||
* Parseur vers un booléen
|
||||
*
|
||||
* Les valeurs suivantes sont acceptées :
|
||||
* - true , 1, oui, vrai
|
||||
* - false, 0, non, faux
|
||||
*
|
||||
* @package CommandLine\Argument\Parser
|
||||
*/
|
||||
class BooleanParser implements IValueParser {
|
||||
public function parseValue ($arg) {
|
||||
if(!in_array($arg, array(
|
||||
'true' , '1', 'oui', 'vrai',
|
||||
'false', '0', 'non', 'faux',
|
||||
)))
|
||||
throw new InvalidArgument($arg, 'La valeur n\'est pas un booléen valide');
|
||||
|
||||
return in_array($arg, array('true' , '1', 'oui', 'vrai'));
|
||||
}
|
||||
|
||||
public function getValueDescription () {
|
||||
return 'boolean';
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\Parser\BooleanParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
use http\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Parseur vers un booléen
|
||||
*
|
||||
* Les valeurs suivantes sont acceptées :
|
||||
* - true , 1, oui, vrai
|
||||
* - false, 0, non, faux
|
||||
*
|
||||
* @package CommandLine\Argument\Parser
|
||||
*/
|
||||
class BooleanParser implements IValueParser {
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function parseValue ($arg) {
|
||||
if(!in_array($arg, array(
|
||||
'true' , '1', 'oui', 'vrai',
|
||||
'false', '0', 'non', 'faux',
|
||||
)))
|
||||
throw new InvalidArgumentException('La valeur n\'est pas un booléen valide : '.$arg);
|
||||
|
||||
return in_array($arg, array('true' , '1', 'oui', 'vrai'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getValueDescription () {
|
||||
return 'boolean';
|
||||
}
|
||||
}
|
@ -1,138 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\Parser\DecimalParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
use Fidit\v3\Exception\InvalidArgument;
|
||||
|
||||
/**
|
||||
* Parseur vers un réel
|
||||
*
|
||||
* @property double|null $valueMin {@see $_valueMin $_valueMin}
|
||||
* @property double|null $valueMax {@see $_valueMax $_valueMax}
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* @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->_isDecimal($arg))
|
||||
throw new InvalidArgument($arg, 'La valeur n\'est pas un réel 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 _isDecimal($val) {
|
||||
if(!is_numeric($val))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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
|
||||
*
|
||||
* @throws InvalidArgument Si la valeur minimum n'est ni null, ni un entier
|
||||
*/
|
||||
public function setValueMin($valueMin = null) {
|
||||
if(!is_null($valueMin) && !$this->_isDecimal($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 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
|
||||
*
|
||||
* @throws InvalidArgument Si la valeur maximum n'est ni null, ni un entier
|
||||
*/
|
||||
public function setValueMax($valueMax = null) {
|
||||
if(!is_null($valueMax) && !$this->_isDecimal($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());
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\Parser\DecimalParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* 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 InvalidArgumentException('La valeur n\'est pas un réel valide : ' . $arg);
|
||||
|
||||
$int = (int)$arg;
|
||||
|
||||
if ($this->hasValueMin()) {
|
||||
if ($int < $this->getValueMin())
|
||||
throw new InvalidArgumentException('La valeur est inférieure au minimum (' . $this->getValueMin().') : ' . $arg);
|
||||
}
|
||||
if ($this->hasValueMax()) {
|
||||
if ($int > $this->getValueMax())
|
||||
throw new InvalidArgumentException($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());
|
||||
}
|
||||
}
|
@ -1,75 +1,74 @@
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\Parser\EnumParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
use Fidit\v3\Exception\InvalidArgument;
|
||||
|
||||
/**
|
||||
* Parseur vers une liste de valeurs possible (chaine de caractère).
|
||||
*
|
||||
* @property string[] $values {@see $_values $_values}
|
||||
*
|
||||
* @package CommandLine\Argument\Parser
|
||||
*/
|
||||
class EnumParser implements IValueParser {
|
||||
/**
|
||||
* @var string[] Liste des valeurs autorisées
|
||||
*/
|
||||
protected $_values;
|
||||
|
||||
/**
|
||||
* Crée un nouveau parseur.
|
||||
*
|
||||
* @param string[] $values La liste des valeurs autorisées.
|
||||
*
|
||||
* @throws InvalidArgument Si l'un des paramètres n'est pas correct
|
||||
*/
|
||||
public function __construct ($values) {
|
||||
$this->setValues($values);
|
||||
}
|
||||
|
||||
public function parseValue ($arg) {
|
||||
if(!in_array($arg, $this->_values))
|
||||
throw new InvalidArgument($arg, 'La valeur ne fait partie de liste des valeurs autorisées : '.implode(', ', $this->getValues()));
|
||||
|
||||
return $arg;
|
||||
}
|
||||
|
||||
public function getValueDescription () {
|
||||
return 'enum('.implode(',', $this->getValues()).')';
|
||||
}
|
||||
|
||||
/**
|
||||
* La liste des valeurs autoriséees.
|
||||
*
|
||||
* @return string[] La liste des valeurs
|
||||
*
|
||||
* @see $_values
|
||||
*/
|
||||
public function getValues() {
|
||||
return $this->_values;
|
||||
}
|
||||
/**
|
||||
* Définit la liste des valeurs autorisées.
|
||||
*
|
||||
* @param string[] $values La liste des valeurs
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws InvalidArgument Si la liste n'est pas un tableau ou si celui-ci est vide
|
||||
*
|
||||
* @see $_values
|
||||
*/
|
||||
public function setValues($values) {
|
||||
if(!is_array($values))
|
||||
throw new InvalidArgument($values, 'La liste de valeurs n\'est pas un tableau');
|
||||
|
||||
if(count($values) == 0)
|
||||
throw new InvalidArgument($values, 'La liste de valeurs doit avoir au moins un élément');
|
||||
|
||||
$this->_values = $values;
|
||||
return $this;
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\Parser\EnumParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Parseur vers une liste de valeurs possible (chaine de caractère).
|
||||
*
|
||||
* @package CommandLine\Argument\Parser
|
||||
*/
|
||||
class EnumParser implements IValueParser {
|
||||
/**
|
||||
* @var string[] Liste des valeurs autorisées
|
||||
*/
|
||||
protected $_values;
|
||||
|
||||
/**
|
||||
* Crée un nouveau parseur
|
||||
*
|
||||
* @param string[] $values La liste des valeurs autorisées
|
||||
*/
|
||||
public function __construct ($values) {
|
||||
$this->setValues($values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function parseValue ($arg) {
|
||||
if(!in_array($arg, $this->_values))
|
||||
throw new InvalidArgumentException('La valeur ne fait partie de liste des valeurs autorisées (' . implode(', ', $this->getValues()) . ') : ' . $arg);
|
||||
|
||||
return $arg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getValueDescription () {
|
||||
return 'enum('.implode(',', $this->getValues()).')';
|
||||
}
|
||||
|
||||
/**
|
||||
* La liste des valeurs autoriséees.
|
||||
*
|
||||
* @return string[] La liste des valeurs
|
||||
*
|
||||
* @see $_values
|
||||
*/
|
||||
public function getValues() {
|
||||
return $this->_values;
|
||||
}
|
||||
/**
|
||||
* Définit la liste des valeurs autorisées.
|
||||
*
|
||||
* @param string[] $values La liste des valeurs
|
||||
*
|
||||
* @return $this
|
||||
* @see $_values
|
||||
*/
|
||||
public function setValues($values) {
|
||||
if(!is_array($values))
|
||||
throw new InvalidArgumentException('La liste de valeurs n\'est pas un tableau');
|
||||
|
||||
if(count($values) == 0)
|
||||
throw new InvalidArgumentException('La liste de valeurs doit avoir au moins un élément');
|
||||
|
||||
$this->_values = $values;
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -1,32 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* Déclare l'interface CommandLine\Argument\Parser\IValueParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
use Fidit\v3\Exception\InvalidArgument;
|
||||
|
||||
/**
|
||||
* Interface pour les parseurs
|
||||
*
|
||||
* @package CommandLine\Argument\Parser
|
||||
*/
|
||||
interface IValueParser {
|
||||
/**
|
||||
* Vérifie et transforme une valeur.
|
||||
*
|
||||
* @param string $val La valeur à transformer.
|
||||
*
|
||||
* @return mixed La valeur transformée.
|
||||
*
|
||||
* @throws InvalidArgument Si la valeur n'est pas valide.
|
||||
*/
|
||||
public function parseValue($val);
|
||||
|
||||
/**
|
||||
* La description des contraintes de la valeur.
|
||||
*
|
||||
* @return string La description.
|
||||
*/
|
||||
public function getValueDescription();
|
||||
<?php
|
||||
/**
|
||||
* Déclare l'interface CommandLine\Argument\Parser\IValueParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
/**
|
||||
* Interface pour les parseurs
|
||||
*
|
||||
* @package CommandLine\Argument\Parser
|
||||
*/
|
||||
interface IValueParser {
|
||||
/**
|
||||
* Vérifie et transforme une valeur.
|
||||
*
|
||||
* @param string $val La valeur à transformer.
|
||||
*
|
||||
* @return mixed La valeur transformée.
|
||||
*/
|
||||
public function parseValue($val);
|
||||
|
||||
/**
|
||||
* La description des contraintes de la valeur.
|
||||
*
|
||||
* @return string La description.
|
||||
*/
|
||||
public function getValueDescription();
|
||||
}
|
@ -1,141 +1,141 @@
|
||||
<?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());
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\Parser\IntegerParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* 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 $_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
|
||||
*/
|
||||
public function __construct ($valueMin = null, $valueMax = null) {
|
||||
$this->setValueMin($valueMin);
|
||||
$this->setValueMax($valueMax);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function parseValue ($arg) {
|
||||
if(!$this->_isInt($arg))
|
||||
throw new InvalidArgumentException('La valeur n\'est pas un entier valide : ' . $arg);
|
||||
|
||||
$int = (int)$arg;
|
||||
|
||||
if($this->hasValueMin()) {
|
||||
if($int < $this->getValueMin())
|
||||
throw new InvalidArgumentException('La valeur est inférieure au minimum (' . $this->getValueMin() . ') :' . $int);
|
||||
}
|
||||
if($this->hasValueMax()) {
|
||||
if($int > $this->getValueMax())
|
||||
throw new InvalidArgumentException('La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $int);
|
||||
}
|
||||
|
||||
return $int;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function _isInt($val) {
|
||||
if(!is_numeric($val))
|
||||
return false;
|
||||
|
||||
if(floor($val) != $val)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
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
|
||||
*/
|
||||
public function setValueMin($valueMin = null) {
|
||||
if(!is_null($valueMin) && !$this->_isInt($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 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
|
||||
*/
|
||||
public function setValueMax($valueMax = null) {
|
||||
if(!is_null($valueMax) && !$this->_isInt($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());
|
||||
}
|
||||
}
|
@ -1,65 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\Parser\RegexParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
use Fidit\v3\Exception\InvalidArgument;
|
||||
|
||||
/**
|
||||
* Parseur vers une chaine de caractère correspondant à un motif
|
||||
*
|
||||
* Renvoie la liste des groupes capturants
|
||||
*
|
||||
* @property string $regex {@see $_regex $_regex}
|
||||
*
|
||||
* @package CommandLine\Argument\Parser
|
||||
*/
|
||||
class RegexParser implements IValueParser {
|
||||
/**
|
||||
* @var string La regex à respecter
|
||||
*/
|
||||
protected $_regex = '';
|
||||
|
||||
/**
|
||||
* Crée un nouveau parseur
|
||||
*
|
||||
* @param string $regex Le motif à respecter
|
||||
*/
|
||||
public function __construct ($regex) {
|
||||
$this->setRegex($regex);
|
||||
}
|
||||
|
||||
public function parseValue ($arg) {
|
||||
if(!preg_match($this->regex, $arg, $matches))
|
||||
throw new InvalidArgument($arg, 'La valeur ne correspond pas au motif attendu');
|
||||
|
||||
return $matches;
|
||||
}
|
||||
|
||||
public function getValueDescription () {
|
||||
return $this->getRegex();
|
||||
}
|
||||
|
||||
/**
|
||||
* Le motif à respecter
|
||||
*
|
||||
* @return string Le motif
|
||||
*
|
||||
* @see $_regex
|
||||
*/
|
||||
public function getRegex() {
|
||||
return $this->_regex;
|
||||
}
|
||||
/**
|
||||
* Modifie le motif à respecter
|
||||
*
|
||||
* @param string $regex Le nouveau motif
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setRegex($regex = null) {
|
||||
$this->_regex = $regex;
|
||||
return $this;
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\Parser\RegexParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Parseur vers une chaine de caractère correspondant à un motif
|
||||
*
|
||||
* Renvoie la liste des groupes capturants
|
||||
*
|
||||
* @package CommandLine\Argument\Parser
|
||||
*/
|
||||
class RegexParser implements IValueParser {
|
||||
/**
|
||||
* @var string La regex à respecter
|
||||
*/
|
||||
protected $_regex = '';
|
||||
|
||||
/**
|
||||
* Crée un nouveau parseur
|
||||
*
|
||||
* @param string $regex Le motif à respecter
|
||||
*/
|
||||
public function __construct ($regex) {
|
||||
$this->setRegex($regex);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function parseValue ($arg) {
|
||||
if(!preg_match($this->_regex, $arg, $matches))
|
||||
throw new InvalidArgumentException('La valeur ne correspond pas au motif attendu : ' . $arg);
|
||||
|
||||
return $matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getValueDescription () {
|
||||
return $this->getRegex();
|
||||
}
|
||||
|
||||
/**
|
||||
* Le motif à respecter
|
||||
*
|
||||
* @return string Le motif
|
||||
*
|
||||
* @see $_regex
|
||||
*/
|
||||
public function getRegex() {
|
||||
return $this->_regex;
|
||||
}
|
||||
/**
|
||||
* Modifie le motif à respecter
|
||||
*
|
||||
* @param string $regex Le nouveau motif
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setRegex($regex = null) {
|
||||
$this->_regex = $regex;
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -1,20 +1,26 @@
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\Parser\StringParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
/**
|
||||
* Parseur vers une chaine de caractères
|
||||
*
|
||||
* @package CommandLine\Argument\Parser
|
||||
*/
|
||||
class StringParser implements IValueParser {
|
||||
public function parseValue ($arg) {
|
||||
return $arg;
|
||||
}
|
||||
|
||||
public function getValueDescription () {
|
||||
return 'string';
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Déclaration de la classe CommandLine\Argument\Parser\StringParser
|
||||
*/
|
||||
namespace CommandLine\Argument\Parser;
|
||||
|
||||
/**
|
||||
* Parseur vers une chaine de caractères
|
||||
*
|
||||
* @package CommandLine\Argument\Parser
|
||||
*/
|
||||
class StringParser implements IValueParser {
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function parseValue ($arg) {
|
||||
return $arg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getValueDescription () {
|
||||
return 'string';
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue