Fixing Exception management

master
Julien Rosset 6 years ago
parent 25974f3c47
commit f950043b9c

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

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

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

@ -1,177 +1,164 @@
<?php
/**
* Déclaration de la classe CommandLine\Argument\Option\OptionAbstract.
*/
namespace CommandLine\Argument\Option;
use Fidit\v3\Exception\InvalidArgument;
use CommandLine\Argument\ArgumentAbstract;
/**
* 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).
*
* @property string|null $tagShort {@see $_tagShort $_tagShort }
* @property string|null $tagLong {@see $_tagLong $_tagLong }
* @property boolean $multiple {@see $_multiple $_multiple }
* @property boolean $isStoppingParse {@see $_isStoppingParse $_isStoppingParse}
*
* @package CommandLine\Argument\Option
*/
abstract class OptionAbstract extends ArgumentAbstract implements IArgumentOption {
/**
* @var string|null L'argument court (cas d'un seul "-"). Null si aucun.
*/
protected $_tagShort;
/**
* @var string L'argument long (cas d'un "--").
*/
protected $_tagLong;
/**
* @var boolean Est-ce que l'argument est autorisé plusieurs fois ?
*/
protected $_multiple = false;
/**
* @var boolean Est-ce que l'argument met fin au parsage ?
*/
protected $_isStoppingParse = false;
/**
* Crée un nouvel argument de type option.
*
* @param string $name Le nom.
* @param string|null $description La description.
* @param string|null $tagLong Le tag long.
* @param string|null $tagShort Le tag court.
*/
public function __construct ($name, $description, $tagLong = null, $tagShort = null) {
parent::__construct($name, $description);
$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
*
* @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;
}
<?php
/**
* Déclaration de la classe CommandLine\Argument\Option\OptionAbstract.
*/
namespace CommandLine\Argument\Option;
use CommandLine\Argument\ArgumentAbstract;
use InvalidArgumentException;
/**
* Classe abstraite pour les arguments de type "option" : --xxx / -x
*
* @package CommandLine\Argument\Option
*/
abstract class OptionAbstract extends ArgumentAbstract implements IArgumentOption {
/**
* @var string|null L'argument court (cas d'un seul "-"). Null si aucun.
*/
protected $_tagShort;
/**
* @var string L'argument long (cas d'un "--").
*/
protected $_tagLong;
/**
* @var boolean Est-ce que l'argument est autorisé plusieurs fois ?
*/
protected $_multiple = false;
/**
* @var boolean Est-ce que l'argument met fin au parsage ?
*/
protected $_isStoppingParse = false;
/**
* Crée un nouvel argument de type option.
*
* @param string $name Le nom.
* @param string|null $description La description.
* @param string|null $tagLong Le tag long.
* @param string|null $tagShort Le tag court.
*/
public function __construct ($name, $description, $tagLong = null, $tagShort = null) {
parent::__construct($name, $description);
$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;
}
}

@ -1,84 +1,86 @@
<?php
/**
* Déclaration de la classe CommandLine\Argument\Option\Value
*/
namespace CommandLine\Argument\Option;
use Fidit\v3\Exception\InvalidArgument;
use CommandLine\Exception\IncorrectParse;
use CommandLine\Argument\IArgumentValueDescription;
use CommandLine\Argument\Parser\IValueParser;
use CommandLine\Argument\ParseResult;
/**
* Option contenant une valeur.
*
* @property IValueParser $valueParser Le parseur de valeur.
*
* @package CommandLine\Argument\Option
*/
class Value extends OptionAbstract implements IArgumentValueDescription {
/**
* @var IValueParser Parseur pour la valeur
*/
protected $_valueParser;
/**
* Crée un nouvel argument de type option.
*
* @param string $name Le nom.
* @param string|null $description La description.
* @param IValueParser $valueParser Le parseur de valeur.
* @param string|null $tagLong Le tag long.
* @param string|null $tagShort Le tag court.
*
* @throws \Fidit\v3\Exception\InvalidArgument Si l'un des paramètres n'est pas correct
*/
public function __construct ($name, $description, IValueParser $valueParser, $tagLong = null, $tagShort = null) {
parent::__construct($name, $description, $tagLong, $tagShort);
$this->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;
}
<?php
/**
* Déclaration de la classe CommandLine\Argument\Option\Value
*/
namespace CommandLine\Argument\Option;
use CommandLine\Argument\IArgumentValueDescription;
use CommandLine\Argument\Parser\IValueParser;
use CommandLine\Argument\ParseResult;
use CommandLine\Exception\IncorrectParse;
use RuntimeException;
/**
* Option contenant une valeur
*
* @package CommandLine\Argument\Option
*/
class Value extends OptionAbstract implements IArgumentValueDescription {
/**
* @var IValueParser Parseur pour la valeur
*/
protected $_valueParser;
/**
* Crée un nouvel argument de type option
*
* @param string $name Le nom
* @param string|null $description La description
* @param IValueParser $valueParser Le parseur de valeur
* @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);
$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;
}
}

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

@ -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'));
}

@ -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;

@ -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;
}

@ -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;

@ -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;
}

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

@ -1,76 +1,80 @@
<?php
/**
* Déclaration de CommandLine\Argument\Value\Value
*/
namespace CommandLine\Argument\Value;
use Fidit\v3\Exception\InvalidArgument;
use CommandLine\Exception\IncorrectParse;
use CommandLine\Argument\IArgumentValueDescription;
use CommandLine\Argument\Parser\IValueParser;
use CommandLine\Argument\ParseResult;
/**
* Argument de type "valeur"
*
* @property IValueParser $valueParser Le parseur de valeur.
*
* @package CommandLine\Argument\Value
*/
class Value extends ValueAbstract implements IArgumentValueDescription {
/**
* @var IValueParser Parseur pour la valeur
*/
protected $_valueParser;
/**
* Crée un nouvel argument de type valeur
*
* @param string $name Le nom
* @param string|null $description La description
* @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);
$this->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;
}
<?php
/**
* Déclaration de CommandLine\Argument\Value\Value
*/
namespace CommandLine\Argument\Value;
use CommandLine\Argument\IArgumentValueDescription;
use CommandLine\Argument\Parser\IValueParser;
use CommandLine\Argument\ParseResult;
use CommandLine\Exception\IncorrectParse;
use RangeException;
/**
* Argument de type "valeur"
*
* @package CommandLine\Argument\Value
*/
class Value extends ValueAbstract implements IArgumentValueDescription {
/**
* @var IValueParser Parseur pour la valeur
*/
protected $_valueParser;
/**
* Crée un nouvel argument de type valeur
*
* @param string $name Le nom
* @param string|null $description La description
* @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);
$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;
}
}

@ -1,117 +1,116 @@
<?php
/**
* Déclaration de la classe CommandLine\Argument\Value\ValueArgument.
*/
namespace CommandLine\Argument\Value;
use Fidit\v3\Exception\InvalidArgument;
use CommandLine\Argument\ArgumentAbstract;
use CommandLine\Argument\Value\IArgumentValue;
/**
* Classe abstraite pour les paramètres de type "valeur"
*
* @property int $occurMin {@see $_occurMin $_occurMin}
* @property int|null $occurMax {@see $_occurMax $_occurMax}
*
* @package CommandLine\Argument\Value
*/
abstract class ValueAbstract extends ArgumentAbstract implements IArgumentValue {
/**
* @var int Le nombre minimum d'occurence.
*/
protected $_occurMin = 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
* @param string|null $description La description de l'argument
* @param boolean $optional Argument optionel ?
*/
protected function __construct ($name, $description, $optional = false) {
parent::__construct($name, $description);
$this->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;
}
<?php
/**
* Déclaration de la classe CommandLine\Argument\Value\ValueArgument.
*/
namespace CommandLine\Argument\Value;
use CommandLine\Argument\ArgumentAbstract;
use InvalidArgumentException;
/**
* Classe abstraite pour les paramètres de type "valeur"
*
* @package CommandLine\Argument\Value
*/
abstract class ValueAbstract extends ArgumentAbstract implements IArgumentValue {
/**
* @var int Le nombre minimum d'occurence.
*/
protected $_occurMin = 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
* @param string|null $description La description de l'argument
* @param boolean $optional Argument optionel ?
*/
protected function __construct ($name, $description, $optional = false) {
parent::__construct($name, $description);
$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;
}
}

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