Skip to content

Commit

Permalink
unify Aggregator interface to a single one
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 11, 2024
1 parent f5994ba commit 3f4bdbd
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,28 @@
use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;


interface IDbalAggregator extends IAggregator
/**
* @template T The type of the aggregation result value.
*/
interface Aggregator
{
/**
* Returns a grouping "key" used to join multiple conditions/joins together.
*
* In SQL, it is used as table alias suffix.
*
* @return literal-string
*/
public function getAggregateKey(): string;


/**
* @param array<T> $values
* @return T|null
*/
public function aggregateValues(array $values);


public function aggregateExpression(
QueryBuilder $queryBuilder,
DbalExpressionResult $expression,
Expand Down
4 changes: 2 additions & 2 deletions src/Collection/Aggregations/AnyAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@


/**
* @implements IArrayAggregator<bool>
* @implements Aggregator<bool>
*/
class AnyAggregator implements IDbalAggregator, IArrayAggregator
class AnyAggregator implements Aggregator
{
/** @var literal-string */
private string $aggregateKey;
Expand Down
4 changes: 2 additions & 2 deletions src/Collection/Aggregations/CountAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@


/**
* @implements IArrayAggregator<bool>
* @implements Aggregator<bool>
*/
class CountAggregator implements IDbalAggregator, IArrayAggregator
class CountAggregator implements Aggregator
{
private int $atLeast;

Expand Down
12 changes: 0 additions & 12 deletions src/Collection/Aggregations/IAggregator.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Collection/Aggregations/IArrayAggregator.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Collection/Aggregations/NoneAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@


/**
* @implements IArrayAggregator<bool>
* @implements Aggregator<bool>
*/
class NoneAggregator implements IDbalAggregator, IArrayAggregator
class NoneAggregator implements Aggregator
{
/** @var literal-string */
private string $aggregateKey;
Expand Down
4 changes: 2 additions & 2 deletions src/Collection/Aggregations/NumericAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

/**
* @internal
* @implements IArrayAggregator<number>
* @implements Aggregator<number>
*/
class NumericAggregator implements IDbalAggregator, IArrayAggregator
class NumericAggregator implements Aggregator
{
/**
* @param callable(array<number>): (number|null) $arrayAggregation
Expand Down
7 changes: 3 additions & 4 deletions src/Collection/Functions/BaseCompareFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@


use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Aggregations\IArrayAggregator;
use Nextras\Orm\Collection\Aggregations\IDbalAggregator;
use Nextras\Orm\Collection\Aggregations\Aggregator;
use Nextras\Orm\Collection\Expression\ExpressionContext;
use Nextras\Orm\Collection\Functions\Result\ArrayExpressionResult;
use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
Expand All @@ -22,7 +21,7 @@ public function processArrayExpression(
ArrayCollectionHelper $helper,
IEntity $entity,
array $args,
?IArrayAggregator $aggregator = null,
?Aggregator $aggregator = null,
): ArrayExpressionResult
{
assert(count($args) === 2);
Expand Down Expand Up @@ -61,7 +60,7 @@ public function processDbalExpression(
QueryBuilder $builder,
array $args,
ExpressionContext $context,
?IDbalAggregator $aggregator = null,
?Aggregator $aggregator = null,
): DbalExpressionResult
{
assert(count($args) === 2);
Expand Down
7 changes: 3 additions & 4 deletions src/Collection/Functions/BaseNumericAggregateFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@


use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Aggregations\IArrayAggregator;
use Nextras\Orm\Collection\Aggregations\IDbalAggregator;
use Nextras\Orm\Collection\Aggregations\Aggregator;
use Nextras\Orm\Collection\Aggregations\NumericAggregator;
use Nextras\Orm\Collection\Expression\ExpressionContext;
use Nextras\Orm\Collection\Functions\Result\ArrayExpressionResult;
Expand Down Expand Up @@ -34,7 +33,7 @@ public function processArrayExpression(
ArrayCollectionHelper $helper,
IEntity $entity,
array $args,
?IArrayAggregator $aggregator = null,
?Aggregator $aggregator = null,
): ArrayExpressionResult
{
assert(count($args) === 1 && is_string($args[0]));
Expand All @@ -56,7 +55,7 @@ public function processDbalExpression(
QueryBuilder $builder,
array $args,
ExpressionContext $context,
?IDbalAggregator $aggregator = null,
?Aggregator $aggregator = null,
): DbalExpressionResult
{
assert(count($args) === 1 && is_string($args[0]));
Expand Down
10 changes: 5 additions & 5 deletions src/Collection/Functions/CollectionFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@


use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Aggregations\IArrayAggregator;
use Nextras\Orm\Collection\Aggregations\IDbalAggregator;
use Nextras\Orm\Collection\Aggregations\Aggregator;
use Nextras\Orm\Collection\Expression\ExpressionContext;
use Nextras\Orm\Collection\Functions\Result\ArrayExpressionResult;
use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
Expand All @@ -27,26 +26,27 @@ interface CollectionFunction
* execution.
* Usually returns a boolean for filtering evaluation.
* @param array<mixed> $args
* @param IArrayAggregator<mixed>|null $aggregator
* @param Aggregator<mixed>|null $aggregator
*/
public function processArrayExpression(
ArrayCollectionHelper $helper,
IEntity $entity,
array $args,
?IArrayAggregator $aggregator = null,
?Aggregator $aggregator = null,
): ArrayExpressionResult;


/**
* Returns true if entity should stay in the result collection; the condition is evaluated in database and this
* method just returns appropriate Nextras Dbal's filtering expression for passed args.
* @param array<int|string, mixed> $args
* @param Aggregator<mixed>|null $aggregator
*/
public function processDbalExpression(
DbalQueryBuilderHelper $helper,
QueryBuilder $builder,
array $args,
ExpressionContext $context,
?IDbalAggregator $aggregator = null,
?Aggregator $aggregator = null,
): DbalExpressionResult;
}
7 changes: 3 additions & 4 deletions src/Collection/Functions/CompareLikeFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

use Nette\Utils\Strings;
use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Aggregations\IArrayAggregator;
use Nextras\Orm\Collection\Aggregations\IDbalAggregator;
use Nextras\Orm\Collection\Aggregations\Aggregator;
use Nextras\Orm\Collection\Expression\ExpressionContext;
use Nextras\Orm\Collection\Expression\LikeExpression;
use Nextras\Orm\Collection\Functions\Result\ArrayExpressionResult;
Expand All @@ -25,7 +24,7 @@ public function processArrayExpression(
ArrayCollectionHelper $helper,
IEntity $entity,
array $args,
?IArrayAggregator $aggregator = null,
?Aggregator $aggregator = null,
): ArrayExpressionResult
{
assert(count($args) === 2);
Expand Down Expand Up @@ -66,7 +65,7 @@ public function processDbalExpression(
QueryBuilder $builder,
array $args,
ExpressionContext $context,
?IDbalAggregator $aggregator = null,
?Aggregator $aggregator = null,
): DbalExpressionResult
{
assert(count($args) === 2);
Expand Down
11 changes: 4 additions & 7 deletions src/Collection/Functions/ConjunctionOperatorFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@


use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Aggregations\IArrayAggregator;
use Nextras\Orm\Collection\Aggregations\IDbalAggregator;
use Nextras\Orm\Collection\Aggregations\Aggregator;
use Nextras\Orm\Collection\Expression\ExpressionContext;
use Nextras\Orm\Collection\Functions\Result\ArrayExpressionResult;
use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
use Nextras\Orm\Collection\Helpers\ArrayCollectionHelper;
use Nextras\Orm\Collection\Helpers\ConditionParser;
use Nextras\Orm\Collection\Helpers\DbalQueryBuilderHelper;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Exception\InvalidArgumentException;
use Nextras\Orm\Exception\InvalidStateException;


Expand All @@ -33,13 +31,12 @@ public function processArrayExpression(
ArrayCollectionHelper $helper,
IEntity $entity,
array $args,
?IArrayAggregator $aggregator = null,
?Aggregator $aggregator = null,
): ArrayExpressionResult
{
[$normalized, $newAggregator] = $this->normalizeFunctions($args);
if ($newAggregator !== null) {
if ($aggregator !== null) throw new InvalidStateException("Cannot apply two aggregations simultaneously.");
if (!$newAggregator instanceof IArrayAggregator) throw new InvalidArgumentException('Array requires aggregation instance of IArrayAggregator.');
$aggregator = $newAggregator;
}

Expand All @@ -50,7 +47,7 @@ public function processArrayExpression(
* aggregation.
*/

/** @var array<string, IArrayAggregator<bool>> $aggregators */
/** @var array<string, Aggregator<bool>> $aggregators */
$aggregators = [];
$values = [];
$sizes = [];
Expand Down Expand Up @@ -107,7 +104,7 @@ public function processDbalExpression(
QueryBuilder $builder,
array $args,
ExpressionContext $context,
?IDbalAggregator $aggregator = null,
?Aggregator $aggregator = null,
): DbalExpressionResult
{
return $this->processQueryBuilderExpressionWithModifier(
Expand Down
11 changes: 4 additions & 7 deletions src/Collection/Functions/DisjunctionOperatorFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@


use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Aggregations\IArrayAggregator;
use Nextras\Orm\Collection\Aggregations\IDbalAggregator;
use Nextras\Orm\Collection\Aggregations\Aggregator;
use Nextras\Orm\Collection\Expression\ExpressionContext;
use Nextras\Orm\Collection\Functions\Result\ArrayExpressionResult;
use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
use Nextras\Orm\Collection\Helpers\ArrayCollectionHelper;
use Nextras\Orm\Collection\Helpers\ConditionParser;
use Nextras\Orm\Collection\Helpers\DbalQueryBuilderHelper;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Exception\InvalidArgumentException;
use Nextras\Orm\Exception\InvalidStateException;


Expand All @@ -33,17 +31,16 @@ public function processArrayExpression(
ArrayCollectionHelper $helper,
IEntity $entity,
array $args,
?IArrayAggregator $aggregator = null,
?Aggregator $aggregator = null,
): ArrayExpressionResult
{
[$normalized, $newAggregator] = $this->normalizeFunctions($args);
if ($newAggregator !== null) {
if ($aggregator !== null) throw new InvalidStateException("Cannot apply two aggregations simultaneously.");
if (!$newAggregator instanceof IArrayAggregator) throw new InvalidArgumentException('Array requires aggregation instance of IArrayAggregator.');
$aggregator = $newAggregator;
}

/** @var array<string, IArrayAggregator<bool>> $aggregators */
/** @var array<string, Aggregator<bool>> $aggregators */
$aggregators = [];
$values = [];
$sizes = [];
Expand Down Expand Up @@ -100,7 +97,7 @@ public function processDbalExpression(
QueryBuilder $builder,
array $args,
ExpressionContext $context,
?IDbalAggregator $aggregator = null,
?Aggregator $aggregator = null,
): DbalExpressionResult
{
return $this->processQueryBuilderExpressionWithModifier(
Expand Down
Loading

0 comments on commit 3f4bdbd

Please sign in to comment.