PregSimple::setCallback()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Popy\Calendar\Parser\DateLexer;
4
5
use InvalidArgumentException;
6
use Popy\Calendar\Parser\FormatToken;
7
use Popy\Calendar\Parser\DateLexerResult;
8
9
/**
10
 * Simple Preg lexer implementation : will match a single pattern and associate
11
 * it with the input token if it's a symbol.
12
 *
13
 * Litterals and EOF are also handled, but the input pattern is not used.
14
 */
15
class PregSimple extends AbstractPreg
16
{
17
    /**
18
     * Registered symbol.
19
     *
20
     * @var string|null
21
     */
22
    protected $symbol;
23
24
    /**
25
     * Expression.
26
     *
27
     * @var string
28
     */
29
    protected $expression;
30
31
    /**
32
     * Match processing callback.
33
     *
34
     * @var callable
35
     */
36
    protected $callback;
37
38
    /**
39
     * Class constructor.
40
     *
41
     * @param FormatToken $token   Token to match.
42
     * @param string|null $pattern Preg pattern.
43
     */
44
    public function __construct(FormatToken $token, $pattern = null)
45
    {
46
        if ($token->isLitteral()) {
47
            $this->expression = preg_quote($token->getValue(), '/');
48
            return;
49
        }
50
51
        if ($token->isType(FormatToken::TYPE_EOF)) {
52
            $this->expression = '$';
53
            return;
54
        }
55
56
        if ($token->isSymbol()) {
57
            if ($pattern === null) {
58
                throw new InvalidArgumentException(
59
                    'You must supply a pattern for a Symbol token'
60
                );
61
            }
62
            $this->symbol = $token->getName();
63
            $this->expression = '(' . $pattern . ')';
64
            return;
65
        }
66
67
        throw new InvalidArgumentException(
68
            'Unsupported token type : ' . $token->getType()
69
        );
70
    }
71
72
    /**
73
     * Match processing callback.
74
     *
75
     * The callback will receive the Lexer and the matched string as input, and
76
     * must return the value to hydrate in the result.
77
     *
78
     * @param callable $callback
79
     */
80
    public function setCallback($callback)
81
    {
82
        $this->callback = $callback;
83
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88
    public function getExpression()
89
    {
90
        return $this->expression;
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    public function hydrateResult(DateLexerResult $result, $match, $offset = 1)
97
    {
98
        if ($this->symbol === null || !isset($match[$offset])) {
99
            return $offset;
100
        }
101
102
        // Did match
103
        if ($match[$offset][1] !== -1) {
104
            $res = $match[$offset][0];
105
106
            if ($this->callback !== null) {
107
                $res = call_user_func($this->callback, $this, $res);
108
            }
109
110
            $result->set($this->symbol, $res);
111
        }
112
113
        return $offset + 1;
114
    }
115
}
116