Skip to content

Commit

Permalink
Add between operator #56 (#68)
Browse files Browse the repository at this point in the history
* Add between operator

* Add strict type on between operator

* Add strict type for $options

* Add strict type for $options var

* Fix auto completion for between
  • Loading branch information
Th3Mouk authored and tbolier committed Sep 9, 2018
1 parent 8cb90ee commit 59fa3dd
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 1 deletion.
72 changes: 72 additions & 0 deletions src/Query/Operation/Between.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
declare(strict_types = 1);

namespace TBolier\RethinkQL\Query\Operation;

use TBolier\RethinkQL\Query\AbstractQuery;
use TBolier\RethinkQL\Query\Aggregation\AggregationTrait;
use TBolier\RethinkQL\Query\QueryInterface;
use TBolier\RethinkQL\Query\Transformation\TransformationTrait;
use TBolier\RethinkQL\RethinkInterface;
use TBolier\RethinkQL\Types\Term\TermType;

class Between extends AbstractQuery
{
use AggregationTrait;
use OperationTrait;
use TransformationTrait;

/**
* @var QueryInterface
*/
private $query;

/**
* @var string
*/
private $min;

/**
* @var string
*/
private $max;

/**
* @var array|null
*/
private $options;

public function __construct(
RethinkInterface $rethink,
QueryInterface $query,
$min,
$max,
array $options = null
) {
parent::__construct($rethink);

$this->rethink = $rethink;
$this->query = $query;
$this->min = $min;
$this->max = $max;
$this->options = $options;
}

public function toArray(): array
{
$args = [
$this->query->toArray(),
$this->min,
$this->max,
];

if ($this->options !== null) {
$args = array_merge($args, [$this->options]);
}

return [
TermType::BETWEEN,
$args,
];
}
}
8 changes: 7 additions & 1 deletion src/Query/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace TBolier\RethinkQL\Query;

use TBolier\RethinkQL\Query\Aggregation\AggregationTrait;
use TBolier\RethinkQL\Query\Operation\Between;
use TBolier\RethinkQL\Query\Manipulation\HasFields;
use TBolier\RethinkQL\Query\Manipulation\ManipulationTrait;
use TBolier\RethinkQL\Query\Operation\Get;
Expand Down Expand Up @@ -32,7 +33,7 @@ public function __construct(string $name, RethinkInterface $rethink)
parent::__construct($rethink);

$this->rethink = $rethink;


$this->query = [
TermType::TABLE,
Expand Down Expand Up @@ -67,6 +68,11 @@ public function indexRename(string $oldValue, string $newValue): AbstractQuery
return new IndexRename($this->rethink, $this, $oldValue, $newValue);
}

public function between($min, $max, array $options = null): Between
{
return new Between($this->rethink, $this, $min, $max, $options);
}

public function hasFields(...$keys)
{
return new HasFields($this->rethink, $this, $keys);
Expand Down
90 changes: 90 additions & 0 deletions test/integration/Query/BetweenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);

namespace TBolier\RethinkQL\IntegrationTest\Query;

use TBolier\RethinkQL\Response\Cursor;
use TBolier\RethinkQL\Response\ResponseInterface;
use TBolier\RethinkQL\Types\Term\TermType;

class BetweenTest extends AbstractTableTest
{
/**
* @throws \Exception
*/
public function testBetweenMin()
{
$this->insertDocument(1);
$this->insertDocument(3);

/** @var Cursor $cursor */
$cursor = $this->r()
->table('tabletest')
->between(1, 2)
->run();

/** @var array $array */
$array = $cursor->current();

$this->assertInstanceOf(\Iterator::class, $cursor);
$this->assertEquals(1, $cursor->count());
$this->assertArraySubset(['title' => 'Test document 1'], $array);
}

/**
* @throws \Exception
*/
public function testBetweenMax()
{
$this->insertDocument(1);
$this->insertDocument(3);

/** @var Cursor $cursor */
$cursor = $this->r()
->table('tabletest')
->between(2, 3)
->run();

$this->assertInstanceOf(\Iterator::class, $cursor);
$this->assertCount(0, $cursor);
}

/**
* @throws \Exception
*/
public function testBetweenMultiple()
{
$this->insertDocument(1);
$this->insertDocument(2);
$this->insertDocument(3);
$this->insertDocument(4);
$this->insertDocument(5);

/** @var Cursor $cursor */
$cursor = $this->r()
->table('tabletest')
->between(2, 4)
->run();

$this->assertInstanceOf(\Iterator::class, $cursor);
$this->assertCount(2, $cursor);
}

/**
* @return void
* @throws \Exception
*/
public function testBetweenNoResult(): void
{
$this->insertDocument('stringId');

/** @var Cursor $cursor */
$cursor = $this->r()
->table('tabletest')
->between(2, 4)
->run();

$this->assertInstanceOf(\Iterator::class, $cursor);
$this->assertCount(0, $cursor);
}
}

0 comments on commit 59fa3dd

Please sign in to comment.