You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
phpcommandline/Argument/Parser/RegexParser.class.php

65 lines
1.5 KiB
PHP

<?php
/**
* Déclaration de la classe CommandLine\Argument\Parser\RegexParser
*/
namespace CommandLine\Argument\Parser;
use Fidit\v3\Exception\InvalidArgument;
/**
* Parseur vers une chaine de caractère correspondant à un motif
*
* Renvoie la liste des groupes capturants
*
* @property string $regex {@see $_regex $_regex}
*
* @package CommandLine\Argument\Parser
*/
class RegexParser implements IValueParser {
/**
* @var string La regex à respecter
*/
protected $_regex = '';
/**
* Crée un nouveau parseur
*
* @param string $regex Le motif à respecter
*/
public function __construct ($regex) {
$this->setRegex($regex);
}
public function parseValue ($arg) {
if(!preg_match($this->regex, $arg, $matches))
throw new InvalidArgument($arg, 'La valeur ne correspond pas au motif attendu');
return $matches;
}
public function getValueDescription () {
return $this->getRegex();
}
/**
* Le motif à respecter
*
* @return string Le motif
*
* @see $_regex
*/
public function getRegex() {
return $this->_regex;
}
/**
* Modifie le motif à respecter
*
* @param string $regex Le nouveau motif
*
* @return $this
*/
public function setRegex($regex = null) {
$this->_regex = $regex;
return $this;
}
}