diff --git a/Argument/IArgument.class.php b/Argument/IArgument.class.php index 4c4c0ed..4c5f164 100644 --- a/Argument/IArgument.class.php +++ b/Argument/IArgument.class.php @@ -1,51 +1,50 @@ -setDefault($default); - } - - public function parse ($args) { - if($this->_parseTag($args[0])) - return new ParseResult(true, 1); - - return null; - } - - /** - * Définit le valeur par défaut - * - * @param boolean $default La valeur par défaut. - * - * @return $this - * - * @throws InvalidArgument Si la valeur n'est pas un booléen. - */ - public function setDefault ($default = null) { - if(is_null($default) || !is_bool($default)) - throw new InvalidArgument($default, 'La valeur par défaut DOIT être un booléen'); - - parent::setDefault($default); - return $this; - } +setDefault($default); + } + + /** + * @inheritDoc + */ + public function parse ($args) { + if($this->_parseTag($args[0])) + return new ParseResult(true, 1); + + return null; + } + + /** + * Définit le valeur par défaut + * + * @param boolean $default La valeur par défaut. + * + * @return $this + */ + public function setDefault ($default = null) { + if(is_null($default) || !is_bool($default)) + throw new InvalidArgumentException('La valeur par défaut DOIT être un booléen'); + + parent::setDefault($default); + return $this; + } } \ No newline at end of file diff --git a/Argument/Option/IArgumentOptionSecondary.class.php b/Argument/Option/IArgumentOptionSecondary.class.php index 6f29b76..f2ea906 100644 --- a/Argument/Option/IArgumentOptionSecondary.class.php +++ b/Argument/Option/IArgumentOptionSecondary.class.php @@ -1,23 +1,19 @@ -setTagLong($tagLong); - $this->setTagShort($tagShort); - } - - /** - * Est-ce la valeur correspond au tag court ou long ? - * - * Utilisé par les classes enfants pour savoir si le tag correspond. - * - * @param string $arg La valeur à examiner. - * - * @return boolean True si le tag correspond, sinon False. - */ - protected function _parseTag($arg) { - if($this->hasTagShort()) { - if ($arg == '-'.$this->getTagShort()) - return true; - } - - return $arg == '--'.$this->getTagLong(); - } - - public function getTagShort() { - return $this->_tagShort; - } - /** - * Définit le tag court. - * - * @param string|null $tagShort Le tage court. - * - * @return $this - * - * @see $_tagShort - */ - public function setTagShort($tagShort = null) { - $this->_tagShort = $tagShort; - return $this; - } - /** - * Est-ce que l'argument existe en forme courte ? - * - * @return boolean True si existe en forme courte, sinon False. - */ - public function hasTagShort() { - return !is_null($this->_tagShort); - } - - public function getTagLong() { - return $this->_tagLong; - } - /** - * Définit le tag long. - * - * Si non fourni, est déduit du {@see $_name nom de l'argument} : - * - Les "_" sont remplacés par "-" - * - Les lettres majuscules sont remplacées par "-" suivit de la lettre en minuscule - * - * @param string|null $tagLong Le tag long. - * - * @return $this - * - * @see $_tagLong - */ - public function setTagLong($tagLong = null) { - $this->_tagLong = $tagLong; - - if(is_null($this->_tagLong)) { - $this->_tagLong = preg_replace_callback( - '/[A-Z_]/', - function ($matches) { - if($matches[0] == '_') - return '-'; - else - return '-'.strtolower($matches[0]); - }, - $this->getName() - ); - } - - return $this; - } - - public function allowMultiple() { - return $this->_multiple; - } - /** - * Définit si l'argument est autorisé plusieurs fois ou non. - * - * @param boolean $multiple Argument autorisé plusieurs fois ? - * - * @return $this - * - * @throws InvalidArgument Si la valeur n'est pas un booléen - * - * @see $_optional - */ - public function setMultiple($multiple) { - if (!is_bool($multiple)) - throw new InvalidArgument($multiple, 'La valeur n\'est pas un booléen'); - - $this->_multiple = $multiple; - return $this; - } - - public function isStoppingParse() { - return $this->_isStoppingParse; - } - /** - * Définit si l'argument met fin au parsage ou non. - * - * @param boolean $stoppingParse Met fin au parsage ? - * - * @return $this - * - * @throws InvalidArgument Si la valeur n'est pas un booléen - * - * @see $_optional - */ - public function setStoppingParse($stoppingParse) { - if (!is_bool($stoppingParse)) - throw new InvalidArgument($stoppingParse, 'La valeur n\'est pas un booléen'); - - $this->_isStoppingParse = $stoppingParse; - return $this; - } +setTagLong($tagLong); + $this->setTagShort($tagShort); + } + + /** + * Est-ce la valeur correspond au tag court ou long ? + * + * Utilisé par les classes enfants pour savoir si le tag correspond. + * + * @param string $arg La valeur à examiner. + * + * @return boolean True si le tag correspond, sinon False. + */ + protected function _parseTag($arg) { + if($this->hasTagShort()) { + if ($arg == '-'.$this->getTagShort()) + return true; + } + + return $arg == '--'.$this->getTagLong(); + } + + public function getTagShort() { + return $this->_tagShort; + } + /** + * Définit le tag court. + * + * @param string|null $tagShort Le tage court. + * + * @return $this + * + * @see $_tagShort + */ + public function setTagShort($tagShort = null) { + $this->_tagShort = $tagShort; + return $this; + } + /** + * Est-ce que l'argument existe en forme courte ? + * + * @return boolean True si existe en forme courte, sinon False. + */ + public function hasTagShort() { + return !is_null($this->_tagShort); + } + + public function getTagLong() { + return $this->_tagLong; + } + /** + * Définit le tag long. + * + * Si non fourni, est déduit du {@see $_name nom de l'argument} : + * - Les "_" sont remplacés par "-" + * - Les lettres majuscules sont remplacées par "-" suivit de la lettre en minuscule + * + * @param string|null $tagLong Le tag long. + * + * @return $this + * + * @see $_tagLong + */ + public function setTagLong($tagLong = null) { + $this->_tagLong = $tagLong; + + if(is_null($this->_tagLong)) { + $this->_tagLong = preg_replace_callback( + '/[A-Z_]/', + function ($matches) { + if($matches[0] == '_') + return '-'; + else + return '-'.strtolower($matches[0]); + }, + $this->getName() + ); + } + + return $this; + } + + public function allowMultiple() { + return $this->_multiple; + } + /** + * Définit si l'argument est autorisé plusieurs fois ou non. + * + * @param boolean $multiple Argument autorisé plusieurs fois ? + * + * @return $this + * @see $_optional + */ + public function setMultiple($multiple) { + if (!is_bool($multiple)) + throw new InvalidArgumentException('La multiplicité n\'est pas un booléen'); + + $this->_multiple = $multiple; + return $this; + } + + public function isStoppingParse() { + return $this->_isStoppingParse; + } + /** + * Définit si l'argument met fin au parsage ou non. + * + * @param boolean $stoppingParse Met fin au parsage ? + * + * @return $this + * @see $_optional + */ + public function setStoppingParse($stoppingParse) { + if (!is_bool($stoppingParse)) + throw new InvalidArgumentException('La stoppabilité n\'est pas un booléen'); + + $this->_isStoppingParse = $stoppingParse; + return $this; + } } \ No newline at end of file diff --git a/Argument/Option/Value.class.php b/Argument/Option/Value.class.php index d2defb2..85f4d7a 100644 --- a/Argument/Option/Value.class.php +++ b/Argument/Option/Value.class.php @@ -1,84 +1,86 @@ -setValueParser($valueParser); - } - - public function parse ($args) { - try { - if(!$this->_parseTag($args[0])) - return null; - - if(count($args) < 2 || is_null($args[1])) - throw new IncorrectParse($this,null, 'Seconde valeur de l\'argument manquante'); - - return new ParseResult($this->_valueParser->parseValue($args[1]), 2); - } - catch (InvalidArgument $e) { - throw IncorrectParse::createFromInvalidArgument($this, $e); - } - } - - public function getValueDescription () { - return $this->_valueParser->getValueDescription(); - } - - /** - * Le parseur de valeur. - * - * @return IValueParser Le parseur. - * - * @see $_valueParser - */ - public function getValueParser() { - return $this->_valueParser; - } - /** - * Définit le parseur de valeur - * - * @param IValueParser $valueParser Le parseur - * - * @return $this - * - * @see $_valueParser - */ - public function setValueParser(IValueParser $valueParser) { - $this->_valueParser = $valueParser; - return $this; - } +setValueParser($valueParser); + } + + /** + * @inheritDoc + */ + public function parse ($args) { + try { + if(!$this->_parseTag($args[0])) + return null; + + if(count($args) < 2 || is_null($args[1])) + throw new IncorrectParse('La seconde valeur de l\'argument manquante'); + + return new ParseResult($this->_valueParser->parseValue($args[1]), 2); + } + catch (RuntimeException $e) { + throw new IncorrectParse('Échec du parsage de la valeur "' . $args[0] . '"', 0, $e); + } + } + + /** + * @inheritDoc + */ + public function getValueDescription () { + return $this->_valueParser->getValueDescription(); + } + + /** + * Le parseur de valeur. + * + * @return IValueParser Le parseur. + * + * @see $_valueParser + */ + public function getValueParser() { + return $this->_valueParser; + } + /** + * Définit le parseur de valeur + * + * @param IValueParser $valueParser Le parseur + * + * @return $this + * + * @see $_valueParser + */ + public function setValueParser(IValueParser $valueParser) { + $this->_valueParser = $valueParser; + return $this; + } } \ No newline at end of file diff --git a/Argument/ParseResult.class.php b/Argument/ParseResult.class.php index 456ea7d..b2ead83 100644 --- a/Argument/ParseResult.class.php +++ b/Argument/ParseResult.class.php @@ -73,7 +73,7 @@ class ParseResult { * * @see $_value */ - public function setValue($value) { + public function setValue($value) { $this->_value = $value; return $this; } diff --git a/Argument/Parser/BooleanParser.class.php b/Argument/Parser/BooleanParser.class.php index 4b95bc4..fdbb477 100644 --- a/Argument/Parser/BooleanParser.class.php +++ b/Argument/Parser/BooleanParser.class.php @@ -4,7 +4,7 @@ */ namespace CommandLine\Argument\Parser; -use http\Exception\InvalidArgumentException; +use RangeException; /** * Parseur vers un booléen @@ -24,7 +24,7 @@ class BooleanParser implements IValueParser { 'true' , '1', 'oui', 'vrai', 'false', '0', 'non', 'faux', ))) - throw new InvalidArgumentException('La valeur n\'est pas un booléen valide : '.$arg); + throw new RangeException('La valeur n\'est pas un booléen valide : '.$arg); return in_array($arg, array('true' , '1', 'oui', 'vrai')); } diff --git a/Argument/Parser/DecimalParser.class.php b/Argument/Parser/DecimalParser.class.php index a36d128..0297137 100644 --- a/Argument/Parser/DecimalParser.class.php +++ b/Argument/Parser/DecimalParser.class.php @@ -5,6 +5,7 @@ namespace CommandLine\Argument\Parser; use InvalidArgumentException; +use RangeException; /** * Parseur vers un réel @@ -37,17 +38,17 @@ class DecimalParser implements IValueParser { */ public function parseValue ($arg) { if (!$this->_isDecimal($arg)) - throw new InvalidArgumentException('La valeur n\'est pas un réel valide : ' . $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 InvalidArgumentException('La valeur est inférieure au minimum (' . $this->getValueMin().') : ' . $arg); + throw new RangeException('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); + throw new RangeException($int, 'La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $arg); } return $int; diff --git a/Argument/Parser/EnumParser.class.php b/Argument/Parser/EnumParser.class.php index f4d402e..e69c3aa 100644 --- a/Argument/Parser/EnumParser.class.php +++ b/Argument/Parser/EnumParser.class.php @@ -5,6 +5,7 @@ namespace CommandLine\Argument\Parser; use InvalidArgumentException; +use RangeException; /** * Parseur vers une liste de valeurs possible (chaine de caractère). @@ -31,7 +32,7 @@ class EnumParser implements IValueParser { */ 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); + throw new RangeException('La valeur ne fait partie de liste des valeurs autorisées (' . implode(', ', $this->getValues()) . ') : ' . $arg); return $arg; } diff --git a/Argument/Parser/IntegerParser.class.php b/Argument/Parser/IntegerParser.class.php index f7423d5..9633aa1 100644 --- a/Argument/Parser/IntegerParser.class.php +++ b/Argument/Parser/IntegerParser.class.php @@ -5,6 +5,7 @@ namespace CommandLine\Argument\Parser; use InvalidArgumentException; +use RangeException; /** * Parseur vers un entier @@ -37,17 +38,17 @@ class IntegerParser implements IValueParser { */ public function parseValue ($arg) { if(!$this->_isInt($arg)) - throw new InvalidArgumentException('La valeur n\'est pas un entier valide : ' . $arg); + throw new RangeException('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); + throw new RangeException('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); + throw new RangeException('La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $int); } return $int; diff --git a/Argument/Parser/RegexParser.class.php b/Argument/Parser/RegexParser.class.php index 3cd3a7e..f1ae888 100644 --- a/Argument/Parser/RegexParser.class.php +++ b/Argument/Parser/RegexParser.class.php @@ -4,7 +4,7 @@ */ namespace CommandLine\Argument\Parser; -use InvalidArgumentException; +use RangeException; /** * Parseur vers une chaine de caractère correspondant à un motif @@ -33,7 +33,7 @@ class RegexParser implements IValueParser { */ public function parseValue ($arg) { if(!preg_match($this->_regex, $arg, $matches)) - throw new InvalidArgumentException('La valeur ne correspond pas au motif attendu : ' . $arg); + throw new RangeException('La valeur ne correspond pas au motif attendu : ' . $arg); return $matches; } diff --git a/Argument/Value/FixedValue.class.php b/Argument/Value/FixedValue.class.php index 9077b94..a875481 100644 --- a/Argument/Value/FixedValue.class.php +++ b/Argument/Value/FixedValue.class.php @@ -1,74 +1,74 @@ -setVarName($varName); - $this->setValue($value); - } - - public function parse ($args) { - if($args[0] == $this->getValue()) - return new ParseResult($this->getValue(), 1); - - return null; - } - - public function getValueDescription () { - return '"'.$this->getValue().'"'; - } - - /** - * La valeur. - * - * @return string La valeur. - * - * @see $_value - */ - public function getValue() { - return $this->_value; - } - /** - * Définit la valeur - * - * @param string $value La valeur - * - * @return $this - * - * @see $_value - */ - public function setValue($value) { - $this->_value = $value; - return $this; - } +setVarName($varName); + $this->setValue($value); + } + + /** + * @inheritDoc + */ + public function parse ($args) { + if($args[0] == $this->getValue()) + return new ParseResult($this->getValue(), 1); + + return null; + } + + /** + * @inheritDoc + */ + public function getValueDescription () { + return '"'.$this->getValue().'"'; + } + + /** + * La valeur. + * + * @return string La valeur. + * + * @see $_value + */ + public function getValue() { + return $this->_value; + } + /** + * Définit la valeur + * + * @param string $value La valeur + * + * @return $this + * @see $_value + */ + public function setValue($value) { + $this->_value = $value; + return $this; + } } \ No newline at end of file diff --git a/Argument/Value/Value.class.php b/Argument/Value/Value.class.php index faefb94..422cb26 100644 --- a/Argument/Value/Value.class.php +++ b/Argument/Value/Value.class.php @@ -1,76 +1,80 @@ -setValueParser($valueParser); - } - - public function parse ($args) { - try { - return new ParseResult($this->_valueParser->parseValue($args[0]), 1); - } - catch(InvalidArgument $e) { - throw IncorrectParse::createFromInvalidArgument($this, $e); - } - } - - public function getValueDescription () { - return $this->_valueParser->getValueDescription(); - } - - /** - * Le parseur de valeur. - * - * @return IValueParser Le parseur. - * - * @see $_valueParser - */ - public function getValueParser() { - return $this->_valueParser; - } - /** - * Définit le parseur de valeur - * - * @param IValueParser $valueParser Le parseur - * - * @return $this - * - * @see $_valueParser - */ - public function setValueParser(IValueParser $valueParser) { - $this->_valueParser = $valueParser; - return $this; - } +setValueParser($valueParser); + } + + /** + * @inheritDoc + */ + public function parse ($args) { + try { + return new ParseResult($this->_valueParser->parseValue($args[0]), 1); + } + catch (RangeException $e) { + throw new IncorrectParse('Échec du parsage de la valeur "' . $args[0] . '"', 0, $e); + } + } + + /** + * @inheritDoc + */ + public function getValueDescription () { + return $this->_valueParser->getValueDescription(); + } + + /** + * Le parseur de valeur. + * + * @return IValueParser Le parseur. + * + * @see $_valueParser + */ + public function getValueParser() { + return $this->_valueParser; + } + /** + * Définit le parseur de valeur + * + * @param IValueParser $valueParser Le parseur + * + * @return $this + * + * @see $_valueParser + */ + public function setValueParser(IValueParser $valueParser) { + $this->_valueParser = $valueParser; + return $this; + } } \ No newline at end of file diff --git a/Argument/Value/ValueAbstract.class.php b/Argument/Value/ValueAbstract.class.php index 4ecaadf..bca51d7 100644 --- a/Argument/Value/ValueAbstract.class.php +++ b/Argument/Value/ValueAbstract.class.php @@ -1,117 +1,116 @@ -setOptional($optional); - } - - public function getOccurMin() { - return $this->_occurMin; - } - /** - * Définit le nombre minimum d'occurence. - * - * @param int $occurMin Le nombre minimum - * - * @return $this - * - * @throws InvalidArgument Si la valeur n'est pas un entier positif. - * - * @see $_occurMin - */ - public function setOccurMin($occurMin) { - if (!is_numeric($occurMin)) - throw new InvalidArgument($occurMin, 'La valeur n\'est pas un entier'); - - if (floor($occurMin) != $occurMin) - throw new InvalidArgument($occurMin, 'La valeur n\'est pas un entier'); - - $int = (int)$occurMin; - if($int < 0) - throw new InvalidArgument($occurMin, 'La valeur n\'est pas un entier positif'); - - $this->_occurMin = $int; - return $this; - } - /** - * Définit si l'argument est facultatif ou non. - * - * @param bool $optional Argument facultatif ? - * - * @return $this - */ - public function setOptional($optional = false) { - try { - $this->setOccurMin($optional ? 0 : 1); - } - catch(InvalidArgument $e) {} - - return $this; - } - - public function getOccurMax() { - return $this->_occurMax; - } - /** - * Définit le nombre mawimum d'occurence. - * - * @param int|null $occurMax Le nombre maximum - * - * @return $this - * - * @throws InvalidArgument Si la valeur n'est pas Null ou un entier strictement positif. - * - * @see $_occurMax - */ - public function setOccurMax($occurMax) { - if (!is_null($occurMax)) { - if (!is_numeric($occurMax)) - throw new InvalidArgument($occurMax, 'La valeur n\'est pas un entier'); - - if (floor($occurMax) != $occurMax) - throw new InvalidArgument($occurMax, 'La valeur n\'est pas un entier'); - - $occurMax = (int)$occurMax; - if ($occurMax <= 0) - throw new InvalidArgument($occurMax, 'La valeur n\'est pas un entier strictement positif'); - } - - $this->_occurMax = $occurMax; - return $this; - } +setOptional($optional); + } + /** + * Définit si l'argument est facultatif ou non. + * + * @param bool $optional Argument facultatif ? + * + * @return $this + */ + public function setOptional ($optional = false) { + $this->setOccurMin($optional ? 0 : 1); + return $this; + } + + /** + * @inheritDoc + */ + public function getOccurMin () { + return $this->_occurMin; + } + /** + * Définit le nombre minimum d'occurence. + * + * @param int $occurMin Le nombre minimum + * + * @return $this + * @see $_occurMin + */ + public function setOccurMin ($occurMin) { + if (!is_numeric($occurMin)) { + throw new InvalidArgumentException('Le nombre minimum d\'ocurrence n\'est pas un entier'); + } + + if (floor($occurMin) != $occurMin) { + throw new InvalidArgumentException('Le nombre minimum d\'ocurrence n\'est pas un entier'); + } + + $int = (int)$occurMin; + if ($int < 0) { + throw new InvalidArgumentException('Le nombre minimum d\'ocurrence n\'est pas un entier positif'); + } + + $this->_occurMin = $int; + return $this; + } + + /** + * @inheritDoc + */ + public function getOccurMax () { + return $this->_occurMax; + } + /** + * Définit le nombre mawimum d'occurence. + * + * @param int|null $occurMax Le nombre maximum + * + * @return $this + * @see $_occurMax + */ + public function setOccurMax ($occurMax) { + if (!is_null($occurMax)) { + if (!is_numeric($occurMax)) { + throw new InvalidArgumentException('Le nombre maximum d\'ocurrence n\'est pas un entier'); + } + + if (floor($occurMax) != $occurMax) { + throw new InvalidArgumentException('Le nombre maximum d\'ocurrence n\'est pas un entier'); + } + + $occurMax = (int)$occurMax; + if ($occurMax <= 0) { + throw new InvalidArgumentException('Le nombre maximum d\'ocurrence n\'est pas un entier strictement positif'); + } + } + + $this->_occurMax = $occurMax; + return $this; + } } \ No newline at end of file diff --git a/Exception/IncorrectParse.class.php b/Exception/IncorrectParse.class.php index d45be2b..fad5d74 100644 --- a/Exception/IncorrectParse.class.php +++ b/Exception/IncorrectParse.class.php @@ -1,21 +1,21 @@ -