Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
CRAP
82.76% covered (warning)
82.76%
24 / 29
PregSimple
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
12.74
82.76% covered (warning)
82.76%
24 / 29
 __construct
0.00% covered (danger)
0.00%
0 / 1
5.76
68.75% covered (warning)
68.75%
11 / 16
 setCallback
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getExpression
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 hydrateResult
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
10 / 10
<?php
namespace Popy\Calendar\Parser\DateLexer;
use InvalidArgumentException;
use Popy\Calendar\Parser\FormatToken;
use Popy\Calendar\Parser\DateLexerResult;
/**
 * Simple Preg lexer implementation : will match a single pattern and associate
 * it with the input token if it's a symbol.
 *
 * Litterals and EOF are also handled, but the input pattern is not used.
 */
class PregSimple extends AbstractPreg
{
    /**
     * Registered symbol.
     *
     * @var string|null
     */
    protected $symbol;
    /**
     * Expression.
     *
     * @var string
     */
    protected $expression;
    /**
     * Match processing callback.
     *
     * @var callable
     */
    protected $callback;
    /**
     * Class constructor.
     *
     * @param FormatToken $token   Token to match.
     * @param string|null $pattern Preg pattern.
     */
    public function __construct(FormatToken $token, $pattern = null)
    {
        if ($token->isLitteral()) {
            $this->expression = preg_quote($token->getValue(), '/');
            return;
        }
        if ($token->isType(FormatToken::TYPE_EOF)) {
            $this->expression = '$';
            return;
        }
        if ($token->isSymbol()) {
            if ($pattern === null) {
                throw new InvalidArgumentException(
                    'You must supply a pattern for a Symbol token'
                );
            }
            $this->symbol = $token->getName();
            $this->expression = '(' . $pattern . ')';
            return;
        }
        throw new InvalidArgumentException(
            'Unsupported token type : ' . $token->getType()
        );
    }
    /**
     * Match processing callback.
     *
     * The callback will receive the Lexer and the matched string as input, and
     * must return the value to hydrate in the result.
     *
     * @param callable $callback
     */
    public function setCallback($callback)
    {
        $this->callback = $callback;
    }
    /**
     * @inheritDoc
     */
    public function getExpression()
    {
        return $this->expression;
    }
    /**
     * @inheritDoc
     */
    public function hydrateResult(DateLexerResult $result, $match, $offset = 1)
    {
        if ($this->symbol === null || !isset($match[$offset])) {
            return $offset;
        }
        // Did match
        if ($match[$offset][1] !== -1) {
            $res = $match[$offset][0];
            if ($this->callback !== null) {
                $res = call_user_func($this->callback, $this, $res);
            }
            $result->set($this->symbol, $res);
        }
        return $offset + 1;
    }
}