From c4f8181c957403f81cfffc98a67181cb79192713 Mon Sep 17 00:00:00 2001 From: tpetry Date: Thu, 30 Nov 2023 09:31:28 +0100 Subject: [PATCH] improvements --- src/Language/{CaseBlock.php => CaseGroup.php} | 17 ++++++------ .../{CaseCondition.php => CaseRule.php} | 4 +-- tests/Language/CaseTest.php | 26 +++++++++---------- 3 files changed, 24 insertions(+), 23 deletions(-) rename src/Language/{CaseBlock.php => CaseGroup.php} (60%) rename src/Language/{CaseCondition.php => CaseRule.php} (87%) diff --git a/src/Language/CaseBlock.php b/src/Language/CaseGroup.php similarity index 60% rename from src/Language/CaseBlock.php rename to src/Language/CaseGroup.php index 505b343..0f98590 100644 --- a/src/Language/CaseBlock.php +++ b/src/Language/CaseGroup.php @@ -4,31 +4,32 @@ namespace Tpetry\QueryExpressions\Language; -use Illuminate\Contracts\Database\Query\ConditionExpression; use Illuminate\Contracts\Database\Query\Expression; use Illuminate\Database\Grammar; use Tpetry\QueryExpressions\Concerns\IdentifiesDriver; use Tpetry\QueryExpressions\Concerns\StringizeExpression; -use Tpetry\QueryExpressions\Function\Conditional\ManyArgumentsExpression; -class CaseBlock extends ManyArgumentsExpression implements ConditionExpression +class CaseGroup implements Expression { use IdentifiesDriver; use StringizeExpression; /** - * @param non-empty-array $when + * @param non-empty-array $when */ public function __construct( - array $when, + private readonly array $when, private readonly string|Expression|null $else = null, ) { - parent::__construct($when); } - public function getValue(Grammar $grammar) + public function getValue(Grammar $grammar): string { - $conditions = implode(' ', $this->getExpressions($grammar)); + $conditions = array_map( + callback: fn ($expression) => $this->stringize($grammar, $expression), + array: $this->when, + ); + $conditions = implode(' ', $conditions); return match ($this->else) { null => "(case {$conditions} end)", diff --git a/src/Language/CaseCondition.php b/src/Language/CaseRule.php similarity index 87% rename from src/Language/CaseCondition.php rename to src/Language/CaseRule.php index bc5afa8..9125698 100644 --- a/src/Language/CaseCondition.php +++ b/src/Language/CaseRule.php @@ -9,7 +9,7 @@ use Illuminate\Database\Grammar; use Tpetry\QueryExpressions\Concerns\StringizeExpression; -class CaseCondition implements Expression +class CaseRule implements Expression { use StringizeExpression; @@ -19,7 +19,7 @@ public function __construct( ) { } - public function getValue(Grammar $grammar) + public function getValue(Grammar $grammar): string { $condition = $this->stringize($grammar, $this->condition); $result = $this->stringize($grammar, $this->result); diff --git a/tests/Language/CaseTest.php b/tests/Language/CaseTest.php index e669100..ad01e36 100644 --- a/tests/Language/CaseTest.php +++ b/tests/Language/CaseTest.php @@ -3,14 +3,14 @@ declare(strict_types=1); use Illuminate\Database\Query\Expression; -use Tpetry\QueryExpressions\Language\CaseBlock; -use Tpetry\QueryExpressions\Language\CaseCondition; +use Tpetry\QueryExpressions\Language\CaseGroup; +use Tpetry\QueryExpressions\Language\CaseRule; use Tpetry\QueryExpressions\Tests\ConditionExpression; it('can create create a case-expression with a single branch') ->expect( - new CaseBlock([ - new CaseCondition(new Expression(2), new ConditionExpression('1 = 1')), + new CaseGroup([ + new CaseRule(new Expression(2), new ConditionExpression('1 = 1')), ]) ) ->toBeExecutable() @@ -20,9 +20,9 @@ ->toBeSqlsrv('(case when 1 = 1 then 2 end)'); it('can create create a case-expression with multiple branches') - ->expect(new CaseBlock([ - new CaseCondition(new Expression(2), new ConditionExpression('1 = 1')), - new CaseCondition('val', new ConditionExpression('2 = 2')), + ->expect(new CaseGroup([ + new CaseRule(new Expression(2), new ConditionExpression('1 = 1')), + new CaseRule('val', new ConditionExpression('2 = 2')), ])) ->toBeExecutable(['val int']) ->toBeMysql('(case when 1 = 1 then 2 when 2 = 2 then `val` end)') @@ -31,10 +31,10 @@ ->toBeSqlsrv('(case when 1 = 1 then 2 when 2 = 2 then [val] end)'); it('can create create a case-expression with multiple branches and expression default') - ->expect(new CaseBlock( + ->expect(new CaseGroup( [ - new CaseCondition(new Expression(2), new ConditionExpression('1 = 1')), - new CaseCondition('val', new ConditionExpression('2 = 2')), + new CaseRule(new Expression(2), new ConditionExpression('1 = 1')), + new CaseRule('val', new ConditionExpression('2 = 2')), ], new Expression('4'), )) @@ -45,10 +45,10 @@ ->toBeSqlsrv('(case when 1 = 1 then 2 when 2 = 2 then [val] else 4 end)'); it('can create create a case-expression with multiple branches and column default') - ->expect(new CaseBlock( + ->expect(new CaseGroup( [ - new CaseCondition(new Expression(2), new ConditionExpression('1 = 1')), - new CaseCondition('val', new ConditionExpression('2 = 2')), + new CaseRule(new Expression(2), new ConditionExpression('1 = 1')), + new CaseRule('val', new ConditionExpression('2 = 2')), ], 'val', ))