-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #546 from nextras/join_array_dependent
ArrayCollection processes conditions as row-dependent in multi-value joins
- Loading branch information
Showing
30 changed files
with
613 additions
and
282 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nextras\Orm\Collection\Aggregations; | ||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nextras\Orm\Collection\Aggregations; | ||
|
||
|
||
/** | ||
* @template T | ||
*/ | ||
interface IArrayAggregator extends IAggregator | ||
{ | ||
/** | ||
* @param array<T> $values | ||
* @return T | ||
*/ | ||
function aggregateValues(array $values); | ||
} |
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,16 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nextras\Orm\Collection\Aggregations; | ||
|
||
|
||
use Nextras\Dbal\QueryBuilder\QueryBuilder; | ||
use Nextras\Orm\Collection\Helpers\DbalExpressionResult; | ||
|
||
|
||
interface IDbalAggregator extends IAggregator | ||
{ | ||
public function aggregateExpression( | ||
QueryBuilder $queryBuilder, | ||
DbalExpressionResult $expressionResult | ||
): DbalExpressionResult; | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.