ソースを参照

Merge branch '4.next' into 5.x

Corey Taylor 4 年 前
コミット
c39cf4dd10

+ 1 - 1
src/Controller/ControllerFactory.php

@@ -259,7 +259,7 @@ class ControllerFactory implements ControllerFactoryInterface, RequestHandlerInt
             case 'float':
                 return is_numeric($argument) ? (float)$argument : null;
             case 'int':
-                return filter_var($argument, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
+                return ctype_digit($argument) ? (int)$argument : null;
             case 'bool':
                 return $argument === '0' ? false : ($argument === '1' ? true : null);
         }

+ 10 - 10
src/Database/Expression/CaseExpression.php

@@ -23,6 +23,8 @@ use Closure;
 
 /**
  * This class represents a SQL Case statement
+ *
+ * @deprecated 4.3.0 Use CaseStatementExpression instead or Query::case()
  */
 class CaseExpression implements ExpressionInterface
 {
@@ -67,11 +69,15 @@ class CaseExpression implements ExpressionInterface
         ExpressionInterface|array|string|float|int|bool|null $values = [],
         array|string $types = []
     ) {
+        $conditions = is_array($conditions) ? $conditions : [$conditions];
+        $values = is_array($values) ? $values : [$values];
+        $types = is_array($types) ? $types : [$types];
+
         if (!empty($conditions)) {
             $this->add($conditions, $values, $types);
         }
 
-        if (is_array($conditions) && is_array($values) && count($values) > count($conditions)) {
+        if (count($values) > count($conditions)) {
             end($values);
             $key = key($values);
             $this->elseValue($values[$key], $types[$key] ?? null);
@@ -94,15 +100,9 @@ class CaseExpression implements ExpressionInterface
         ExpressionInterface|array|string|float|int|bool|null $values = [],
         array|string $types = []
     ) {
-        if (!is_array($conditions)) {
-            $conditions = [$conditions];
-        }
-        if (!is_array($values)) {
-            $values = [$values];
-        }
-        if (!is_array($types)) {
-            $types = [$types];
-        }
+        $conditions = is_array($conditions) ? $conditions : [$conditions];
+        $values = is_array($values) ? $values : [$values];
+        $types = is_array($types) ? $types : [$types];
 
         $this->_addExpressions($conditions, $values, $types);
 

+ 8 - 7
src/Database/Expression/CaseStatementExpression.php

@@ -25,6 +25,9 @@ use Closure;
 use InvalidArgumentException;
 use LogicException;
 
+/**
+ * Represents a SQL case statement with a fluid API
+ */
 class CaseStatementExpression implements ExpressionInterface, TypedResultInterface
 {
     use CaseExpressionTrait;
@@ -275,7 +278,7 @@ class CaseStatementExpression implements ExpressionInterface, TypedResultInterfa
      *      ->bind(':userData', $userData, 'integer')
      * ```
      *
-     * @param \Cake\Database\ExpressionInterface|\Closure|array|object|scalar $when The `WHEN` value. When using an
+     * @param \Cake\Database\ExpressionInterface|\Closure|object|array|scalar $when The `WHEN` value. When using an
      *  array of conditions, it must be compatible with `\Cake\Database\Query::where()`. Note that this argument is
      *  _not_ completely safe for use with user data, as a user supplied array would allow for raw SQL to slip in! If
      *  you plan to use user data, either pass a single type for the `$type` argument (which forces the `$when` value to
@@ -488,14 +491,12 @@ class CaseStatementExpression implements ExpressionInterface, TypedResultInterfa
      *
      * 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\WhenThenExpression>`)`: An array of self-contained
-     *   `WHEN ... THEN ...` expressions.
-     * * `else` (`\Cake\Database\ExpressionInterface|object|scalar|null`): The `ELSE` result value.
+     * * `value`: The case value for a `CASE case_value WHEN ...` expression.
+     * * `when`: An array of `WHEN ... THEN ...` expressions.
+     * * `else`: The `ELSE` result value.
      *
      * @param string $clause The name of the clause to obtain.
-     * @return array<\Cake\Database\Expression\WhenThenExpression>|\Cake\Database\ExpressionInterface|object|scalar|null
+     * @return \Cake\Database\ExpressionInterface|object|array<\Cake\Database\Expression\WhenThenExpression>|scalar|null
      * @throws \InvalidArgumentException In case the given clause name is invalid.
      */
     public function clause(string $clause)

+ 1 - 0
src/Database/Expression/QueryExpression.php

@@ -346,6 +346,7 @@ class QueryExpression implements ExpressionInterface, Countable
      * @param array<string> $types Associative array of types to be associated with the values
      * passed in $values
      * @return $this
+     * @deprecated 4.3.0 Use Query::case() or CaseStatementExpression instead
      */
     public function addCase(
         ExpressionInterface|array $conditions,

+ 6 - 3
src/Database/Expression/WhenThenExpression.php

@@ -25,6 +25,9 @@ use Closure;
 use InvalidArgumentException;
 use LogicException;
 
+/**
+ * Represents a SQL when/then clause with a fluid API
+ */
 class WhenThenExpression implements ExpressionInterface
 {
     use CaseExpressionTrait;
@@ -102,7 +105,7 @@ class WhenThenExpression implements ExpressionInterface
     /**
      * Sets the `WHEN` value.
      *
-     * @param \Cake\Database\ExpressionInterface|array|object|scalar $when The `WHEN` value. When using an array of
+     * @param \Cake\Database\ExpressionInterface|object|array|scalar $when The `WHEN` value. When using an array of
      *  conditions, it must be compatible with `\Cake\Database\Query::where()`. Note that this argument is _not_
      *  completely safe for use with user data, as a user supplied array would allow for raw SQL to slip in! If you
      *  plan to use user data, either pass a single type for the `$type` argument (which forces the `$when` value to be
@@ -248,8 +251,8 @@ class WhenThenExpression implements ExpressionInterface
      *
      * 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.
+     * * `when`: The `WHEN` value.
+     * * `then`: The `THEN` result value.
      *
      * @param string $clause The name of the clause to obtain.
      * @return \Cake\Database\ExpressionInterface|object|scalar|null

+ 1 - 1
tests/TestCase/Controller/ControllerFactoryTest.php

@@ -684,7 +684,7 @@ class ControllerFactoryTest extends TestCase
                 'plugin' => null,
                 'controller' => 'Dependencies',
                 'action' => 'requiredTyped',
-                'pass' => ['1.0', '2', '0'],
+                'pass' => ['1.0', '02', '0'],
             ],
         ]);
         $controller = $this->factory->create($request);

+ 4 - 0
tests/TestCase/Database/Expression/CaseExpressionTest.php

@@ -43,6 +43,10 @@ class CaseExpressionTest extends TestCase
         $expected = 'CASE WHEN test = :c0 THEN :param1 WHEN test2 = :c2 THEN :param3 END';
         $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
 
+        $caseExpression = new CaseExpression($expr, ['foobar', 'else']);
+        $expected = 'CASE WHEN test = :c0 THEN :param1 ELSE :param2 END';
+        $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
+
         $caseExpression = new CaseExpression([$expr], ['foobar', 'else']);
         $expected = 'CASE WHEN test = :c0 THEN :param1 ELSE :param2 END';
         $this->assertSame($expected, $caseExpression->sql(new ValueBinder()));