Completed
Push — master ( 82eb41...4f6b0a )
by Alexander
03:49
created

GlobSubLexer::getTokenAt()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 10
nc 3
nop 2
crap 4
1
<?php
2
namespace Xiag\Rql\Parser\SubLexer;
3
4
use Xiag\Rql\Parser\Token;
5
use Xiag\Rql\Parser\SubLexerInterface;
6
use Xiag\Rql\Parser\Glob;
7
8
class GlobSubLexer implements SubLexerInterface
9
{
10
    /**
11
     * @inheritdoc
12
     */
13 84
    public function getTokenAt($code, $cursor)
14
    {
15 84
        if (!preg_match('/([a-z0-9\*\?]|\%[0-9a-f]{2})+/Ai', $code, $matches, null, $cursor)) {
16 14
            return null;
17 84
        } elseif (strpos($matches[0], '?') === false && strpos($matches[0], '*') === false) {
18 84
            return null;
19
        }
20
21 22
        return new Token(
22 22
            Token::T_GLOB,
23 22
            $this->decodeGlob($matches[0]),
24 22
            $cursor,
25 22
            $cursor + strlen($matches[0])
26 22
        );
27
    }
28
29 22
    private function decodeGlob($glob)
30
    {
31 22
        return preg_replace_callback(
32 22
            '/[^\*\?]+/i',
33 22
            function ($encoded) {
34 13
                return Glob::encode(rawurldecode($encoded[0]));
35 22
            },
36
            $glob
37 22
        );
38
    }
39
}
40