Skip to content

Commit 6ce8597

Browse files
committed
Add Range node generator and tests
1 parent 285543d commit 6ce8597

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace QueryTranslator\Languages\Galach\Generators\Native;
4+
5+
use LogicException;
6+
use QueryTranslator\Languages\Galach\Generators\Common\Visitor;
7+
use QueryTranslator\Languages\Galach\Values\Node\Term;
8+
use QueryTranslator\Languages\Galach\Values\Token\Range as RangeToken;
9+
use QueryTranslator\Values\Node;
10+
11+
/**
12+
* Range Node Visitor implementation.
13+
*/
14+
final class Range extends Visitor
15+
{
16+
public function accept(Node $node)
17+
{
18+
return $node instanceof Term && $node->token instanceof RangeToken;
19+
}
20+
21+
public function visit(Node $node, Visitor $subVisitor = null, $options = null)
22+
{
23+
if (!$node instanceof Term) {
24+
throw new LogicException(
25+
'Implementation accepts instance of Term Node'
26+
);
27+
}
28+
29+
$token = $node->token;
30+
31+
if (!$token instanceof RangeToken) {
32+
throw new LogicException(
33+
'Implementation accepts instance of Range Token'
34+
);
35+
}
36+
37+
$domainPrefix = '' === $token->domain ? '' : "{$token->domain}:";
38+
39+
switch ($token->type) {
40+
case RangeToken::TYPE_INCLUSIVE:
41+
return $domainPrefix . '[' . $token->rangeFrom . ' TO ' . $token->rangeTo . ']';
42+
43+
case RangeToken::TYPE_EXCLUSIVE:
44+
return $domainPrefix . '{' . $token->rangeFrom . ' TO ' . $token->rangeTo . '}';
45+
46+
default:
47+
throw new LogicException(sprintf('Range type %s is not supported', $token->type));
48+
}
49+
}
50+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace QueryTranslator\Tests\Galach\Generators\Native;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use QueryTranslator\Languages\Galach\Generators\Common\Visitor;
7+
use QueryTranslator\Languages\Galach\Generators\Native\Range;
8+
use QueryTranslator\Languages\Galach\Values\Node\Term;
9+
use QueryTranslator\Languages\Galach\Values\Token\Range as RangeToken;
10+
use QueryTranslator\Languages\Galach\Values\Token\Word;
11+
use QueryTranslator\Values\Node;
12+
13+
class RangeTest extends TestCase
14+
{
15+
/**
16+
* @var Visitor
17+
*/
18+
public $visitor;
19+
20+
protected function setUp()
21+
{
22+
$this->visitor = new Range();
23+
}
24+
25+
public function acceptDataprovider()
26+
{
27+
return [
28+
[true, new Term(new RangeToken('[a TO b]', 0, '', 'a', 'b', 'inclusive'))],
29+
[false, new Term(new Word('word', 0, '', 'a'))],
30+
];
31+
}
32+
33+
/**
34+
* @param bool $expected
35+
* @param Node $token
36+
*
37+
* @dataProvider acceptDataprovider
38+
*/
39+
public function testAccepts($expected, $node)
40+
{
41+
$this->assertSame($expected, $this->visitor->accept($node));
42+
}
43+
44+
public function visitDataprovider()
45+
{
46+
return [
47+
['[a TO b]', new Term(new RangeToken('[a TO b]', 0, '', 'a', 'b', 'inclusive'))],
48+
['{a TO b}', new Term(new RangeToken('{a TO b}', 0, '', 'a', 'b', 'exclusive'))],
49+
];
50+
}
51+
52+
/**
53+
* @param string $expected
54+
* @param Node $token
55+
*
56+
* @dataProvider visitDataprovider
57+
*/
58+
public function testVisit($expected, $node)
59+
{
60+
$this->assertSame($expected, $this->visitor->visit($node));
61+
}
62+
63+
public function testVisitUnknownTypeFails()
64+
{
65+
$this->expectException(\LogicException::class);
66+
$node = new Term(new RangeToken('{a TO b}', 0, '', 'a', 'b', 'unknown'));
67+
$this->visitor->visit($node);
68+
}
69+
}

0 commit comments

Comments
 (0)