|
1
|
|
|
<?php |
|
2
|
|
|
namespace Xiag\Rql\Parser\ValueParser; |
|
3
|
|
|
|
|
4
|
|
|
use Xiag\Rql\Parser\Token; |
|
5
|
|
|
use Xiag\Rql\Parser\TokenStream; |
|
6
|
|
|
use Xiag\Rql\Parser\TypeCasterInterface; |
|
7
|
|
|
use Xiag\Rql\Parser\SubParserInterface; |
|
8
|
|
|
use Xiag\Rql\Parser\Exception\SyntaxErrorException; |
|
9
|
|
|
|
|
10
|
|
|
class ScalarParser implements SubParserInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var TypeCasterInterface[] |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $typeCasters = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @inheritdoc |
|
19
|
|
|
*/ |
|
20
|
28 |
|
public function parse(TokenStream $tokenStream) |
|
21
|
|
|
{ |
|
22
|
28 |
|
if (($typeToken = $tokenStream->nextIf(Token::T_TYPE)) !== null) { |
|
23
|
5 |
|
$tokenStream->expect(Token::T_COLON); |
|
24
|
5 |
|
$value = $this->getTypeCaster($typeToken->getValue())->typeCast($tokenStream->next()); |
|
25
|
4 |
|
} else { |
|
26
|
23 |
|
$value = $this->getScalarValue($tokenStream->next()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
25 |
|
return $value; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $type |
|
34
|
|
|
* @param TypeCasterInterface $typeCaster |
|
35
|
|
|
* @return $this |
|
36
|
|
|
*/ |
|
37
|
64 |
|
public function registerTypeCaster($type, TypeCasterInterface $typeCaster) |
|
38
|
|
|
{ |
|
39
|
64 |
|
$this->typeCasters[$type] = $typeCaster; |
|
40
|
64 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $type |
|
45
|
|
|
* @return TypeCasterInterface |
|
46
|
|
|
*/ |
|
47
|
5 |
|
public function getTypeCaster($type) |
|
48
|
|
|
{ |
|
49
|
5 |
|
if (!isset($this->typeCasters[$type])) { |
|
50
|
1 |
|
throw new SyntaxErrorException(sprintf('Unknown type "%s"', $type)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
return $this->typeCasters[$type]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param Token $token |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
* @throws SyntaxErrorException |
|
60
|
|
|
*/ |
|
61
|
23 |
|
protected function getScalarValue(Token $token) |
|
62
|
|
|
{ |
|
63
|
23 |
|
if ($token->test(Token::T_FALSE)) { |
|
64
|
1 |
|
return false; |
|
65
|
23 |
|
} elseif ($token->test(Token::T_TRUE)) { |
|
66
|
1 |
|
return true; |
|
67
|
23 |
|
} elseif ($token->test(Token::T_NULL)) { |
|
68
|
2 |
|
return null; |
|
69
|
23 |
|
} elseif ($token->test(Token::T_EMPTY)) { |
|
70
|
1 |
|
return ''; |
|
71
|
22 |
|
} elseif ($token->test(Token::T_DATE)) { |
|
72
|
2 |
|
return new \DateTime($token->getValue()); |
|
73
|
21 |
|
} elseif ($token->test(Token::T_STRING)) { |
|
74
|
13 |
|
return $token->getValue(); |
|
75
|
11 |
|
} elseif ($token->test(Token::T_INTEGER)) { |
|
76
|
8 |
|
return (int)$token->getValue(); |
|
77
|
6 |
|
} elseif ($token->test(Token::T_FLOAT)) { |
|
78
|
2 |
|
return (float)$token->getValue(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
throw new SyntaxErrorException( |
|
82
|
4 |
|
sprintf( |
|
83
|
4 |
|
'Invalid scalar token "%s" (%s)', |
|
84
|
4 |
|
$token->getValue(), |
|
85
|
4 |
|
$token->getName() |
|
86
|
4 |
|
) |
|
87
|
4 |
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|