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

DatetimeSubLexer::getTokenAt()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 8.8571
cc 6
eloc 12
nc 3
nop 2
crap 6
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\Exception\SyntaxErrorException;
7
8
class DatetimeSubLexer implements SubLexerInterface
9
{
10
    /**
11
     * @inheritdoc
12
     */
13 45
    public function getTokenAt($code, $cursor)
14
    {
15 45
        $regExp = '/(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})T(?<h>\d{2}):(?<i>\d{2}):(?<s>\d{2})Z/A';
16 45
        if (!preg_match($regExp, $code, $matches, null, $cursor)) {
17 37
            return null;
18
        }
19
20 15
        if (!checkdate($matches['m'], $matches['d'], $matches['y']) ||
21 15
            !($matches['h'] < 24 && $matches['i'] < 60 && $matches['s'] < 60)) {
22 2
            throw new SyntaxErrorException(sprintf('Invalid datetime value "%s"', $matches[0]));
23
        }
24
25 15
        return new Token(
26 15
            Token::T_DATE,
27 15
            $matches[0],
28 15
            $cursor,
29 15
            $cursor + strlen($matches[0])
30 15
        );
31
    }
32
}
33