Skip to content

Commit 249fafe

Browse files
committed
Add skip aggregation
1 parent 24772ec commit 249fafe

File tree

6 files changed

+151
-2
lines changed

6 files changed

+151
-2
lines changed

docs/roadmap.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
| withFields ||| |
6767
| concatMap ||| |
6868
| orderBy ||| |
69-
| skip || | |
69+
| skip || | |
7070
| limit ||| |
7171
| slice ||| |
7272
| nth ||| |

src/Query/Aggregation/AbstractAggregation.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public function limit($value): AggregationInterface
1515
return new Limit($this->rethink, $this->message, $this, $value);
1616
}
1717

18+
/**
19+
* @inheritdoc
20+
*/
21+
public function skip($value): AggregationInterface
22+
{
23+
return new Skip($this->rethink, $this->message, $this, $value);
24+
}
25+
1826
/**
1927
* @inheritdoc
2028
*/

src/Query/Aggregation/AggregationInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ interface AggregationInterface
1414
*/
1515
public function limit($n): AggregationInterface;
1616

17+
/**
18+
* @param int $n
19+
* @return AggregationInterface
20+
*/
21+
public function skip($n): AggregationInterface;
22+
1723
/**
1824
* @param mixed|QueryInterface $key
1925
* @return AggregationInterface

src/Query/Aggregation/Limit.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace TBolier\RethinkQL\Query\Aggregation;
55

66
use TBolier\RethinkQL\Message\MessageInterface;
7-
use TBolier\RethinkQL\Query\AbstractQuery;
87
use TBolier\RethinkQL\Query\QueryInterface;
98
use TBolier\RethinkQL\RethinkInterface;
109
use TBolier\RethinkQL\Types\Term\TermType;

src/Query/Aggregation/Skip.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace TBolier\RethinkQL\Query\Aggregation;
5+
6+
use TBolier\RethinkQL\Message\MessageInterface;
7+
use TBolier\RethinkQL\Query\QueryInterface;
8+
use TBolier\RethinkQL\RethinkInterface;
9+
use TBolier\RethinkQL\Types\Term\TermType;
10+
11+
class Skip extends AbstractAggregation
12+
{
13+
/**
14+
* @var int
15+
*/
16+
private $n;
17+
18+
/**
19+
* @var QueryInterface
20+
*/
21+
private $query;
22+
23+
/**
24+
* @param RethinkInterface $rethink
25+
* @param MessageInterface $message
26+
* @param QueryInterface $query
27+
* @param $n
28+
*/
29+
public function __construct(
30+
RethinkInterface $rethink,
31+
MessageInterface $message,
32+
QueryInterface $query,
33+
int $n
34+
) {
35+
parent::__construct($rethink, $message);
36+
37+
$this->query = $query;
38+
$this->n = $n;
39+
$this->rethink = $rethink;
40+
$this->message = $message;
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function toArray(): array
47+
{
48+
return [
49+
TermType::SKIP,
50+
[
51+
$this->query->toArray(),
52+
[
53+
TermType::DATUM,
54+
$this->n,
55+
],
56+
],
57+
];
58+
}
59+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace TBolier\RethinkQL\IntegrationTest\Aggregation;
5+
6+
use TBolier\RethinkQL\IntegrationTest\Query\AbstractTableTest;
7+
use TBolier\RethinkQL\Response\Cursor;
8+
use TBolier\RethinkQL\Response\ResponseInterface;
9+
10+
class SkipTest extends AbstractTableTest
11+
{
12+
/**
13+
* @return void
14+
* @throws \Exception
15+
*/
16+
public function testSkip(): void
17+
{
18+
$this->insertDocument(1);
19+
$this->insertDocument(2);
20+
$this->insertDocument(3);
21+
$this->insertDocument(4);
22+
$this->insertDocument(5);
23+
24+
/** @var Cursor $cursor */
25+
$cursor = $this->r()
26+
->table('tabletest')
27+
->skip(2)
28+
->run();
29+
30+
$this->assertCount(3, $cursor);
31+
}
32+
33+
/**
34+
* @return void
35+
* @throws \Exception
36+
*/
37+
public function testFilterAndSkip(): void
38+
{
39+
$this->insertDocument(1);
40+
$this->insertDocument(2);
41+
$this->insertDocument(3);
42+
$this->insertDocument(4);
43+
$this->insertDocument(5);
44+
45+
/** @var Cursor $cursor */
46+
$cursor = $this->r()
47+
->table('tabletest')
48+
->filter(['description' => 'A document description.'])
49+
->skip(2)
50+
->run();
51+
52+
$this->assertCount(3, $cursor);
53+
}
54+
55+
/**
56+
* @return void
57+
* @throws \Exception
58+
*/
59+
public function testSkipAndOrderBy(): void
60+
{
61+
$this->insertDocument(1);
62+
$this->insertDocument(2);
63+
$this->insertDocument(3);
64+
$this->insertDocument(4);
65+
$this->insertDocument(5);
66+
67+
/** @var ResponseInterface $res */
68+
$res = $this->r()
69+
->table('tabletest')
70+
->filter(['description' => 'A document description.'])
71+
->skip(2)
72+
->orderBy($this->r()->asc('id'))
73+
->run();
74+
75+
$this->assertArraySubset(['id' => 1], $res->getData()[0]);
76+
}
77+
}

0 commit comments

Comments
 (0)