Fixing Exception management

master
Julien Rosset 6 years ago
parent 25974f3c47
commit f950043b9c

@ -1,51 +1,50 @@
<?php <?php
/** /**
* Déclaration de l'interface CommandLine\Argument\IArgument * Déclaration de l'interface CommandLine\Argument\IArgument
*/ */
namespace CommandLine\Argument; namespace CommandLine\Argument;
use CommandLine\Exception\IncorrectParse; use CommandLine\Exception\IncorrectParse;
/** /**
* Interface que tout argument doit implémenter * Interface que tout argument doit implémenter
* *
* @package CommandLine\Argument * @package CommandLine\Argument
*/ */
interface IArgument { interface IArgument {
/** /**
* Le nom de la variable de sortie l'argument * Le nom de la variable de sortie l'argument
* *
* @return string Le nom * @return string Le nom
*/ */
public function getVarName(); public function getVarName();
/** /**
* Le nom de l'argument * Le nom de l'argument
* *
* @return string Le nom * @return string Le nom
*/ */
public function getName(); public function getName();
/** /**
* La valeur par défaut de l'argument. Null si pas de valeur par défaut * La valeur par défaut de l'argument. Null si pas de valeur par défaut
* *
* @return mixed|null La valeur par défaut * @return mixed|null La valeur par défaut
*/ */
public function getDefault(); public function getDefault();
/** /**
* La description de l'argument * La description de l'argument
* *
* @return string|null La description * @return string|null La description
*/ */
public function getDescription(); public function getDescription();
/** /**
* Parse les arguments. * Parse les arguments.
* *
* @param $args array La liste des arguments encore à traiter (par ordre de réception) * @param $args array La liste des arguments encore à traiter (par ordre de réception)
* *
* @return ParseResult|null Le résultat du parsage. Null si rien parsé * @return ParseResult|null Le résultat du parsage. Null si rien parsé
* * @throws IncorrectParse Echec du parsage de l'argument
* @throws IncorrectParse Echec du parsage de l'argument */
*/ public function parse($args);
public function parse($args);
} }

@ -1,60 +1,58 @@
<?php <?php
/** /**
* Déclare la classe CommandLine\Argument\Option\Flag. * Déclare la classe CommandLine\Argument\Option\Flag.
*/ */
namespace CommandLine\Argument\Option; namespace CommandLine\Argument\Option;
use Fidit\v3\Exception\InvalidArgument; use CommandLine\Argument\ParseResult;
use CommandLine\Argument\ArgumentAbstract; use InvalidArgumentException;
use CommandLine\Argument\ParseResult;
/**
/** * Option de type "flag" contenant True ou False
* Option de type "flag" contenant True ou False. *
* * Si fourni, retournera la valeur inverse de {@see getDefault() getDefault}
* Si fourni, retournera la valeur inverse de {@see getDefault() getDefault} *
* * @see FlagReverse
* @see FlagReverse *
* * @package CommandLine\Argument\Option
* @package CommandLine\Argument\Option */
*/ class Flag extends OptionAbstract {
class Flag extends OptionAbstract { /**
/** * Crée un nouvel argument de type option
* Crée un nouvel argument de type option. *
* * @param string $name Le nom
* @param string $name Le nom. * @param boolean $default La valeur par défaut
* @param boolean $default La valeur par défaut. * @param string|null $description La description
* @param string|null $description La description. * @param string|null $tagLong Le tag long
* @param string|null $tagLong Le tag long. * @param string|null $tagShort Le tag court
* @param string|null $tagShort Le tag court. */
* public function __construct ($name, $default, $description, $tagLong = null, $tagShort = null) {
* @throws InvalidArgument Si l'un des paramètres n'est pas correct parent::__construct($name, $description, $tagLong, $tagShort);
*/ $this->setDefault($default);
public function __construct ($name, $default, $description, $tagLong = null, $tagShort = null) { }
parent::__construct($name, $description, $tagLong, $tagShort);
$this->setDefault($default); /**
} * @inheritDoc
*/
public function parse ($args) { public function parse ($args) {
if($this->_parseTag($args[0])) if($this->_parseTag($args[0]))
return new ParseResult(true, 1); return new ParseResult(true, 1);
return null; return null;
} }
/** /**
* Définit le valeur par défaut * Définit le valeur par défaut
* *
* @param boolean $default La valeur par défaut. * @param boolean $default La valeur par défaut.
* *
* @return $this * @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))
public function setDefault ($default = null) { throw new InvalidArgumentException('La valeur par défaut DOIT être un booléen');
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;
parent::setDefault($default); }
return $this;
}
} }

@ -1,23 +1,19 @@
<?php <?php
/** /**
* Déclaration de l'interface CommandLine\Argument\Option\IArgumentSecondary. * Déclaration de l'interface CommandLine\Argument\Option\IArgumentSecondary.
*/ */
namespace CommandLine\Argument\Option; namespace CommandLine\Argument\Option;
use Fidit\v3\Exception\InvalidArgument; /**
* Interface à implémenter si l'argument "option" auto-déclare d'autres arguments
/** *
* Interface à implémenter si l'argument "option" auto-déclare d'autres arguments * @package CommandLine\Argument\Option
* */
* @package CommandLine\Argument\Option interface IArgumentOptionSecondary {
*/ /**
interface IArgumentOptionSecondary { * La liste des arguments auto-déclarés.
/** *
* La liste des arguments auto-déclarés. * @return IArgumentOption[] La liste des arguments
* */
* @return IArgumentOption[] La liste des arguments public function getOthersOptions();
*
* @throws InvalidArgument
*/
public function getOthersOptions();
} }

@ -1,177 +1,164 @@
<?php <?php
/** /**
* Déclaration de la classe CommandLine\Argument\Option\OptionAbstract. * Déclaration de la classe CommandLine\Argument\Option\OptionAbstract.
*/ */
namespace CommandLine\Argument\Option; namespace CommandLine\Argument\Option;
use Fidit\v3\Exception\InvalidArgument; use CommandLine\Argument\ArgumentAbstract;
use CommandLine\Argument\ArgumentAbstract; use InvalidArgumentException;
/** /**
* Classe abstraite pour les arguments de type "option" : --xxx / -x * Classe abstraite pour les arguments de type "option" : --xxx / -x
* *
* NOTE : les arguments de type "option" ne peuvent autoriser plusieurs valeurs ({@see $default $default} = 1). * @package CommandLine\Argument\Option
* */
* @property string|null $tagShort {@see $_tagShort $_tagShort } abstract class OptionAbstract extends ArgumentAbstract implements IArgumentOption {
* @property string|null $tagLong {@see $_tagLong $_tagLong } /**
* @property boolean $multiple {@see $_multiple $_multiple } * @var string|null L'argument court (cas d'un seul "-"). Null si aucun.
* @property boolean $isStoppingParse {@see $_isStoppingParse $_isStoppingParse} */
* protected $_tagShort;
* @package CommandLine\Argument\Option /**
*/ * @var string L'argument long (cas d'un "--").
abstract class OptionAbstract extends ArgumentAbstract implements IArgumentOption { */
/** protected $_tagLong;
* @var string|null L'argument court (cas d'un seul "-"). Null si aucun.
*/ /**
protected $_tagShort; * @var boolean Est-ce que l'argument est autorisé plusieurs fois ?
/** */
* @var string L'argument long (cas d'un "--"). protected $_multiple = false;
*/ /**
protected $_tagLong; * @var boolean Est-ce que l'argument met fin au parsage ?
*/
/** protected $_isStoppingParse = false;
* @var boolean Est-ce que l'argument est autorisé plusieurs fois ?
*/ /**
protected $_multiple = false; * Crée un nouvel argument de type option.
/** *
* @var boolean Est-ce que l'argument met fin au parsage ? * @param string $name Le nom.
*/ * @param string|null $description La description.
protected $_isStoppingParse = false; * @param string|null $tagLong Le tag long.
* @param string|null $tagShort Le tag court.
/** */
* Crée un nouvel argument de type option. public function __construct ($name, $description, $tagLong = null, $tagShort = null) {
* parent::__construct($name, $description);
* @param string $name Le nom.
* @param string|null $description La description. $this->setTagLong($tagLong);
* @param string|null $tagLong Le tag long. $this->setTagShort($tagShort);
* @param string|null $tagShort Le tag court. }
*/
public function __construct ($name, $description, $tagLong = null, $tagShort = null) { /**
parent::__construct($name, $description); * Est-ce la valeur correspond au tag court ou long ?
*
$this->setTagLong($tagLong); * Utilisé par les classes enfants pour savoir si le tag correspond.
$this->setTagShort($tagShort); *
} * @param string $arg La valeur à examiner.
*
/** * @return boolean True si le tag correspond, sinon False.
* Est-ce la valeur correspond au tag court ou long ? */
* protected function _parseTag($arg) {
* Utilisé par les classes enfants pour savoir si le tag correspond. if($this->hasTagShort()) {
* if ($arg == '-'.$this->getTagShort())
* @param string $arg La valeur à examiner. return true;
* }
* @return boolean True si le tag correspond, sinon False.
*/ return $arg == '--'.$this->getTagLong();
protected function _parseTag($arg) { }
if($this->hasTagShort()) {
if ($arg == '-'.$this->getTagShort()) public function getTagShort() {
return true; return $this->_tagShort;
} }
/**
return $arg == '--'.$this->getTagLong(); * Définit le tag court.
} *
* @param string|null $tagShort Le tage court.
public function getTagShort() { *
return $this->_tagShort; * @return $this
} *
/** * @see $_tagShort
* Définit le tag court. */
* public function setTagShort($tagShort = null) {
* @param string|null $tagShort Le tage court. $this->_tagShort = $tagShort;
* return $this;
* @return $this }
* /**
* @see $_tagShort * Est-ce que l'argument existe en forme courte ?
*/ *
public function setTagShort($tagShort = null) { * @return boolean True si existe en forme courte, sinon False.
$this->_tagShort = $tagShort; */
return $this; public function hasTagShort() {
} return !is_null($this->_tagShort);
/** }
* Est-ce que l'argument existe en forme courte ?
* public function getTagLong() {
* @return boolean True si existe en forme courte, sinon False. return $this->_tagLong;
*/ }
public function hasTagShort() { /**
return !is_null($this->_tagShort); * Définit le tag long.
} *
* Si non fourni, est déduit du {@see $_name nom de l'argument} :
public function getTagLong() { * - Les "_" sont remplacés par "-"
return $this->_tagLong; * - Les lettres majuscules sont remplacées par "-" suivit de la lettre en minuscule
} *
/** * @param string|null $tagLong Le tag long.
* Définit le tag long. *
* * @return $this
* Si non fourni, est déduit du {@see $_name nom de l'argument} : *
* - Les "_" sont remplacés par "-" * @see $_tagLong
* - Les lettres majuscules sont remplacées par "-" suivit de la lettre en minuscule */
* public function setTagLong($tagLong = null) {
* @param string|null $tagLong Le tag long. $this->_tagLong = $tagLong;
*
* @return $this if(is_null($this->_tagLong)) {
* $this->_tagLong = preg_replace_callback(
* @see $_tagLong '/[A-Z_]/',
*/ function ($matches) {
public function setTagLong($tagLong = null) { if($matches[0] == '_')
$this->_tagLong = $tagLong; return '-';
else
if(is_null($this->_tagLong)) { return '-'.strtolower($matches[0]);
$this->_tagLong = preg_replace_callback( },
'/[A-Z_]/', $this->getName()
function ($matches) { );
if($matches[0] == '_') }
return '-';
else return $this;
return '-'.strtolower($matches[0]); }
},
$this->getName() public function allowMultiple() {
); return $this->_multiple;
} }
/**
return $this; * Définit si l'argument est autorisé plusieurs fois ou non.
} *
* @param boolean $multiple Argument autorisé plusieurs fois ?
public function allowMultiple() { *
return $this->_multiple; * @return $this
} * @see $_optional
/** */
* Définit si l'argument est autorisé plusieurs fois ou non. public function setMultiple($multiple) {
* if (!is_bool($multiple))
* @param boolean $multiple Argument autorisé plusieurs fois ? throw new InvalidArgumentException('La multiplicité n\'est pas un booléen');
*
* @return $this $this->_multiple = $multiple;
* return $this;
* @throws InvalidArgument Si la valeur n'est pas un booléen }
*
* @see $_optional public function isStoppingParse() {
*/ return $this->_isStoppingParse;
public function setMultiple($multiple) { }
if (!is_bool($multiple)) /**
throw new InvalidArgument($multiple, 'La valeur n\'est pas un booléen'); * Définit si l'argument met fin au parsage ou non.
*
$this->_multiple = $multiple; * @param boolean $stoppingParse Met fin au parsage ?
return $this; *
} * @return $this
* @see $_optional
public function isStoppingParse() { */
return $this->_isStoppingParse; public function setStoppingParse($stoppingParse) {
} if (!is_bool($stoppingParse))
/** throw new InvalidArgumentException('La stoppabilité n\'est pas un booléen');
* Définit si l'argument met fin au parsage ou non.
* $this->_isStoppingParse = $stoppingParse;
* @param boolean $stoppingParse Met fin au parsage ? return $this;
* }
* @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;
}
} }

@ -1,84 +1,86 @@
<?php <?php
/** /**
* Déclaration de la classe CommandLine\Argument\Option\Value * Déclaration de la classe CommandLine\Argument\Option\Value
*/ */
namespace CommandLine\Argument\Option; namespace CommandLine\Argument\Option;
use Fidit\v3\Exception\InvalidArgument; use CommandLine\Argument\IArgumentValueDescription;
use CommandLine\Exception\IncorrectParse; use CommandLine\Argument\Parser\IValueParser;
use CommandLine\Argument\IArgumentValueDescription; use CommandLine\Argument\ParseResult;
use CommandLine\Argument\Parser\IValueParser; use CommandLine\Exception\IncorrectParse;
use CommandLine\Argument\ParseResult; use RuntimeException;
/** /**
* Option contenant une valeur. * Option contenant une valeur
* *
* @property IValueParser $valueParser Le parseur de valeur. * @package CommandLine\Argument\Option
* */
* @package CommandLine\Argument\Option class Value extends OptionAbstract implements IArgumentValueDescription {
*/ /**
class Value extends OptionAbstract implements IArgumentValueDescription { * @var IValueParser Parseur pour la valeur
/** */
* @var IValueParser Parseur pour la valeur protected $_valueParser;
*/
protected $_valueParser; /**
* Crée un nouvel argument de type option
/** *
* Crée un nouvel argument de type option. * @param string $name Le nom
* * @param string|null $description La description
* @param string $name Le nom. * @param IValueParser $valueParser Le parseur de valeur
* @param string|null $description La description. * @param string|null $tagLong Le tag long
* @param IValueParser $valueParser Le parseur de valeur. * @param string|null $tagShort Le tag court
* @param string|null $tagLong Le tag long. */
* @param string|null $tagShort Le tag court. public function __construct ($name, $description, IValueParser $valueParser, $tagLong = null, $tagShort = null) {
* parent::__construct($name, $description, $tagLong, $tagShort);
* @throws \Fidit\v3\Exception\InvalidArgument Si l'un des paramètres n'est pas correct $this->setValueParser($valueParser);
*/ }
public function __construct ($name, $description, IValueParser $valueParser, $tagLong = null, $tagShort = null) {
parent::__construct($name, $description, $tagLong, $tagShort); /**
$this->setValueParser($valueParser); * @inheritDoc
} */
public function parse ($args) {
public function parse ($args) { try {
try { if(!$this->_parseTag($args[0]))
if(!$this->_parseTag($args[0])) return null;
return null;
if(count($args) < 2 || is_null($args[1]))
if(count($args) < 2 || is_null($args[1])) throw new IncorrectParse('La seconde valeur de l\'argument manquante');
throw new IncorrectParse($this,null, 'Seconde valeur de l\'argument manquante');
return new ParseResult($this->_valueParser->parseValue($args[1]), 2);
return new ParseResult($this->_valueParser->parseValue($args[1]), 2); }
} catch (RuntimeException $e) {
catch (InvalidArgument $e) { throw new IncorrectParse('Échec du parsage de la valeur "' . $args[0] . '"', 0, $e);
throw IncorrectParse::createFromInvalidArgument($this, $e); }
} }
}
/**
public function getValueDescription () { * @inheritDoc
return $this->_valueParser->getValueDescription(); */
} public function getValueDescription () {
return $this->_valueParser->getValueDescription();
/** }
* Le parseur de valeur.
* /**
* @return IValueParser Le parseur. * Le parseur de valeur.
* *
* @see $_valueParser * @return IValueParser Le parseur.
*/ *
public function getValueParser() { * @see $_valueParser
return $this->_valueParser; */
} public function getValueParser() {
/** return $this->_valueParser;
* Définit le parseur de valeur }
* /**
* @param IValueParser $valueParser Le parseur * Définit le parseur de valeur
* *
* @return $this * @param IValueParser $valueParser Le parseur
* *
* @see $_valueParser * @return $this
*/ *
public function setValueParser(IValueParser $valueParser) { * @see $_valueParser
$this->_valueParser = $valueParser; */
return $this; public function setValueParser(IValueParser $valueParser) {
} $this->_valueParser = $valueParser;
return $this;
}
} }

@ -73,7 +73,7 @@ class ParseResult {
* *
* @see $_value * @see $_value
*/ */
public function setValue($value) { public function setValue($value) {
$this->_value = $value; $this->_value = $value;
return $this; return $this;
} }

@ -4,7 +4,7 @@
*/ */
namespace CommandLine\Argument\Parser; namespace CommandLine\Argument\Parser;
use http\Exception\InvalidArgumentException; use RangeException;
/** /**
* Parseur vers un booléen * Parseur vers un booléen
@ -24,7 +24,7 @@ class BooleanParser implements IValueParser {
'true' , '1', 'oui', 'vrai', 'true' , '1', 'oui', 'vrai',
'false', '0', 'non', 'faux', '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')); return in_array($arg, array('true' , '1', 'oui', 'vrai'));
} }

@ -5,6 +5,7 @@
namespace CommandLine\Argument\Parser; namespace CommandLine\Argument\Parser;
use InvalidArgumentException; use InvalidArgumentException;
use RangeException;
/** /**
* Parseur vers un réel * Parseur vers un réel
@ -37,17 +38,17 @@ class DecimalParser implements IValueParser {
*/ */
public function parseValue ($arg) { public function parseValue ($arg) {
if (!$this->_isDecimal($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; $int = (int)$arg;
if ($this->hasValueMin()) { if ($this->hasValueMin()) {
if ($int < $this->getValueMin()) 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 ($this->hasValueMax()) {
if ($int > $this->getValueMax()) 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; return $int;

@ -5,6 +5,7 @@
namespace CommandLine\Argument\Parser; namespace CommandLine\Argument\Parser;
use InvalidArgumentException; use InvalidArgumentException;
use RangeException;
/** /**
* Parseur vers une liste de valeurs possible (chaine de caractère). * Parseur vers une liste de valeurs possible (chaine de caractère).
@ -31,7 +32,7 @@ class EnumParser implements IValueParser {
*/ */
public function parseValue ($arg) { public function parseValue ($arg) {
if(!in_array($arg, $this->_values)) 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; return $arg;
} }

@ -5,6 +5,7 @@
namespace CommandLine\Argument\Parser; namespace CommandLine\Argument\Parser;
use InvalidArgumentException; use InvalidArgumentException;
use RangeException;
/** /**
* Parseur vers un entier * Parseur vers un entier
@ -37,17 +38,17 @@ class IntegerParser implements IValueParser {
*/ */
public function parseValue ($arg) { public function parseValue ($arg) {
if(!$this->_isInt($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; $int = (int)$arg;
if($this->hasValueMin()) { if($this->hasValueMin()) {
if($int < $this->getValueMin()) 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($this->hasValueMax()) {
if($int > $this->getValueMax()) 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; return $int;

@ -4,7 +4,7 @@
*/ */
namespace CommandLine\Argument\Parser; namespace CommandLine\Argument\Parser;
use InvalidArgumentException; use RangeException;
/** /**
* Parseur vers une chaine de caractère correspondant à un motif * Parseur vers une chaine de caractère correspondant à un motif
@ -33,7 +33,7 @@ class RegexParser implements IValueParser {
*/ */
public function parseValue ($arg) { public function parseValue ($arg) {
if(!preg_match($this->_regex, $arg, $matches)) 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; return $matches;
} }

@ -1,74 +1,74 @@
<?php <?php
/** /**
* Déclaration de CommandLine\Argument\Value\FixedValue * Déclaration de CommandLine\Argument\Value\FixedValue
*/ */
namespace CommandLine\Argument\Value; namespace CommandLine\Argument\Value;
use CommandLine\Argument\IArgumentValueDescription; use CommandLine\Argument\IArgumentValueDescription;
use CommandLine\Argument\ParseResult; use CommandLine\Argument\ParseResult;
/** /**
* Argument devant correspondre une valeur fixe * Argument devant correspondre une valeur fixe
* *
* @property string $value {@see $_value $_value}. * @package CommandLine\Argument
* */
* @package CommandLine\Argument class FixedValue extends ValueAbstract implements IArgumentValueDescription {
*/ /**
class FixedValue extends ValueAbstract implements IArgumentValueDescription { * @var string La valeur que doit avoir l'argument
/** */
* @var string La valeur que doit avoir l'argument protected $_value;
*/
protected $_value; /**
* Crée un nouvel argument de type valeur
/** *
* Crée un nouvel argument de type valeur * @param string $varName Le nom de la variable
* * @param string $value La valeur
* @param int $position La position * @param string|null $description La description
* @param string $varName Le nom de la variable * @param boolean $optional Valeur optionnelle ?
* @param string $value La valeur */
* @param string|null $description La description public function __construct ($varName, $value, $description, $optional = false) {
* @param boolean $optional Valeur optionnelle ? parent::__construct($value, $description, $optional);
* $this->setVarName($varName);
* @throws \Fidit\v3\Exception\InvalidArgument Si l'un des paramètres n'est pas correct $this->setValue($value);
*/ }
public function __construct ($varName, $value, $description, $optional = false) {
parent::__construct($value, $description, $optional); /**
$this->setVarName($varName); * @inheritDoc
$this->setValue($value); */
} public function parse ($args) {
if($args[0] == $this->getValue())
public function parse ($args) { return new ParseResult($this->getValue(), 1);
if($args[0] == $this->getValue())
return new ParseResult($this->getValue(), 1); return null;
}
return null;
} /**
* @inheritDoc
public function getValueDescription () { */
return '"'.$this->getValue().'"'; public function getValueDescription () {
} return '"'.$this->getValue().'"';
}
/**
* La valeur. /**
* * La valeur.
* @return string La valeur. *
* * @return string La valeur.
* @see $_value *
*/ * @see $_value
public function getValue() { */
return $this->_value; public function getValue() {
} return $this->_value;
/** }
* Définit la valeur /**
* * Définit la valeur
* @param string $value La valeur *
* * @param string $value La valeur
* @return $this *
* * @return $this
* @see $_value * @see $_value
*/ */
public function setValue($value) { public function setValue($value) {
$this->_value = $value; $this->_value = $value;
return $this; return $this;
} }
} }

@ -1,76 +1,80 @@
<?php <?php
/** /**
* Déclaration de CommandLine\Argument\Value\Value * Déclaration de CommandLine\Argument\Value\Value
*/ */
namespace CommandLine\Argument\Value; namespace CommandLine\Argument\Value;
use Fidit\v3\Exception\InvalidArgument; use CommandLine\Argument\IArgumentValueDescription;
use CommandLine\Exception\IncorrectParse; use CommandLine\Argument\Parser\IValueParser;
use CommandLine\Argument\IArgumentValueDescription; use CommandLine\Argument\ParseResult;
use CommandLine\Argument\Parser\IValueParser; use CommandLine\Exception\IncorrectParse;
use CommandLine\Argument\ParseResult; use RangeException;
/** /**
* Argument de type "valeur" * Argument de type "valeur"
* *
* @property IValueParser $valueParser Le parseur de valeur. * @package CommandLine\Argument\Value
* */
* @package CommandLine\Argument\Value class Value extends ValueAbstract implements IArgumentValueDescription {
*/ /**
class Value extends ValueAbstract implements IArgumentValueDescription { * @var IValueParser Parseur pour la valeur
/** */
* @var IValueParser Parseur pour la valeur protected $_valueParser;
*/
protected $_valueParser; /**
* Crée un nouvel argument de type valeur
/** *
* Crée un nouvel argument de type valeur * @param string $name Le nom
* * @param string|null $description La description
* @param string $name Le nom * @param IValueParser $valueParser Le parseur de valeur.
* @param string|null $description La description * @param boolean $optional Valeur optionnelle ?
* @param IValueParser $valueParser Le parseur de valeur. */
* @param boolean $optional Valeur optionnelle ? public function __construct ($name, $description, IValueParser $valueParser, $optional = false) {
*/ parent::__construct($name, $description, $optional);
public function __construct ($name, $description, IValueParser $valueParser, $optional = false) {
parent::__construct($name, $description, $optional); $this->setValueParser($valueParser);
}
$this->setValueParser($valueParser);
} /**
* @inheritDoc
public function parse ($args) { */
try { public function parse ($args) {
return new ParseResult($this->_valueParser->parseValue($args[0]), 1); try {
} return new ParseResult($this->_valueParser->parseValue($args[0]), 1);
catch(InvalidArgument $e) { }
throw IncorrectParse::createFromInvalidArgument($this, $e); catch (RangeException $e) {
} throw new IncorrectParse('Échec du parsage de la valeur "' . $args[0] . '"', 0, $e);
} }
}
public function getValueDescription () {
return $this->_valueParser->getValueDescription(); /**
} * @inheritDoc
*/
/** public function getValueDescription () {
* Le parseur de valeur. return $this->_valueParser->getValueDescription();
* }
* @return IValueParser Le parseur.
* /**
* @see $_valueParser * Le parseur de valeur.
*/ *
public function getValueParser() { * @return IValueParser Le parseur.
return $this->_valueParser; *
} * @see $_valueParser
/** */
* Définit le parseur de valeur public function getValueParser() {
* return $this->_valueParser;
* @param IValueParser $valueParser Le parseur }
* /**
* @return $this * Définit le parseur de valeur
* *
* @see $_valueParser * @param IValueParser $valueParser Le parseur
*/ *
public function setValueParser(IValueParser $valueParser) { * @return $this
$this->_valueParser = $valueParser; *
return $this; * @see $_valueParser
} */
public function setValueParser(IValueParser $valueParser) {
$this->_valueParser = $valueParser;
return $this;
}
} }

@ -1,117 +1,116 @@
<?php <?php
/** /**
* Déclaration de la classe CommandLine\Argument\Value\ValueArgument. * Déclaration de la classe CommandLine\Argument\Value\ValueArgument.
*/ */
namespace CommandLine\Argument\Value;
namespace CommandLine\Argument\Value;
use Fidit\v3\Exception\InvalidArgument;
use CommandLine\Argument\ArgumentAbstract; use CommandLine\Argument\ArgumentAbstract;
use CommandLine\Argument\Value\IArgumentValue; use InvalidArgumentException;
/** /**
* Classe abstraite pour les paramètres de type "valeur" * Classe abstraite pour les paramètres de type "valeur"
* *
* @property int $occurMin {@see $_occurMin $_occurMin} * @package CommandLine\Argument\Value
* @property int|null $occurMax {@see $_occurMax $_occurMax} */
* abstract class ValueAbstract extends ArgumentAbstract implements IArgumentValue {
* @package CommandLine\Argument\Value /**
*/ * @var int Le nombre minimum d'occurence.
abstract class ValueAbstract extends ArgumentAbstract implements IArgumentValue { */
/** protected $_occurMin = 1;
* @var int Le nombre minimum d'occurence. /**
*/ * @var int|null Le nombre maximum d'occurence.
protected $_occurMin = 1; */
/** protected $_occurMax = 1;
* @var int|null Le nombre maximum d'occurence.
*/ /**
protected $_occurMax = 1; * Crée un nouvel argument.
*
/** * @param string $name Le nom de l'argument
* Crée un nouvel argument. * @param string|null $description La description de l'argument
* * @param boolean $optional Argument optionel ?
* @param string $name Le nom de l'argument */
* @param string|null $description La description de l'argument protected function __construct ($name, $description, $optional = false) {
* @param boolean $optional Argument optionel ? parent::__construct($name, $description);
*/
protected function __construct ($name, $description, $optional = false) { $this->setOptional($optional);
parent::__construct($name, $description); }
/**
$this->setOptional($optional); * Définit si l'argument est facultatif ou non.
} *
* @param bool $optional Argument facultatif ?
public function getOccurMin() { *
return $this->_occurMin; * @return $this
} */
/** public function setOptional ($optional = false) {
* Définit le nombre minimum d'occurence. $this->setOccurMin($optional ? 0 : 1);
* return $this;
* @param int $occurMin Le nombre minimum }
*
* @return $this /**
* * @inheritDoc
* @throws InvalidArgument Si la valeur n'est pas un entier positif. */
* public function getOccurMin () {
* @see $_occurMin return $this->_occurMin;
*/ }
public function setOccurMin($occurMin) { /**
if (!is_numeric($occurMin)) * Définit le nombre minimum d'occurence.
throw new InvalidArgument($occurMin, 'La valeur n\'est pas un entier'); *
* @param int $occurMin Le nombre minimum
if (floor($occurMin) != $occurMin) *
throw new InvalidArgument($occurMin, 'La valeur n\'est pas un entier'); * @return $this
* @see $_occurMin
$int = (int)$occurMin; */
if($int < 0) public function setOccurMin ($occurMin) {
throw new InvalidArgument($occurMin, 'La valeur n\'est pas un entier positif'); if (!is_numeric($occurMin)) {
throw new InvalidArgumentException('Le nombre minimum d\'ocurrence n\'est pas un entier');
$this->_occurMin = $int; }
return $this;
} if (floor($occurMin) != $occurMin) {
/** throw new InvalidArgumentException('Le nombre minimum d\'ocurrence n\'est pas un entier');
* Définit si l'argument est facultatif ou non. }
*
* @param bool $optional Argument facultatif ? $int = (int)$occurMin;
* if ($int < 0) {
* @return $this throw new InvalidArgumentException('Le nombre minimum d\'ocurrence n\'est pas un entier positif');
*/ }
public function setOptional($optional = false) {
try { $this->_occurMin = $int;
$this->setOccurMin($optional ? 0 : 1); return $this;
} }
catch(InvalidArgument $e) {}
/**
return $this; * @inheritDoc
} */
public function getOccurMax () {
public function getOccurMax() { return $this->_occurMax;
return $this->_occurMax; }
} /**
/** * Définit le nombre mawimum d'occurence.
* Définit le nombre mawimum d'occurence. *
* * @param int|null $occurMax Le nombre maximum
* @param int|null $occurMax Le nombre maximum *
* * @return $this
* @return $this * @see $_occurMax
* */
* @throws InvalidArgument Si la valeur n'est pas Null ou un entier strictement positif. public function setOccurMax ($occurMax) {
* if (!is_null($occurMax)) {
* @see $_occurMax if (!is_numeric($occurMax)) {
*/ throw new InvalidArgumentException('Le nombre maximum d\'ocurrence n\'est pas un entier');
public function setOccurMax($occurMax) { }
if (!is_null($occurMax)) {
if (!is_numeric($occurMax)) if (floor($occurMax) != $occurMax) {
throw new InvalidArgument($occurMax, 'La valeur n\'est pas un entier'); throw new InvalidArgumentException('Le nombre maximum d\'ocurrence 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) {
$occurMax = (int)$occurMax; throw new InvalidArgumentException('Le nombre maximum d\'ocurrence n\'est pas un entier strictement positif');
if ($occurMax <= 0) }
throw new InvalidArgument($occurMax, 'La valeur n\'est pas un entier strictement positif'); }
}
$this->_occurMax = $occurMax;
$this->_occurMax = $occurMax; return $this;
return $this; }
}
} }

@ -1,21 +1,21 @@
<?php <?php
/** /**
* Déclaration de l'exception CommandLine\Exception\IncorrectParse. * Déclaration de l'exception CommandLine\Exception\IncorrectParse.
*/ */
namespace CommandLine\Exception; namespace CommandLine\Exception;
use CommandLine\Argument\IArgument; use CommandLine\Argument\IArgument;
use LogicException; use RuntimeException;
/** /**
* Exception levée quand le parsage d'un argument échoue * Exception levée quand le parsage d'un argument échoue
* *
* Exemples : * Exemples :
* - Valeur incorrecte * - Valeur incorrecte
* - Valeur manquante * - Valeur manquante
* *
* @package CommandLine\Exception * @package CommandLine\Exception
* *
* @see IArgument::parse() * @see IArgument::parse()
*/ */
class IncorrectParse extends LogicException implements IException {} class IncorrectParse extends RuntimeException implements IException {}
Loading…
Cancel
Save