Browse Source

Consolidate the expression part getters into a single method.

ndm2 4 years ago
parent
commit
37233021ba

+ 15 - 18
src/Database/Expression/CaseExpressionInterface.php

@@ -23,11 +23,23 @@ use Cake\Database\TypeMap;
 interface CaseExpressionInterface extends ExpressionInterface, TypedResultInterface
 {
     /**
-     * Returns the case value for a `CASE case_value WHEN ...` expression.
+     * Returns the available data for the given clause.
      *
-     * @return \Cake\Database\ExpressionInterface|object|scalar|null
+     * ### Available clauses
+     *
+     * The following clause names are available:
+     *
+     * * `value` (`\Cake\Database\ExpressionInterface|object|scalar|null`): The case value for a
+     *   `CASE case_value WHEN ...` expression.
+     * * `when (`array<\Cake\Database\Expression\WhenThenExpressionInterface>`)`: An array of self-contained
+     *   `WHEN ... THEN ...` expressions.
+     * * `else` (`\Cake\Database\ExpressionInterface|object|scalar|null`): The `ELSE` result value.
+     *
+     * @param string $clause The name of the clause to obtain.
+     * @return array<\Cake\Database\Expression\WhenThenExpressionInterface>|\Cake\Database\ExpressionInterface|object|scalar|null
+     * @throws \InvalidArgumentException In case the given clause name is invalid.
      */
-    public function getValue();
+    public function clause(string $clause);
 
     /**
      * Sets the value for the case expression.
@@ -54,13 +66,6 @@ interface CaseExpressionInterface extends ExpressionInterface, TypedResultInterf
     public function getValueType(): ?string;
 
     /**
-     * Returns the `WHEN ... THEN ...` statement expressions.
-     *
-     * @return \Cake\Database\Expression\WhenThenExpressionInterface[]
-     */
-    public function getWhen(): array;
-
-    /**
      * Sets the `WHEN` value for a `WHEN ... THEN ...` expression, or a
      * self-contained expression that holds both the value for `WHEN`
      * and the value for `THEN`.
@@ -259,14 +264,6 @@ interface CaseExpressionInterface extends ExpressionInterface, TypedResultInterf
     public function then($result, ?string $type = null);
 
     /**
-     * Returns the `ELSE` result value.
-     *
-     * @return \Cake\Database\ExpressionInterface|object|scalar|null The result value, or `null` if none has been set
-     *  yet.
-     */
-    public function getElse();
-
-    /**
      * Sets the `ELSE` result value.
      *
      * @param \Cake\Database\ExpressionInterface|object|scalar|null $result The result value.

+ 19 - 0
src/Database/Expression/CaseExpressionTrait.php

@@ -22,6 +22,7 @@ use Cake\Database\ExpressionInterface;
 use Cake\Database\Query;
 use Cake\Database\ValueBinder;
 use DateTimeInterface;
+use InvalidArgumentException;
 
 /**
  * Trait that holds shared functionality for case related expressions.
@@ -35,6 +36,24 @@ use DateTimeInterface;
 trait CaseExpressionTrait
 {
     /**
+     * @inheritDoc
+     */
+    public function clause(string $clause)
+    {
+        if (!in_array($clause, $this->validClauseNames, true)) {
+            throw new InvalidArgumentException(
+                sprintf(
+                    'The `$clause` argument must be one of `%s`, the given value `%s` is invalid.',
+                    implode('`, `', $this->validClauseNames),
+                    $clause
+                )
+            );
+        }
+
+        return $this->{$clause};
+    }
+
+    /**
      * Infers the abstract type for the given value.
      *
      * @param mixed $value The value for which to infer the type.

+ 12 - 24
src/Database/Expression/CaseStatementExpression.php

@@ -32,6 +32,18 @@ class CaseStatementExpression implements CaseExpressionInterface
     use TypeMapTrait;
 
     /**
+     * The names of the clauses that are valid for use with the
+     * `clause()` method.
+     *
+     * @var array<string>
+     */
+    protected $validClauseNames = [
+        'value',
+        'when',
+        'else',
+    ];
+
+    /**
      * Whether this is a simple case expression.
      *
      * @var bool
@@ -104,14 +116,6 @@ class CaseStatementExpression implements CaseExpressionInterface
     /**
      * @inheritDoc
      */
-    public function getValue()
-    {
-        return $this->value;
-    }
-
-    /**
-     * @inheritDoc
-     */
     public function value($value, ?string $valueType = null)
     {
         if (
@@ -153,14 +157,6 @@ class CaseStatementExpression implements CaseExpressionInterface
     /**
      * @inheritDoc
      */
-    public function getWhen(): array
-    {
-        return $this->when;
-    }
-
-    /**
-     * @inheritDoc
-     */
     public function when($when, $type = null)
     {
         if ($this->whenBuffer !== null) {
@@ -216,14 +212,6 @@ class CaseStatementExpression implements CaseExpressionInterface
     /**
      * @inheritDoc
      */
-    public function getElse()
-    {
-        return $this->else;
-    }
-
-    /**
-     * @inheritDoc
-     */
     public function else($result, ?string $type = null)
     {
         if ($this->whenBuffer !== null) {

+ 11 - 16
src/Database/Expression/WhenThenExpression.php

@@ -31,6 +31,17 @@ class WhenThenExpression implements WhenThenExpressionInterface
     use ExpressionTypeCasterTrait;
 
     /**
+     * The names of the clauses that are valid for use with the
+     * `clause()` method.
+     *
+     * @var array<string>
+     */
+    protected $validClauseNames = [
+        'when',
+        'then',
+    ];
+
+    /**
      * The type map to use when using an array of conditions for the
      * `WHEN` value.
      *
@@ -91,14 +102,6 @@ class WhenThenExpression implements WhenThenExpressionInterface
     /**
      * @inheritDoc
      */
-    public function getWhen()
-    {
-        return $this->when;
-    }
-
-    /**
-     * @inheritDoc
-     */
     public function when($when, $type = null)
     {
         if (
@@ -181,14 +184,6 @@ class WhenThenExpression implements WhenThenExpressionInterface
     /**
      * @inheritDoc
      */
-    public function getThen()
-    {
-        return $this->then;
-    }
-
-    /**
-     * @inheritDoc
-     */
     public function then($result, ?string $type = null)
     {
         if (

+ 11 - 9
src/Database/Expression/WhenThenExpressionInterface.php

@@ -21,11 +21,20 @@ use Cake\Database\ExpressionInterface;
 interface WhenThenExpressionInterface extends ExpressionInterface
 {
     /**
-     * Returns the `WHEN` value.
+     * Returns the available data for the given clause.
      *
+     * ### Available clauses
+     *
+     * The following clause names are available:
+     *
+     * * `when` (`\Cake\Database\ExpressionInterface|object|scalar|null`): The `WHEN` value.
+     * * `then` (`\Cake\Database\ExpressionInterface|object|scalar|null`): The `THEN` result value.
+     *
+     * @param string $clause The name of the clause to obtain.
      * @return \Cake\Database\ExpressionInterface|object|scalar|null
+     * @throws \InvalidArgumentException In case the given clause name is invalid.
      */
-    public function getWhen();
+    public function clause(string $clause);
 
     /**
      * Sets the `WHEN` value.
@@ -59,13 +68,6 @@ interface WhenThenExpressionInterface extends ExpressionInterface
     public function getWhenType();
 
     /**
-     * Returns the `THEN` result value.
-     *
-     * @return \Cake\Database\ExpressionInterface|object|scalar|null The result value.
-     */
-    public function getThen();
-
-    /**
      * Sets the `THEN` result value.
      *
      * @param \Cake\Database\ExpressionInterface|object|scalar|null $result The result value.

+ 54 - 30
tests/TestCase/Database/Expression/CaseStatementExpressionTest.php

@@ -252,7 +252,7 @@ class CaseStatementExpressionTest extends TestCase
         $expression = (new CaseStatementExpression(new TypeMap(['Table.column' => 'boolean'])))
             ->when($value)
             ->then(1);
-        $this->assertSame($type, $expression->getWhen()[0]->getWhenType());
+        $this->assertSame($type, $expression->clause('when')[0]->getWhenType());
     }
 
     /**
@@ -265,7 +265,7 @@ class CaseStatementExpressionTest extends TestCase
         $expression = (new CaseStatementExpression(new TypeMap(['Table.column' => 'boolean'])))
             ->when(['Table.column' => true])
             ->then($value);
-        $this->assertSame($type, $expression->getWhen()[0]->getThenType());
+        $this->assertSame($type, $expression->clause('when')[0]->getThenType());
     }
 
     /**
@@ -911,18 +911,38 @@ class CaseStatementExpressionTest extends TestCase
 
     // region Getters
 
-    public function testGetValue(): void
+    public function testGetInvalidCaseExpressionClause()
+    {
+        $this->expectException(InvalidArgumentException::class);
+        $this->expectExceptionMessage(
+            'The `$clause` argument must be one of `value`, `when`, `else`, the given value `invalid` is invalid.'
+        );
+
+        (new CaseStatementExpression())->clause('invalid');
+    }
+
+    public function testGetInvalidWhenThenExpressionClause()
+    {
+        $this->expectException(InvalidArgumentException::class);
+        $this->expectExceptionMessage(
+            'The `$clause` argument must be one of `when`, `then`, the given value `invalid` is invalid.'
+        );
+
+        (new WhenThenExpression())->clause('invalid');
+    }
+
+    public function testGetValueClause(): void
     {
         $expression = new CaseStatementExpression();
 
-        $this->assertNull($expression->getValue());
+        $this->assertNull($expression->clause('value'));
 
         $expression
             ->value(1)
             ->when(1)
             ->then(2);
 
-        $this->assertSame(1, $expression->getValue());
+        $this->assertSame(1, $expression->clause('value'));
     }
 
     public function testGetValueType(): void
@@ -936,27 +956,27 @@ class CaseStatementExpressionTest extends TestCase
         $this->assertSame('integer', $expression->getValueType());
     }
 
-    public function testGetWhen(): void
+    public function testGetWhenClause(): void
     {
         $when = ['Table.column' => true];
 
         $expression = new CaseStatementExpression();
-        $this->assertSame([], $expression->getWhen());
+        $this->assertSame([], $expression->clause('when'));
 
         $expression
             ->when($when)
             ->then(1);
 
-        $this->assertCount(1, $expression->getWhen());
-        $this->assertInstanceOf(WhenThenExpressionInterface::class, $expression->getWhen()[0]);
+        $this->assertCount(1, $expression->clause('when'));
+        $this->assertInstanceOf(WhenThenExpressionInterface::class, $expression->clause('when')[0]);
     }
 
-    public function testWhenArrayValueGetWhen(): void
+    public function testWhenArrayValueGetWhenClause(): void
     {
         $when = ['Table.column' => true];
 
         $expression = new CaseStatementExpression();
-        $this->assertSame([], $expression->getWhen());
+        $this->assertSame([], $expression->clause('when'));
 
         $expression
             ->when($when)
@@ -964,20 +984,20 @@ class CaseStatementExpressionTest extends TestCase
 
         $this->assertEquals(
             new QueryExpression($when),
-            $expression->getWhen()[0]->getWhen()
+            $expression->clause('when')[0]->clause('when')
         );
     }
 
-    public function testWhenNonArrayValueGetWhen(): void
+    public function testWhenNonArrayValueGetWhenClause(): void
     {
         $expression = new CaseStatementExpression();
-        $this->assertSame([], $expression->getWhen());
+        $this->assertSame([], $expression->clause('when'));
 
         $expression
             ->when(1)
             ->then(2);
 
-        $this->assertSame(1, $expression->getWhen()[0]->getWhen());
+        $this->assertSame(1, $expression->clause('when')[0]->clause('when'));
     }
 
     public function testWhenGetWhenType(): void
@@ -987,32 +1007,32 @@ class CaseStatementExpressionTest extends TestCase
                 return $whenThen;
             });
 
-        $this->assertNull($expression->getWhen()[0]->getWhenType());
+        $this->assertNull($expression->clause('when')[0]->getWhenType());
 
-        $expression->getWhen()[0]->when(1);
+        $expression->clause('when')[0]->when(1);
 
-        $this->assertSame('integer', $expression->getWhen()[0]->getWhenType());
+        $this->assertSame('integer', $expression->clause('when')[0]->getWhenType());
 
         $types = [
             'Table.column' => 'boolean',
         ];
-        $expression->getWhen()[0]->when(['Table.column' => true], $types);
+        $expression->clause('when')[0]->when(['Table.column' => true], $types);
 
-        $this->assertSame($types, $expression->getWhen()[0]->getWhenType());
+        $this->assertSame($types, $expression->clause('when')[0]->getWhenType());
     }
 
-    public function testWhenGetThen(): void
+    public function testWhenGetThenClause(): void
     {
         $expression = (new CaseStatementExpression())
             ->when(function (WhenThenExpressionInterface $whenThen) {
                 return $whenThen;
             });
 
-        $this->assertNull($expression->getWhen()[0]->getThen());
+        $this->assertNull($expression->clause('when')[0]->clause('then'));
 
-        $expression->getWhen()[0]->then(1);
+        $expression->clause('when')[0]->then(1);
 
-        $this->assertSame(1, $expression->getWhen()[0]->getThen());
+        $this->assertSame(1, $expression->clause('when')[0]->clause('then'));
     }
 
     public function testWhenGetThenType(): void
@@ -1022,21 +1042,25 @@ class CaseStatementExpressionTest extends TestCase
                 return $whenThen;
             });
 
-        $this->assertNull($expression->getWhen()[0]->getThenType());
+        $this->assertNull($expression->clause('when')[0]->getThenType());
 
-        $expression->getWhen()[0]->then(1);
+        $expression->clause('when')[0]->then(1);
 
-        $this->assertSame('integer', $expression->getWhen()[0]->getThenType());
+        $this->assertSame('integer', $expression->clause('when')[0]->getThenType());
     }
 
-    public function testGetElse(): void
+    public function testGetElseClause(): void
     {
-        $expression = (new CaseStatementExpression())
+        $expression = new CaseStatementExpression();
+
+        $this->assertNull($expression->clause('else'));
+
+        $expression
             ->when(['Table.column' => true])
             ->then(1)
             ->else(2);
 
-        $this->assertSame(2, $expression->getElse());
+        $this->assertSame(2, $expression->clause('else'));
     }
 
     public function testGetElseType(): void