Skip to content

Commit

Permalink
feat: 🎸 Implement New Feature: Aggregation (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw authored Jun 19, 2021
1 parent f08c1b0 commit cb4273f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,15 @@ Paginator::setMapping(string[] $mapping): $this
#### Arguments

- **`(string[])`** __*$mapping*__<br> An associative array that contains `$columnNameOrCursorKey => $fetchedFieldName`.


### Paginator::aggregated()

```php
Paginator::aggregated(bool $aggregated = true): $this
```

Declare that `HAVING` should be used instead of `WHERE` for aggregation.

### Paginator::setMaxResults()

Alias for `\Lampager\Paginator::limit()`.
Expand Down
19 changes: 18 additions & 1 deletion src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@

class Compiler
{
/**
* @var bool
*/
protected $aggregated;

/**
* Compiler constructor.
*
* @param bool $aggregated
*/
public function __construct($aggregated = false)
{
$this->aggregated = $aggregated;
}

/**
* Convert: Lampager Query -> Doctrine Query Builder
*
Expand Down Expand Up @@ -65,7 +80,9 @@ protected function compileWhere(QueryBuilder $builder, Select $select)
}

if ($orX) {
$builder->andWhere($builder->expr()->orX(...$orX));
$this->aggregated
? $builder->andHaving($builder->expr()->orX(...$orX))
: $builder->andWhere($builder->expr()->orX(...$orX));
}

return $this;
Expand Down
19 changes: 18 additions & 1 deletion src/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class Paginator extends BasePaginator
{
use HasProcessor;

/**
* @var bool
*/
public $aggregated;

/**
* @return static
*/
Expand Down Expand Up @@ -50,14 +55,26 @@ public function setMapping(array $mapping)
return $this;
}

/**
* Declare that HAVING should be used instead of WHERE.
*
* @param bool $aggregated
* @return $this
*/
public function aggregated($aggregated = true)
{
$this->aggregated = $aggregated;
return $this;
}

/**
* Convert: Lampager Query -> Doctrine Query
*
* @return DoctrineQuery
*/
public function transform(Query $query)
{
$compiler = new Compiler();
$compiler = new Compiler($this->aggregated);

return $compiler
->compile($this->builder, $query->selectOrUnionAll())
Expand Down
39 changes: 39 additions & 0 deletions tests/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,43 @@ public function testArrayResult()
)
);
}

/**
* @test
*/
public function testAggregatedPagination()
{
$this->assertResultSame(
[
'records' => [
[
'minId' => '1',
'maxId' => '5',
'groupedUpdatedAt' => '2017-01-01 10:00:00',
],
],
'hasPrevious' => null,
'previousCursor' => null,
'hasNext' => true,
'nextCursor' => [
'groupedUpdatedAt' => '2017-01-01 11:00:00',
'maxId' => '2',
],
],
Paginator::create(
$this->posts
->createQueryBuilder('p')
->select('min(p.id) as minId, max(p.id) as maxId', "CONCAT('', p.updatedAt) as groupedUpdatedAt")
->groupBy('p.updatedAt')
)
->forward()->setMaxResults(1)
->aggregated()
->orderBy('groupedUpdatedAt')
->orderBy('maxId')
->setMapping([
'maxId' => 'minId',
])
->paginate([], Query::HYDRATE_ARRAY)
);
}
}

0 comments on commit cb4273f

Please sign in to comment.