-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace QueryTranslator\Languages\Galach\Generators\Native; | ||
|
||
use LogicException; | ||
use QueryTranslator\Languages\Galach\Generators\Common\Visitor; | ||
use QueryTranslator\Languages\Galach\Values\Node\Term; | ||
use QueryTranslator\Languages\Galach\Values\Token\Range as RangeToken; | ||
use QueryTranslator\Values\Node; | ||
|
||
/** | ||
* Range Node Visitor implementation. | ||
*/ | ||
final class Range extends Visitor | ||
{ | ||
public function accept(Node $node) | ||
{ | ||
return $node instanceof Term && $node->token instanceof RangeToken; | ||
} | ||
|
||
public function visit(Node $node, Visitor $subVisitor = null, $options = null) | ||
{ | ||
if (!$node instanceof Term) { | ||
throw new LogicException( | ||
'Implementation accepts instance of Term Node' | ||
); | ||
} | ||
|
||
$token = $node->token; | ||
|
||
if (!$token instanceof RangeToken) { | ||
throw new LogicException( | ||
'Implementation accepts instance of Range Token' | ||
); | ||
} | ||
|
||
$domainPrefix = '' === $token->domain ? '' : "{$token->domain}:"; | ||
|
||
switch ($token->type) { | ||
case RangeToken::TYPE_INCLUSIVE: | ||
return $domainPrefix . '[' . $token->rangeFrom . ' TO ' . $token->rangeTo . ']'; | ||
|
||
case RangeToken::TYPE_EXCLUSIVE: | ||
return $domainPrefix . '{' . $token->rangeFrom . ' TO ' . $token->rangeTo . '}'; | ||
|
||
default: | ||
throw new LogicException(sprintf('Range type %s is not supported', $token->type)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace QueryTranslator\Tests\Galach\Generators\Native; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use QueryTranslator\Languages\Galach\Generators\Common\Visitor; | ||
use QueryTranslator\Languages\Galach\Generators\Native\Range; | ||
use QueryTranslator\Languages\Galach\Values\Node\Term; | ||
use QueryTranslator\Languages\Galach\Values\Token\Range as RangeToken; | ||
use QueryTranslator\Languages\Galach\Values\Token\Word; | ||
use QueryTranslator\Values\Node; | ||
|
||
class RangeTest extends TestCase | ||
{ | ||
/** | ||
* @var Visitor | ||
*/ | ||
public $visitor; | ||
|
||
protected function setUp() | ||
{ | ||
$this->visitor = new Range(); | ||
} | ||
|
||
public function acceptDataprovider() | ||
{ | ||
return [ | ||
[true, new Term(new RangeToken('[a TO b]', 0, '', 'a', 'b', 'inclusive'))], | ||
[false, new Term(new Word('word', 0, '', 'a'))], | ||
]; | ||
} | ||
|
||
/** | ||
* @param bool $expected | ||
* @param Node $token | ||
* | ||
* @dataProvider acceptDataprovider | ||
*/ | ||
public function testAccepts($expected, $node) | ||
{ | ||
$this->assertSame($expected, $this->visitor->accept($node)); | ||
} | ||
|
||
public function visitDataprovider() | ||
{ | ||
return [ | ||
['[a TO b]', new Term(new RangeToken('[a TO b]', 0, '', 'a', 'b', 'inclusive'))], | ||
['{a TO b}', new Term(new RangeToken('{a TO b}', 0, '', 'a', 'b', 'exclusive'))], | ||
]; | ||
} | ||
|
||
/** | ||
* @param string $expected | ||
* @param Node $token | ||
* | ||
* @dataProvider visitDataprovider | ||
*/ | ||
public function testVisit($expected, $node) | ||
{ | ||
$this->assertSame($expected, $this->visitor->visit($node)); | ||
} | ||
|
||
public function testVisitUnknownTypeFails() | ||
{ | ||
$this->expectException(\LogicException::class); | ||
$node = new Term(new RangeToken('{a TO b}', 0, '', 'a', 'b', 'unknown')); | ||
$this->visitor->visit($node); | ||
} | ||
} |