| @@ 22-73 (lines=52) @@ | ||
| 19 | use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter; |
|
| 20 | use Happyr\DoctrineSpecification\Operand\Operand; |
|
| 21 | ||
| 22 | final class Avg implements Operand |
|
| 23 | { |
|
| 24 | /** |
|
| 25 | * @var Operand|string |
|
| 26 | */ |
|
| 27 | private $field; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @var bool |
|
| 31 | */ |
|
| 32 | private $distinct; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * @param Operand|string $field |
|
| 36 | * @param bool $distinct |
|
| 37 | */ |
|
| 38 | public function __construct($field, bool $distinct = false) |
|
| 39 | { |
|
| 40 | $this->field = $field; |
|
| 41 | $this->distinct = $distinct; |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * @param QueryBuilder $qb |
|
| 46 | * @param string $context |
|
| 47 | * |
|
| 48 | * @return string |
|
| 49 | */ |
|
| 50 | public function transform(QueryBuilder $qb, string $context): string |
|
| 51 | { |
|
| 52 | $field = ArgumentToOperandConverter::toField($this->field); |
|
| 53 | $field = $field->transform($qb, $context); |
|
| 54 | ||
| 55 | $expression = ''; |
|
| 56 | ||
| 57 | if ($this->distinct) { |
|
| 58 | $expression = 'DISTINCT '; |
|
| 59 | } |
|
| 60 | ||
| 61 | return sprintf('AVG(%s%s)', $expression, $field); |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * @param mixed[]|object $candidate |
|
| 66 | */ |
|
| 67 | public function execute($candidate): void |
|
| 68 | { |
|
| 69 | throw new OperandNotExecuteException( |
|
| 70 | sprintf('The operand "%s" cannot be executed for a single candidate.', self::class) |
|
| 71 | ); |
|
| 72 | } |
|
| 73 | } |
|
| 74 | ||
| @@ 22-73 (lines=52) @@ | ||
| 19 | use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter; |
|
| 20 | use Happyr\DoctrineSpecification\Operand\Operand; |
|
| 21 | ||
| 22 | final class Count implements Operand |
|
| 23 | { |
|
| 24 | /** |
|
| 25 | * @var Operand|string |
|
| 26 | */ |
|
| 27 | private $field; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @var bool |
|
| 31 | */ |
|
| 32 | private $distinct; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * @param Operand|string $field |
|
| 36 | * @param bool $distinct |
|
| 37 | */ |
|
| 38 | public function __construct($field, bool $distinct = false) |
|
| 39 | { |
|
| 40 | $this->field = $field; |
|
| 41 | $this->distinct = $distinct; |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * @param QueryBuilder $qb |
|
| 46 | * @param string $context |
|
| 47 | * |
|
| 48 | * @return string |
|
| 49 | */ |
|
| 50 | public function transform(QueryBuilder $qb, string $context): string |
|
| 51 | { |
|
| 52 | $field = ArgumentToOperandConverter::toField($this->field); |
|
| 53 | $field = $field->transform($qb, $context); |
|
| 54 | ||
| 55 | $expression = ''; |
|
| 56 | ||
| 57 | if ($this->distinct) { |
|
| 58 | $expression = 'DISTINCT '; |
|
| 59 | } |
|
| 60 | ||
| 61 | return sprintf('COUNT(%s%s)', $expression, $field); |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * @param mixed[]|object $candidate |
|
| 66 | */ |
|
| 67 | public function execute($candidate): void |
|
| 68 | { |
|
| 69 | throw new OperandNotExecuteException( |
|
| 70 | sprintf('The operand "%s" cannot be executed for a single candidate.', self::class) |
|
| 71 | ); |
|
| 72 | } |
|
| 73 | } |
|
| 74 | ||
| @@ 22-73 (lines=52) @@ | ||
| 19 | use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter; |
|
| 20 | use Happyr\DoctrineSpecification\Operand\Operand; |
|
| 21 | ||
| 22 | final class Max implements Operand |
|
| 23 | { |
|
| 24 | /** |
|
| 25 | * @var Operand|string |
|
| 26 | */ |
|
| 27 | private $field; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @var bool |
|
| 31 | */ |
|
| 32 | private $distinct; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * @param Operand|string $field |
|
| 36 | * @param bool $distinct |
|
| 37 | */ |
|
| 38 | public function __construct($field, bool $distinct = false) |
|
| 39 | { |
|
| 40 | $this->field = $field; |
|
| 41 | $this->distinct = $distinct; |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * @param QueryBuilder $qb |
|
| 46 | * @param string $context |
|
| 47 | * |
|
| 48 | * @return string |
|
| 49 | */ |
|
| 50 | public function transform(QueryBuilder $qb, string $context): string |
|
| 51 | { |
|
| 52 | $field = ArgumentToOperandConverter::toField($this->field); |
|
| 53 | $field = $field->transform($qb, $context); |
|
| 54 | ||
| 55 | $expression = ''; |
|
| 56 | ||
| 57 | if ($this->distinct) { |
|
| 58 | $expression = 'DISTINCT '; |
|
| 59 | } |
|
| 60 | ||
| 61 | return sprintf('MAX(%s%s)', $expression, $field); |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * @param mixed[]|object $candidate |
|
| 66 | */ |
|
| 67 | public function execute($candidate): void |
|
| 68 | { |
|
| 69 | throw new OperandNotExecuteException( |
|
| 70 | sprintf('The operand "%s" cannot be executed for a single candidate.', self::class) |
|
| 71 | ); |
|
| 72 | } |
|
| 73 | } |
|
| 74 | ||
| @@ 22-73 (lines=52) @@ | ||
| 19 | use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter; |
|
| 20 | use Happyr\DoctrineSpecification\Operand\Operand; |
|
| 21 | ||
| 22 | final class Min implements Operand |
|
| 23 | { |
|
| 24 | /** |
|
| 25 | * @var Operand|string |
|
| 26 | */ |
|
| 27 | private $field; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @var bool |
|
| 31 | */ |
|
| 32 | private $distinct; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * @param Operand|string $field |
|
| 36 | * @param bool $distinct |
|
| 37 | */ |
|
| 38 | public function __construct($field, bool $distinct = false) |
|
| 39 | { |
|
| 40 | $this->field = $field; |
|
| 41 | $this->distinct = $distinct; |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * @param QueryBuilder $qb |
|
| 46 | * @param string $context |
|
| 47 | * |
|
| 48 | * @return string |
|
| 49 | */ |
|
| 50 | public function transform(QueryBuilder $qb, string $context): string |
|
| 51 | { |
|
| 52 | $field = ArgumentToOperandConverter::toField($this->field); |
|
| 53 | $field = $field->transform($qb, $context); |
|
| 54 | ||
| 55 | $expression = ''; |
|
| 56 | ||
| 57 | if ($this->distinct) { |
|
| 58 | $expression = 'DISTINCT '; |
|
| 59 | } |
|
| 60 | ||
| 61 | return sprintf('MIN(%s%s)', $expression, $field); |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * @param mixed[]|object $candidate |
|
| 66 | */ |
|
| 67 | public function execute($candidate): void |
|
| 68 | { |
|
| 69 | throw new OperandNotExecuteException( |
|
| 70 | sprintf('The operand "%s" cannot be executed for a single candidate.', self::class) |
|
| 71 | ); |
|
| 72 | } |
|
| 73 | } |
|
| 74 | ||
| @@ 22-73 (lines=52) @@ | ||
| 19 | use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter; |
|
| 20 | use Happyr\DoctrineSpecification\Operand\Operand; |
|
| 21 | ||
| 22 | final class Sum implements Operand |
|
| 23 | { |
|
| 24 | /** |
|
| 25 | * @var Operand|string |
|
| 26 | */ |
|
| 27 | private $field; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @var bool |
|
| 31 | */ |
|
| 32 | private $distinct; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * @param Operand|string $field |
|
| 36 | * @param bool $distinct |
|
| 37 | */ |
|
| 38 | public function __construct($field, bool $distinct = false) |
|
| 39 | { |
|
| 40 | $this->field = $field; |
|
| 41 | $this->distinct = $distinct; |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * @param QueryBuilder $qb |
|
| 46 | * @param string $context |
|
| 47 | * |
|
| 48 | * @return string |
|
| 49 | */ |
|
| 50 | public function transform(QueryBuilder $qb, string $context): string |
|
| 51 | { |
|
| 52 | $field = ArgumentToOperandConverter::toField($this->field); |
|
| 53 | $field = $field->transform($qb, $context); |
|
| 54 | ||
| 55 | $expression = ''; |
|
| 56 | ||
| 57 | if ($this->distinct) { |
|
| 58 | $expression = 'DISTINCT '; |
|
| 59 | } |
|
| 60 | ||
| 61 | return sprintf('SUM(%s%s)', $expression, $field); |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * @param mixed[]|object $candidate |
|
| 66 | */ |
|
| 67 | public function execute($candidate): void |
|
| 68 | { |
|
| 69 | throw new OperandNotExecuteException( |
|
| 70 | sprintf('The operand "%s" cannot be executed for a single candidate.', self::class) |
|
| 71 | ); |
|
| 72 | } |
|
| 73 | } |
|
| 74 | ||