-
Notifications
You must be signed in to change notification settings - Fork 15
/
07-timestamp-caster.php
40 lines (32 loc) · 1.12 KB
/
07-timestamp-caster.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace Graviton\RqlParserExample07;
use Graviton\RqlParser\Lexer;
use Graviton\RqlParser\Parser;
use Graviton\RqlParser\Token;
use Graviton\RqlParser\TypeCasterInterface;
use Graviton\RqlParser\Exception\SyntaxErrorException;
use Graviton\RqlParser\NodeParser\Query\ComparisonOperator\Rql\EqNodeParser;
use Graviton\RqlParser\ValueParser;
require __DIR__ . '/../vendor/autoload.php';
class TimestampCaster implements TypeCasterInterface
{
/**
* @inheritdoc
*/
public function typeCast(Token $token)
{
if (!$token->test(Token::T_INTEGER)) {
throw new SyntaxErrorException('Timestamp type caster expects an integer token');
}
return new \DateTime('@' . $token->getValue());
}
}
$scalarParser = (new ValueParser\ScalarParser())
->registerTypeCaster('timestamp', new TimestampCaster());
$fieldNameParser = new ValueParser\FieldParser();
$nodeParser = new EqNodeParser($fieldNameParser, $scalarParser);
// parse
$lexer = new Lexer();
$parser = new Parser($nodeParser);
$tokenStream = $lexer->tokenize('eq(a,timestamp:1444000000)');
var_dump($parser->parse($tokenStream));