-
-
Notifications
You must be signed in to change notification settings - Fork 57
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 #376 from nextras/aggregation_functions
Collection Aggregations 🎉
- Loading branch information
Showing
19 changed files
with
553 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Nextras\Orm library. | ||
* @license MIT | ||
* @link https://github.com/nextras/orm | ||
*/ | ||
|
||
namespace Nextras\Orm\Collection\Functions; | ||
|
||
|
||
class AvgAggregateFunction extends BaseAggregateFunction | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('AVG'); | ||
} | ||
|
||
|
||
protected function calculateAggregation(array $values) | ||
{ | ||
return \array_sum($values) / \count($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,66 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Nextras\Orm library. | ||
* @license MIT | ||
* @link https://github.com/nextras/orm | ||
*/ | ||
|
||
namespace Nextras\Orm\Collection\Functions; | ||
|
||
use Nextras\Dbal\QueryBuilder\QueryBuilder; | ||
use Nextras\Orm\Collection\Helpers\ArrayCollectionHelper; | ||
use Nextras\Orm\Collection\Helpers\DbalExpressionResult; | ||
use Nextras\Orm\Collection\Helpers\DbalQueryBuilderHelper; | ||
use Nextras\Orm\Entity\IEntity; | ||
use Nextras\Orm\InvalidArgumentException; | ||
|
||
|
||
abstract class BaseAggregateFunction implements IArrayFunction, IQueryBuilderFunction | ||
{ | ||
/** @var string */ | ||
private $sqlFunction; | ||
|
||
|
||
protected function __construct(string $sqlFunction) | ||
{ | ||
$this->sqlFunction = $sqlFunction; | ||
} | ||
|
||
|
||
/** | ||
* @param array<number> $values | ||
* @return number | ||
*/ | ||
abstract protected function calculateAggregation(array $values); | ||
|
||
|
||
public function processArrayExpression(ArrayCollectionHelper $helper, IEntity $entity, array $args) | ||
{ | ||
\assert(\count($args) === 1 && \is_string($args[0])); | ||
|
||
$valueReference = $helper->getValue($entity, $args[0]); | ||
if (!$valueReference->isMultiValue) { | ||
throw new InvalidArgumentException('Aggregation is not called over has many relationship.'); | ||
} | ||
\assert(\is_array($valueReference->value)); | ||
|
||
return $this->calculateAggregation($valueReference->value); | ||
} | ||
|
||
|
||
public function processQueryBuilderExpression( | ||
DbalQueryBuilderHelper $helper, | ||
QueryBuilder $builder, | ||
array $args | ||
): DbalExpressionResult | ||
{ | ||
\assert(\count($args) === 1 && \is_string($args[0])); | ||
|
||
$expression = $helper->processPropertyExpr($builder, $args[0]); | ||
return new DbalExpressionResult( | ||
["{$this->sqlFunction}(%ex)", $expression->args], | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Nextras\Orm library. | ||
* @license MIT | ||
* @link https://github.com/nextras/orm | ||
*/ | ||
|
||
namespace Nextras\Orm\Collection\Functions; | ||
|
||
|
||
class CountAggregateFunction extends BaseAggregateFunction | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('COUNT'); | ||
} | ||
|
||
|
||
protected function calculateAggregation(array $values) | ||
{ | ||
return \count($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
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,24 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Nextras\Orm library. | ||
* @license MIT | ||
* @link https://github.com/nextras/orm | ||
*/ | ||
|
||
namespace Nextras\Orm\Collection\Functions; | ||
|
||
|
||
class MaxAggregateFunction extends BaseAggregateFunction | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('MAX'); | ||
} | ||
|
||
|
||
protected function calculateAggregation(array $values) | ||
{ | ||
return \max($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,24 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Nextras\Orm library. | ||
* @license MIT | ||
* @link https://github.com/nextras/orm | ||
*/ | ||
|
||
namespace Nextras\Orm\Collection\Functions; | ||
|
||
|
||
class MinAggregateFunction extends BaseAggregateFunction | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('MIN'); | ||
} | ||
|
||
|
||
protected function calculateAggregation(array $values) | ||
{ | ||
return \min($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,24 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Nextras\Orm library. | ||
* @license MIT | ||
* @link https://github.com/nextras/orm | ||
*/ | ||
|
||
namespace Nextras\Orm\Collection\Functions; | ||
|
||
|
||
class SumAggregateFunction extends BaseAggregateFunction | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('SUM'); | ||
} | ||
|
||
|
||
protected function calculateAggregation(array $values) | ||
{ | ||
return \array_sum($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
Oops, something went wrong.