-
-
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: fix array improperly processing join conditions as indepe…
…ndent
- Loading branch information
Showing
10 changed files
with
274 additions
and
12 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,89 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nextras\Orm\Collection\Aggregations; | ||
|
||
|
||
use Nextras\Dbal\QueryBuilder\QueryBuilder; | ||
use Nextras\Orm\Collection\Helpers\DbalExpressionResult; | ||
use Nextras\Orm\Collection\Helpers\DbalJoinEntry; | ||
use Nextras\Orm\Exception\InvalidStateException; | ||
|
||
|
||
/** | ||
* @implements IArrayAggregator<bool> | ||
*/ | ||
class CountAggregator implements IDbalAggregator, IArrayAggregator | ||
{ | ||
/** @var int */ | ||
private $atLeast; | ||
|
||
/** @var int */ | ||
private $atMost; | ||
|
||
/** @var string */ | ||
private $aggregateKey; | ||
|
||
|
||
public function __construct( | ||
int $atLeast, | ||
int $atMost, | ||
string $aggregateKey = 'count' | ||
) | ||
{ | ||
$this->atLeast = $atLeast; | ||
$this->atMost = $atMost; | ||
$this->aggregateKey = $aggregateKey; | ||
} | ||
|
||
|
||
public function getAggregateKey(): string | ||
{ | ||
return $this->aggregateKey; | ||
} | ||
|
||
|
||
public function aggregateValues(array $values): bool | ||
{ | ||
$count = count(array_filter($values)); | ||
return $count >= $this->atLeast && $count <= $this->atMost; | ||
} | ||
|
||
|
||
public function aggregateExpression( | ||
QueryBuilder $queryBuilder, | ||
DbalExpressionResult $expression | ||
): DbalExpressionResult | ||
{ | ||
$joinExpression = $expression->expression; | ||
|
||
$joinArgs = $expression->args; | ||
$joins = $expression->joins; | ||
$join = array_pop($joins); | ||
if ($join === null) { | ||
throw new InvalidStateException('Aggregation applied over expression without a relationship'); | ||
} | ||
|
||
$joins[] = new DbalJoinEntry( | ||
$join->toExpression, | ||
$join->toArgs, | ||
$join->toAlias, | ||
"($join->onExpression) AND $joinExpression", | ||
array_merge($join->onArgs, $joinArgs), | ||
$join->conventions | ||
); | ||
|
||
$primaryKey = $join->conventions->getStoragePrimaryKey()[0]; | ||
$groupBy = $expression->groupBy; | ||
|
||
return new DbalExpressionResult( | ||
'COUNT(%table.%column) >= %i AND COUNT(%table.%column) <= %i', | ||
[$join->toAlias, $primaryKey, $this->atLeast, $join->toAlias, $primaryKey, $this->atMost], | ||
$joins, | ||
$groupBy, | ||
null, | ||
true, | ||
null, | ||
null | ||
); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
|
||
interface IAggregator | ||
{ | ||
public function getAggregateKey(): string; | ||
} |
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
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