1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Apicart\FQL\Generator\Native; |
4
|
|
|
|
5
|
|
|
use Apicart\FQL\Generator\Common\AbstractVisitor; |
6
|
|
|
use Apicart\FQL\Token\Node\Term; |
7
|
|
|
use Apicart\FQL\Token\Token\Range as RangeToken; |
8
|
|
|
use Apicart\FQL\Value\AbstractNode; |
9
|
|
|
use LogicException; |
10
|
|
|
|
11
|
|
|
final class Range extends AbstractVisitor |
12
|
|
|
{ |
13
|
|
|
|
14
|
2 |
|
public function accept(AbstractNode $node): bool |
15
|
|
|
{ |
16
|
2 |
|
return $node instanceof Term && $node->getToken() instanceof RangeToken; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
|
20
|
8 |
|
public function visit(AbstractNode $node, ?AbstractVisitor $subVisitor = null, ?array $options = null): string |
21
|
|
|
{ |
22
|
8 |
|
if (! $node instanceof Term) { |
23
|
1 |
|
throw new LogicException('Implementation accepts instance of Term Node'); |
24
|
|
|
} |
25
|
7 |
|
$token = $node->getToken(); |
26
|
7 |
|
if (! $token instanceof RangeToken) { |
27
|
1 |
|
throw new LogicException('Implementation accepts instance of Range Token'); |
28
|
|
|
} |
29
|
6 |
|
$domainPrefix = $token->getDomain() === '' ? '' : "{$token->getDomain()}:"; |
30
|
|
|
return $domainPrefix . |
31
|
6 |
|
$this->buildRangeStart($token) . |
32
|
5 |
|
' TO ' . |
33
|
5 |
|
$this->buildRangeEnd($token); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
6 |
|
private function buildRangeStart(RangeToken $token): string |
38
|
|
|
{ |
39
|
6 |
|
switch ($token->getStartType()) { |
40
|
6 |
|
case RangeToken::TYPE_INCLUSIVE: |
41
|
3 |
|
return '[' . $token->getStartValue(); |
42
|
3 |
|
case RangeToken::TYPE_EXCLUSIVE: |
43
|
2 |
|
return '{' . $token->getStartValue(); |
44
|
|
|
default: |
45
|
1 |
|
throw new LogicException(sprintf('Range start type %s is not supported', $token->getStartType())); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
5 |
|
private function buildRangeEnd(RangeToken $token): string |
51
|
|
|
{ |
52
|
5 |
|
switch ($token->getEndType()) { |
53
|
5 |
|
case RangeToken::TYPE_INCLUSIVE: |
54
|
2 |
|
return $token->getEndValue() . ']'; |
55
|
3 |
|
case RangeToken::TYPE_EXCLUSIVE: |
56
|
2 |
|
return $token->getEndValue() . '}'; |
57
|
|
|
default: |
58
|
1 |
|
throw new LogicException(sprintf('Range end type %s is not supported', $token->getEndType())); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|