Fixing InvalidArgumentException in parsers

master
Julien Rosset 6 years ago
parent 3d869f684f
commit 33d04e872c

@ -5,20 +5,12 @@
*/ */
namespace CommandLine\Argument; namespace CommandLine\Argument;
use Fidit\v3\AutoProperty;
use Fidit\v3\Exception\InvalidArgument;
/** /**
* Classe abstraite pour n'importe quel type de paramètre * 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 * @package CommandLine\Argument
*/ */
abstract class ArgumentAbstract extends AutoProperty implements IArgument { abstract class ArgumentAbstract implements IArgument {
/** /**
* @var string Le nom de la variable de retour de l'argument. * @var string Le nom de la variable de retour de l'argument.
*/ */
@ -48,6 +40,9 @@ abstract class ArgumentAbstract extends AutoProperty implements IArgument {
$this->setDescription($description); $this->setDescription($description);
} }
/**
* @inheritDoc
*/
public function getVarName() { public function getVarName() {
return $this->_varName; return $this->_varName;
} }
@ -65,6 +60,9 @@ abstract class ArgumentAbstract extends AutoProperty implements IArgument {
return $this; return $this;
} }
/**
* @inheritDoc
*/
public function getName() { public function getName() {
return $this->_name; return $this->_name;
} }
@ -85,6 +83,9 @@ abstract class ArgumentAbstract extends AutoProperty implements IArgument {
return $this; return $this;
} }
/**
* @inheritDoc
*/
public function getDescription() { public function getDescription() {
return $this->_description; return $this->_description;
} }
@ -102,6 +103,9 @@ abstract class ArgumentAbstract extends AutoProperty implements IArgument {
return $this; return $this;
} }
/**
* @inheritDoc
*/
public function getDefault() { public function getDefault() {
return $this->_default; return $this->_default;
} }

@ -4,17 +4,12 @@
*/ */
namespace CommandLine\Argument; namespace CommandLine\Argument;
use Fidit\v3\AutoProperty;
/** /**
* Résultat du parsage d'un argument * Résultat du parsage d'un argument
* *
* @property int $consume {@see $_consume $_consume}
* @property mixed $value {@see $_value $_value }
*
* @package CommandLine\Argument * @package CommandLine\Argument
*/ */
class ParseResult extends AutoProperty { class ParseResult {
/** /**
* @var int Le nombre d'argument consumé. * @var int Le nombre d'argument consumé.
*/ */

@ -4,7 +4,7 @@
*/ */
namespace CommandLine\Argument\Parser; namespace CommandLine\Argument\Parser;
use Fidit\v3\Exception\InvalidArgument; use http\Exception\InvalidArgumentException;
/** /**
* Parseur vers un booléen * Parseur vers un booléen
@ -16,16 +16,22 @@ use Fidit\v3\Exception\InvalidArgument;
* @package CommandLine\Argument\Parser * @package CommandLine\Argument\Parser
*/ */
class BooleanParser implements IValueParser { class BooleanParser implements IValueParser {
/**
* @inheritDoc
*/
public function parseValue ($arg) { public function parseValue ($arg) {
if(!in_array($arg, array( if(!in_array($arg, array(
'true' , '1', 'oui', 'vrai', 'true' , '1', 'oui', 'vrai',
'false', '0', 'non', 'faux', 'false', '0', 'non', 'faux',
))) )))
throw new InvalidArgument($arg, 'La valeur n\'est pas un booléen valide'); throw new InvalidArgumentException('La valeur n\'est pas un booléen valide : '.$arg);
return in_array($arg, array('true' , '1', 'oui', 'vrai')); return in_array($arg, array('true' , '1', 'oui', 'vrai'));
} }
/**
* @inheritDoc
*/
public function getValueDescription () { public function getValueDescription () {
return 'boolean'; return 'boolean';
} }

@ -4,14 +4,11 @@
*/ */
namespace CommandLine\Argument\Parser; namespace CommandLine\Argument\Parser;
use Fidit\v3\Exception\InvalidArgument; use InvalidArgumentException;
/** /**
* Parseur vers un réel * Parseur vers un réel
* *
* @property double|null $valueMin {@see $_valueMin $_valueMin}
* @property double|null $valueMax {@see $_valueMax $_valueMax}
*
* @package CommandLine\Argument\Parser * @package CommandLine\Argument\Parser
*/ */
class DecimalParser implements IValueParser { class DecimalParser implements IValueParser {
@ -29,32 +26,36 @@ class DecimalParser implements IValueParser {
* *
* @param double|null $valueMin La valeur minimum autorisée * @param double|null $valueMin La valeur minimum autorisée
* @param double|null $valueMax La valeur maximum 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) { public function __construct ($valueMin = null, $valueMax = null) {
$this->setValueMin($valueMin); $this->setValueMin($valueMin);
$this->setValueMax($valueMax); $this->setValueMax($valueMax);
} }
/**
* @inheritDoc
*/
public function parseValue ($arg) { public function parseValue ($arg) {
if (!$this->_isDecimal($arg)) if (!$this->_isDecimal($arg))
throw new InvalidArgument($arg, 'La valeur n\'est pas un réel valide'); throw new InvalidArgumentException('La valeur n\'est pas un réel valide : ' . $arg);
$int = (int)$arg; $int = (int)$arg;
if ($this->hasValueMin()) { if ($this->hasValueMin()) {
if ($int < $this->getValueMin()) if ($int < $this->getValueMin())
throw new InvalidArgument($int, 'La valeur est inférieure au minimum : ' . $this->getValueMin()); throw new InvalidArgumentException('La valeur est inférieure au minimum (' . $this->getValueMin().') : ' . $arg);
} }
if ($this->hasValueMax()) { if ($this->hasValueMax()) {
if ($int > $this->getValueMax()) if ($int > $this->getValueMax())
throw new InvalidArgument($int, 'La valeur est supérieur au maximum : ' . $this->getValueMax()); throw new InvalidArgumentException($int, 'La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $arg);
} }
return $int; return $int;
} }
/**
* @inheritDoc
*/
protected function _isDecimal($val) { protected function _isDecimal($val) {
if(!is_numeric($val)) if(!is_numeric($val))
return false; return false;
@ -62,6 +63,9 @@ class DecimalParser implements IValueParser {
return true; return true;
} }
/**
* @inheritDoc
*/
public function getValueDescription () { public function getValueDescription () {
return ($this->hasValueMin() ? $this->getValueMin().' <= ' : '').'decimal'.($this->hasValueMax() ? ' <= '.$this->getValueMax() : ''); return ($this->hasValueMin() ? $this->getValueMin().' <= ' : '').'decimal'.($this->hasValueMax() ? ' <= '.$this->getValueMax() : '');
} }
@ -82,12 +86,10 @@ class DecimalParser implements IValueParser {
* @param double|null $valueMin La valeur minimum. * @param double|null $valueMin La valeur minimum.
* *
* @return $this * @return $this
*
* @throws InvalidArgument Si la valeur minimum n'est ni null, ni un entier
*/ */
public function setValueMin($valueMin = null) { public function setValueMin($valueMin = null) {
if(!is_null($valueMin) && !$this->_isDecimal($valueMin)) if(!is_null($valueMin) && !$this->_isDecimal($valueMin))
throw new InvalidArgument($valueMin, 'La valeur n\'est pas un entier ou null'); throw new InvalidArgumentException('La valeur n\'est pas un entier ou null' . $valueMin);
$this->_valueMin = (int)$valueMin; $this->_valueMin = (int)$valueMin;
return $this; return $this;
@ -117,12 +119,10 @@ class DecimalParser implements IValueParser {
* @param double|null $valueMax La valeur maximum. * @param double|null $valueMax La valeur maximum.
* *
* @return $this * @return $this
*
* @throws InvalidArgument Si la valeur maximum n'est ni null, ni un entier
*/ */
public function setValueMax($valueMax = null) { public function setValueMax($valueMax = null) {
if(!is_null($valueMax) && !$this->_isDecimal($valueMax)) if(!is_null($valueMax) && !$this->_isDecimal($valueMax))
throw new InvalidArgument($valueMax, 'La valeur n\'est pas un entier ou null'); throw new InvalidArgumentException('La valeur n\'est pas un entier ou null : ' . $valueMax);
$this->_valueMax = (int)$valueMax; $this->_valueMax = (int)$valueMax;
return $this; return $this;

@ -4,13 +4,11 @@
*/ */
namespace CommandLine\Argument\Parser; namespace CommandLine\Argument\Parser;
use Fidit\v3\Exception\InvalidArgument; use InvalidArgumentException;
/** /**
* Parseur vers une liste de valeurs possible (chaine de caractère). * Parseur vers une liste de valeurs possible (chaine de caractère).
* *
* @property string[] $values {@see $_values $_values}
*
* @package CommandLine\Argument\Parser * @package CommandLine\Argument\Parser
*/ */
class EnumParser implements IValueParser { class EnumParser implements IValueParser {
@ -20,23 +18,27 @@ class EnumParser implements IValueParser {
protected $_values; protected $_values;
/** /**
* Crée un nouveau parseur. * 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 * @param string[] $values La liste des valeurs autorisées
*/ */
public function __construct ($values) { public function __construct ($values) {
$this->setValues($values); $this->setValues($values);
} }
/**
* @inheritDoc
*/
public function parseValue ($arg) { public function parseValue ($arg) {
if(!in_array($arg, $this->_values)) if(!in_array($arg, $this->_values))
throw new InvalidArgument($arg, 'La valeur ne fait partie de liste des valeurs autorisées : '.implode(', ', $this->getValues())); throw new InvalidArgumentException('La valeur ne fait partie de liste des valeurs autorisées (' . implode(', ', $this->getValues()) . ') : ' . $arg);
return $arg; return $arg;
} }
/**
* @inheritDoc
*/
public function getValueDescription () { public function getValueDescription () {
return 'enum('.implode(',', $this->getValues()).')'; return 'enum('.implode(',', $this->getValues()).')';
} }
@ -57,17 +59,14 @@ class EnumParser implements IValueParser {
* @param string[] $values La liste des valeurs * @param string[] $values La liste des valeurs
* *
* @return $this * @return $this
*
* @throws InvalidArgument Si la liste n'est pas un tableau ou si celui-ci est vide
*
* @see $_values * @see $_values
*/ */
public function setValues($values) { public function setValues($values) {
if(!is_array($values)) if(!is_array($values))
throw new InvalidArgument($values, 'La liste de valeurs n\'est pas un tableau'); throw new InvalidArgumentException('La liste de valeurs n\'est pas un tableau');
if(count($values) == 0) if(count($values) == 0)
throw new InvalidArgument($values, 'La liste de valeurs doit avoir au moins un élément'); throw new InvalidArgumentException('La liste de valeurs doit avoir au moins un élément');
$this->_values = $values; $this->_values = $values;
return $this; return $this;

@ -4,8 +4,6 @@
*/ */
namespace CommandLine\Argument\Parser; namespace CommandLine\Argument\Parser;
use Fidit\v3\Exception\InvalidArgument;
/** /**
* Interface pour les parseurs * Interface pour les parseurs
* *
@ -18,8 +16,6 @@ interface IValueParser {
* @param string $val La valeur à transformer. * @param string $val La valeur à transformer.
* *
* @return mixed La valeur transformée. * @return mixed La valeur transformée.
*
* @throws InvalidArgument Si la valeur n'est pas valide.
*/ */
public function parseValue($val); public function parseValue($val);

@ -4,13 +4,10 @@
*/ */
namespace CommandLine\Argument\Parser; namespace CommandLine\Argument\Parser;
use Fidit\v3\Exception\InvalidArgument; use InvalidArgumentException;
/** /**
* Parseur vers un entier. * Parseur vers un entier
*
* @property int|null $valueMin {@see $_valueMin $_valueMin}
* @property int|null $valueMax {@see $_valueMax $_valueMax}
* *
* @package CommandLine\Argument\Parser * @package CommandLine\Argument\Parser
*/ */
@ -29,32 +26,36 @@ class IntegerParser implements IValueParser {
* *
* @param int|null $valueMin La valeur minimum autorisée * @param int|null $valueMin La valeur minimum autorisée
* @param int|null $valueMax La valeur maximum 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) { public function __construct ($valueMin = null, $valueMax = null) {
$this->setValueMin($valueMin); $this->setValueMin($valueMin);
$this->setValueMax($valueMax); $this->setValueMax($valueMax);
} }
/**
* @inheritDoc
*/
public function parseValue ($arg) { public function parseValue ($arg) {
if(!$this->_isInt($arg)) if(!$this->_isInt($arg))
throw new InvalidArgument($arg, 'La valeur n\'est pas un entier valide'); throw new InvalidArgumentException('La valeur n\'est pas un entier valide : ' . $arg);
$int = (int)$arg; $int = (int)$arg;
if($this->hasValueMin()) { if($this->hasValueMin()) {
if($int < $this->getValueMin()) if($int < $this->getValueMin())
throw new InvalidArgument($int, 'La valeur est inférieure au minimum : '.$this->getValueMin()); throw new InvalidArgumentException('La valeur est inférieure au minimum (' . $this->getValueMin() . ') :' . $int);
} }
if($this->hasValueMax()) { if($this->hasValueMax()) {
if($int > $this->getValueMax()) if($int > $this->getValueMax())
throw new InvalidArgument($int, 'La valeur est supérieur au maximum : '.$this->getValueMax()); throw new InvalidArgumentException('La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $int);
} }
return $int; return $int;
} }
/**
* @inheritDoc
*/
protected function _isInt($val) { protected function _isInt($val) {
if(!is_numeric($val)) if(!is_numeric($val))
return false; return false;
@ -65,6 +66,9 @@ class IntegerParser implements IValueParser {
return true; return true;
} }
/**
* @inheritDoc
*/
public function getValueDescription () { public function getValueDescription () {
return ($this->hasValueMin() ? $this->getValueMin().' <= ' : '').'integer'.($this->hasValueMax() ? ' <= '.$this->getValueMax() : ''); return ($this->hasValueMin() ? $this->getValueMin().' <= ' : '').'integer'.($this->hasValueMax() ? ' <= '.$this->getValueMax() : '');
} }
@ -85,12 +89,10 @@ class IntegerParser implements IValueParser {
* @param int|null $valueMin La valeur minimum. * @param int|null $valueMin La valeur minimum.
* *
* @return $this * @return $this
*
* @throws InvalidArgument Si la valeur minimum n'est ni null, ni un entier
*/ */
public function setValueMin($valueMin = null) { public function setValueMin($valueMin = null) {
if(!is_null($valueMin) && !$this->_isInt($valueMin)) if(!is_null($valueMin) && !$this->_isInt($valueMin))
throw new InvalidArgument($valueMin, 'La valeur n\'est pas un entier ou null'); throw new InvalidArgumentException('La valeur n\'est pas un entier ou null : '.$valueMin);
$this->_valueMin = (int)$valueMin; $this->_valueMin = (int)$valueMin;
return $this; return $this;
@ -120,12 +122,10 @@ class IntegerParser implements IValueParser {
* @param int|null $valueMax La valeur maximum. * @param int|null $valueMax La valeur maximum.
* *
* @return $this * @return $this
*
* @throws InvalidArgument Si la valeur maximum n'est ni null, ni un entier
*/ */
public function setValueMax($valueMax = null) { public function setValueMax($valueMax = null) {
if(!is_null($valueMax) && !$this->_isInt($valueMax)) if(!is_null($valueMax) && !$this->_isInt($valueMax))
throw new InvalidArgument($valueMax, 'La valeur n\'est pas un entier ou null'); throw new InvalidArgumentException('La valeur n\'est pas un entier ou null : '.$valueMax);
$this->_valueMax = (int)$valueMax; $this->_valueMax = (int)$valueMax;
return $this; return $this;

@ -4,15 +4,13 @@
*/ */
namespace CommandLine\Argument\Parser; namespace CommandLine\Argument\Parser;
use Fidit\v3\Exception\InvalidArgument; use InvalidArgumentException;
/** /**
* Parseur vers une chaine de caractère correspondant à un motif * Parseur vers une chaine de caractère correspondant à un motif
* *
* Renvoie la liste des groupes capturants * Renvoie la liste des groupes capturants
* *
* @property string $regex {@see $_regex $_regex}
*
* @package CommandLine\Argument\Parser * @package CommandLine\Argument\Parser
*/ */
class RegexParser implements IValueParser { class RegexParser implements IValueParser {
@ -30,13 +28,19 @@ class RegexParser implements IValueParser {
$this->setRegex($regex); $this->setRegex($regex);
} }
/**
* @inheritDoc
*/
public function parseValue ($arg) { public function parseValue ($arg) {
if(!preg_match($this->regex, $arg, $matches)) if(!preg_match($this->_regex, $arg, $matches))
throw new InvalidArgument($arg, 'La valeur ne correspond pas au motif attendu'); throw new InvalidArgumentException('La valeur ne correspond pas au motif attendu : ' . $arg);
return $matches; return $matches;
} }
/**
* @inheritDoc
*/
public function getValueDescription () { public function getValueDescription () {
return $this->getRegex(); return $this->getRegex();
} }

@ -10,10 +10,16 @@ namespace CommandLine\Argument\Parser;
* @package CommandLine\Argument\Parser * @package CommandLine\Argument\Parser
*/ */
class StringParser implements IValueParser { class StringParser implements IValueParser {
/**
* @inheritDoc
*/
public function parseValue ($arg) { public function parseValue ($arg) {
return $arg; return $arg;
} }
/**
* @inheritDoc
*/
public function getValueDescription () { public function getValueDescription () {
return 'string'; return 'string';
} }

Loading…
Cancel
Save