diff --git a/UPGRADE.md b/UPGRADE.md index 582216c0..e36d97a2 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -255,6 +255,41 @@ * The `Satisfiable` interface was added. * The `Specification` interface was extends `Satisfiable` interface. * The `BaseSpecification` class implement `Satisfiable` interface. +* The `Happyr\DoctrineSpecification\Operand\CountDistinct` class was removed, use + `Happyr\DoctrineSpecification\Operand\PlatformFunction\Count` instead. +* The `Spec::countDistinct()` method was removed, use `Spec::COUNT()` instead. + + Before: + + ```php + new CountDistinct('field_name'); + Spec::countDistinct('field_name'); + ``` + + After: + + ```php + new Count('field_name', true); + Spec::COUNT('field_name', true); + ``` + +* The `COUNT` function as argument to `Spec::fun()` is not longer supported, use `Spec::COUNT()` instead. +* The `COUNT` function as argument to `Happyr\DoctrineSpecification\Operand\PlatformFunction` is not longer supported, + use `Spec::COUNT()` instead. + + Before: + + ```php + new PlatformFunction('COUNT', 'field_name'); + Spec::fun('COUNT', 'field_name'); + ``` + + After: + + ```php + new Count('field_name'); + Spec::COUNT('field_name'); + ``` # Upgrade from 1.0 to 1.1 diff --git a/src/Operand/PlatformFunction.php b/src/Operand/PlatformFunction.php index b2361b34..d29eb151 100644 --- a/src/Operand/PlatformFunction.php +++ b/src/Operand/PlatformFunction.php @@ -22,7 +22,6 @@ use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\BitAndExecutor; use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\BitOrExecutor; use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\ConcatExecutor; -use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CountExecutor; use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CurrentDateExecutor; use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CurrentTimeExecutor; use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CurrentTimestampExecutor; @@ -72,7 +71,6 @@ final class PlatformFunction implements Operand 'MAX' => MaxExecutor::class, 'AVG' => AvgExecutor::class, 'SUM' => SumExecutor::class, - 'COUNT' => CountExecutor::class, // Datetime functions 'CURRENT_DATE' => CurrentDateExecutor::class, 'CURRENT_TIME' => CurrentTimeExecutor::class, diff --git a/src/Operand/CountDistinct.php b/src/Operand/PlatformFunction/Count.php similarity index 69% rename from src/Operand/CountDistinct.php rename to src/Operand/PlatformFunction/Count.php index 2a6c5633..baea5ac2 100644 --- a/src/Operand/CountDistinct.php +++ b/src/Operand/PlatformFunction/Count.php @@ -12,24 +12,33 @@ * file that was distributed with this source code. */ -namespace Happyr\DoctrineSpecification\Operand; +namespace Happyr\DoctrineSpecification\Operand\PlatformFunction; use Doctrine\ORM\QueryBuilder; use Happyr\DoctrineSpecification\Exception\OperandNotExecuteException; +use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter; +use Happyr\DoctrineSpecification\Operand\Operand; -final class CountDistinct implements Operand +final class Count implements Operand { /** * @var Operand|string */ private $field; + /** + * @var bool + */ + private $distinct; + /** * @param Operand|string $field + * @param bool $distinct */ - public function __construct($field) + public function __construct($field, bool $distinct = false) { $this->field = $field; + $this->distinct = $distinct; } /** @@ -43,7 +52,13 @@ public function transform(QueryBuilder $qb, string $context): string $field = ArgumentToOperandConverter::toField($this->field); $field = $field->transform($qb, $context); - return sprintf('COUNT(DISTINCT %s)', $field); + $expression = ''; + + if ($this->distinct) { + $expression = 'DISTINCT '; + } + + return sprintf('COUNT(%s%s)', $expression, $field); } /** diff --git a/src/Operand/PlatformFunction/Executor/CountExecutor.php b/src/Operand/PlatformFunction/Executor/CountExecutor.php deleted file mode 100644 index 72ba275c..00000000 --- a/src/Operand/PlatformFunction/Executor/CountExecutor.php +++ /dev/null @@ -1,30 +0,0 @@ - - * Kacper Gunia - * Peter Gribanov - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor; - -use Happyr\DoctrineSpecification\Exception\OperandNotExecuteException; - -final class CountExecutor -{ - /** - * @throw OperandNotExecuteException - */ - public function __invoke(): void - { - throw new OperandNotExecuteException( - sprintf('Platform function "%s" cannot be executed for a single candidate.', __CLASS__) - ); - } -} diff --git a/src/Spec.php b/src/Spec.php index 5649736f..b3e50096 100644 --- a/src/Spec.php +++ b/src/Spec.php @@ -32,13 +32,13 @@ use Happyr\DoctrineSpecification\Logic\OrX; use Happyr\DoctrineSpecification\Operand\Addition; use Happyr\DoctrineSpecification\Operand\Alias; -use Happyr\DoctrineSpecification\Operand\CountDistinct; use Happyr\DoctrineSpecification\Operand\Division; use Happyr\DoctrineSpecification\Operand\Field; use Happyr\DoctrineSpecification\Operand\LikePattern; use Happyr\DoctrineSpecification\Operand\Multiplication; use Happyr\DoctrineSpecification\Operand\Operand; use Happyr\DoctrineSpecification\Operand\PlatformFunction; +use Happyr\DoctrineSpecification\Operand\PlatformFunction\Count; use Happyr\DoctrineSpecification\Operand\PlatformFunction\DateAdd; use Happyr\DoctrineSpecification\Operand\PlatformFunction\DateSub; use Happyr\DoctrineSpecification\Operand\PlatformFunction\Trim; @@ -91,7 +91,6 @@ * @method static PlatformFunction MAX($a) * @method static PlatformFunction AVG($a) * @method static PlatformFunction SUM($a) - * @method static PlatformFunction COUNT($a) * @method static PlatformFunction CURRENT_DATE() Return the current date * @method static PlatformFunction CURRENT_TIME() Returns the current time * @method static PlatformFunction CURRENT_TIMESTAMP() Returns a timestamp of the current date and time. @@ -578,16 +577,6 @@ public static function likePattern(string $value, string $format = LikePattern:: return new LikePattern($value, $format); } - /** - * @param Operand|string $field - * - * @return CountDistinct - */ - public static function countDistinct($field): CountDistinct - { - return new CountDistinct($field); - } - // Arithmetic operands /** @@ -637,15 +626,14 @@ public static function div($field, $value): Division // Platform functions /** - * Trim the string by the given trim char, defaults to whitespaces. + * @param Operand|string $field + * @param bool $distinct * - * @param Operand|string $string - * @param string $mode - * @param string $characters + * @return Count */ - public static function TRIM($string, string $mode = Trim::BOTH, string $characters = ''): Trim + public static function COUNT($field, bool $distinct = false): Count { - return new Trim($string, $mode, $characters); + return new Count($field, $distinct); } /** @@ -676,6 +664,18 @@ public static function DATE_SUB($date, $value, string $unit): DateSub return new DateSub($date, $value, $unit); } + /** + * Trim the string by the given trim char, defaults to whitespaces. + * + * @param Operand|string $string + * @param string $mode + * @param string $characters + */ + public static function TRIM($string, string $mode = Trim::BOTH, string $characters = ''): Trim + { + return new Trim($string, $mode, $characters); + } + /** * Call DQL function. * diff --git a/tests/Operand/CountDistinctSpec.php b/tests/Operand/PlatformFunction/CountSpec.php similarity index 72% rename from tests/Operand/CountDistinctSpec.php rename to tests/Operand/PlatformFunction/CountSpec.php index e5e5c161..d1954742 100644 --- a/tests/Operand/CountDistinctSpec.php +++ b/tests/Operand/PlatformFunction/CountSpec.php @@ -12,18 +12,18 @@ * file that was distributed with this source code. */ -namespace tests\Happyr\DoctrineSpecification\Operand; +namespace tests\Happyr\DoctrineSpecification\Operand\PlatformFunction; use Doctrine\ORM\QueryBuilder; use Happyr\DoctrineSpecification\Exception\OperandNotExecuteException; -use Happyr\DoctrineSpecification\Operand\CountDistinct; use Happyr\DoctrineSpecification\Operand\Operand; +use Happyr\DoctrineSpecification\Operand\PlatformFunction\Count; use PhpSpec\ObjectBehavior; /** - * @mixin CountDistinct + * @mixin Count */ -final class CountDistinctSpec extends ObjectBehavior +final class CountSpec extends ObjectBehavior { private $field = 'foo'; @@ -34,7 +34,7 @@ public function let(): void public function it_is_a_count_distinct(): void { - $this->shouldBeAnInstanceOf(CountDistinct::class); + $this->shouldBeAnInstanceOf(Count::class); } public function it_is_a_operand(): void @@ -44,6 +44,13 @@ public function it_is_a_operand(): void public function it_is_transformable(QueryBuilder $qb): void { + $this->transform($qb, 'a')->shouldReturn('COUNT(a.foo)'); + } + + public function it_is_transformable_distinct(QueryBuilder $qb): void + { + $this->beConstructedWith($this->field, true); + $this->transform($qb, 'a')->shouldReturn('COUNT(DISTINCT a.foo)'); } diff --git a/tests/Operand/PlatformFunction/Executor/CountExecutorSpec.php b/tests/Operand/PlatformFunction/Executor/CountExecutorSpec.php deleted file mode 100644 index ed9b496a..00000000 --- a/tests/Operand/PlatformFunction/Executor/CountExecutorSpec.php +++ /dev/null @@ -1,30 +0,0 @@ - - * Kacper Gunia - * Peter Gribanov - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace tests\Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor; - -use Happyr\DoctrineSpecification\Exception\OperandNotExecuteException; -use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CountExecutor; -use PhpSpec\ObjectBehavior; - -/** - * @mixin CountExecutor - */ -final class CountExecutorSpec extends ObjectBehavior -{ - public function it_should_throw_exception_on_execute(): void - { - $this->shouldThrow(OperandNotExecuteException::class)->during('__invoke'); - } -} diff --git a/tests/SpecSpec.php b/tests/SpecSpec.php index 4daff8ef..a665dc23 100644 --- a/tests/SpecSpec.php +++ b/tests/SpecSpec.php @@ -18,7 +18,6 @@ use Happyr\DoctrineSpecification\Logic\LogicX; use Happyr\DoctrineSpecification\Operand\Addition; use Happyr\DoctrineSpecification\Operand\Alias; -use Happyr\DoctrineSpecification\Operand\CountDistinct; use Happyr\DoctrineSpecification\Operand\Division; use Happyr\DoctrineSpecification\Operand\Multiplication; use Happyr\DoctrineSpecification\Operand\PlatformFunction; @@ -47,11 +46,6 @@ public function it_creates_distinct(): void $this->distinct()->shouldReturnAnInstanceOf(Distinct::class); } - public function it_creates_count_distinct(): void - { - $this->countDistinct('foo')->shouldReturnAnInstanceOf(CountDistinct::class); - } - public function it_creates_add_operand(): void { $this->add('foo', 'bar')->shouldReturnAnInstanceOf(Addition::class);