Skip to content

Commit ae141fd

Browse files
committed
Add IsEmpty aggregation
1 parent 249fafe commit ae141fd

File tree

6 files changed

+98
-3
lines changed

6 files changed

+98
-3
lines changed

docs/roadmap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
| slice ||| |
7272
| nth ||| |
7373
| offsetsOf ||| |
74-
| isEmpty || | |
74+
| isEmpty || | |
7575
| union ||| |
7676
| sample ||| |
7777
| | | | |
@@ -80,7 +80,7 @@
8080
| ungroup ||| |
8181
| reduce ||| |
8282
| fold ||| |
83-
| count | | | |
83+
| count | | | |
8484
| sum ||| |
8585
| avg ||| |
8686
| min ||| |

src/Query/Aggregation/AbstractAggregation.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@
44
namespace TBolier\RethinkQL\Query\Aggregation;
55

66
use TBolier\RethinkQL\Query\AbstractQuery;
7+
use TBolier\RethinkQL\Query\QueryInterface;
78

89
abstract class AbstractAggregation extends AbstractQuery implements AggregationInterface
910
{
11+
/**
12+
* @inheritdoc
13+
*/
14+
public function isEmpty(): QueryInterface
15+
{
16+
return new IsEmpty($this->rethink, $this->message, $this);
17+
}
18+
1019
/**
1120
* @inheritdoc
1221
*/

src/Query/Aggregation/AggregationInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
interface AggregationInterface
1010
{
11+
/**
12+
* @return QueryInterface
13+
*/
14+
public function isEmpty(): QueryInterface;
15+
1116
/**
1217
* @param int $n
1318
* @return AggregationInterface

src/Query/Aggregation/IsEmpty.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\AbstractQuery;
8+
use TBolier\RethinkQL\Query\QueryInterface;
9+
use TBolier\RethinkQL\RethinkInterface;
10+
use TBolier\RethinkQL\Types\Term\TermType;
11+
12+
class IsEmpty extends AbstractQuery
13+
{
14+
/**
15+
* @var QueryInterface
16+
*/
17+
private $query;
18+
19+
/**
20+
* @param RethinkInterface $rethink
21+
* @param MessageInterface $message
22+
* @param QueryInterface $query
23+
*/
24+
public function __construct(RethinkInterface $rethink, MessageInterface $message, QueryInterface $query)
25+
{
26+
parent::__construct($rethink, $message);
27+
28+
$this->query = $query;
29+
$this->rethink = $rethink;
30+
$this->message = $message;
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function toArray(): array
37+
{
38+
return [
39+
TermType::IS_EMPTY,
40+
[
41+
$this->query->toArray(),
42+
],
43+
];
44+
}
45+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace TBolier\RethinkQL\IntegrationTest\Aggregation;
5+
6+
use TBolier\RethinkQL\IntegrationTest\Query\AbstractTableTest;
7+
8+
class IsEmptyTest extends AbstractTableTest
9+
{
10+
/**
11+
* @throws \Exception
12+
*/
13+
public function testIsEmpty()
14+
{
15+
$res = $this->r()
16+
->table('tabletest')
17+
->isEmpty()
18+
->run();
19+
20+
$this->assertTrue($res->getData());
21+
}
22+
23+
/**
24+
* @throws \Exception
25+
*/
26+
public function testIsNotEmpty()
27+
{
28+
$this->insertDocument(1);
29+
30+
$res = $this->r()
31+
->table('tabletest')
32+
->isEmpty()
33+
->run();
34+
35+
$this->assertFalse($res->getData());
36+
}
37+
}

test/integration/Query/CountTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace TBolier\RethinkQL\IntegrationTest\Query;
55

6-
use TBolier\RethinkQL\IntegrationTest\Query\AbstractTableTest;
76
use TBolier\RethinkQL\Response\ResponseInterface;
87

98
class CountTest extends AbstractTableTest

0 commit comments

Comments
 (0)