-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
collection: extract numeric aggregation logic to aggregator
- Loading branch information
Showing
8 changed files
with
112 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nextras\Orm\Collection\Aggregations; | ||
|
||
|
||
use Nextras\Dbal\QueryBuilder\QueryBuilder; | ||
use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult; | ||
|
||
|
||
/** | ||
* @internal | ||
* @implements IArrayAggregator<number> | ||
*/ | ||
class NumericAggregator implements IDbalAggregator, IArrayAggregator | ||
{ | ||
/** | ||
* @param callable(array<number>): (number|null) $arrayAggregation | ||
* @param literal-string $dbalAggregationFunction | ||
*/ | ||
public function __construct( | ||
private readonly mixed $arrayAggregation, | ||
private readonly string $dbalAggregationFunction, | ||
) | ||
{ | ||
} | ||
|
||
|
||
public function getAggregateKey(): string | ||
{ | ||
return '_' . $this->dbalAggregationFunction; | ||
} | ||
|
||
|
||
public function aggregateValues(array $values): mixed | ||
{ | ||
$cb = $this->arrayAggregation; | ||
return $cb($values); | ||
} | ||
|
||
|
||
public function aggregateExpression( | ||
QueryBuilder $queryBuilder, | ||
DbalExpressionResult $expression | ||
): DbalExpressionResult | ||
{ | ||
return new DbalExpressionResult( | ||
expression: "{$this->dbalAggregationFunction}($expression->expression)", | ||
args: $expression->args, | ||
joins: $expression->joins, | ||
groupBy: $expression->groupBy, | ||
isHavingClause: true, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters