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

69 lines
1.4 KiB
PHP

<?php
/**
* Déclaration de la classe CommandLine\Argument\Parser\RegexParser
*/
namespace CommandLine\Argument\Parser;
use InvalidArgumentException;
/**
* Parseur vers une chaine de caractère correspondant à un motif
*
* Renvoie la liste des groupes capturants
*
* @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);
}
/**
* @inheritDoc
*/
public function parseValue ($arg) {
if(!preg_match($this->_regex, $arg, $matches))
throw new InvalidArgumentException('La valeur ne correspond pas au motif attendu : ' . $arg);
return $matches;
}
/**
* @inheritDoc
*/
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;
}
}