2222 *
2323 * ```php
2424 * $case = new CaseX(
25- * when1: new When('condition1' , 'result1'),
26- * when2: new When('condition2' , 'result2'),
27- * else: 'defaultResult ',
25+ * when1: new WhenThen(true , 'result1'),
26+ * when2: new WhenThen(false , 'result2'),
27+ * else: 'default result ',
2828 * );
2929 * ```
3030 *
3131 * This will be generated into a SQL `CASE` expression like:
3232 *
3333 * ```sql
3434 * CASE
35- * WHEN condition1 THEN result1
36- * WHEN condition2 THEN result2
37- * ELSE defaultResult
35+ * WHEN TRUE THEN ' result1'
36+ * WHEN FALSE THEN ' result2'
37+ * ELSE 'default result'
3838 * END
3939 * ```
4040 *
4141 * Example with a specific case value:
4242 *
4343 * ```php
4444 * $case = new CaseX(
45- * 'expression ',
46- * when1: new When(1 , 'result1'),
47- * when2: new When(2 , 'result2'),
48- * else: 'defaultResult ',
45+ * 'column_name ',
46+ * when1: new WhenThen('one' , 'result1'),
47+ * when2: new WhenThen('two' , 'result2'),
48+ * else: 'default result ',
4949 * );
5050 * ```
5151 *
5252 * This will be generated into a SQL `CASE` expression like:
5353 *
5454 * ```sql
55- * CASE expression
56- * WHEN 1 THEN result1
57- * WHEN 2 THEN result2
58- * ELSE defaultResult
55+ * CASE "column_name"
56+ * WHEN 'one' THEN ' result1'
57+ * WHEN 'two' THEN ' result2'
58+ * ELSE 'default result'
5959 * END
6060 * ```
6161 */
6262final class CaseX implements ExpressionInterface
6363{
6464 /**
65- * @var When [] List of `WHEN` conditions and their corresponding results in the `CASE` expression.
65+ * @var WhenThen [] List of `WHEN-THEN ` conditions and their corresponding results in the `CASE` expression.
6666 */
67- public readonly array $ when ;
67+ public readonly array $ whenThen ;
6868 /**
6969 * @var mixed The result to return if no conditions match in the CASE expression.
7070 * If not set, the `CASE` expression will not have an `ELSE` clause.
@@ -75,37 +75,38 @@ final class CaseX implements ExpressionInterface
7575
7676 /**
7777 * @param mixed $value Comparison condition in the `CASE` expression:
78- * - `string` is treated as a SQL expression ;
78+ * - `string` is treated as a table column name which will be quoted before usage in the SQL statement ;
7979 * - `array` is treated as a condition to check, see {@see QueryInterface::where()};
8080 * - other values will be converted to their string representation using {@see QueryBuilderInterface::buildValue()}.
81- * If not provided, the `CASE` expression will be a WHEN-THEN structure without a specific case value.
82- * @param ColumnInterface|string $valueType Optional data type of the CASE expression which can be used in some DBMS
83- * to specify the expected type (for example in PostgreSQL).
84- * @param mixed|When ...$args List of `WHEN` conditions and their corresponding results represented
85- * as {@see When} instances or `ELSE` value in the `CASE` expression.
81+ * If not provided, the `CASE` expression will be a `WHEN-THEN` structure without a specific case value.
82+ * @param ColumnInterface|string $valueType Optional data type of the `CASE` expression which can be used
83+ * in some DBMS to specify the expected type (for example, in PostgreSQL).
84+ * @param mixed|WhenThen ...$args List of `WHEN-THEN` conditions and their corresponding results represented
85+ * as {@see WhenThen} instances or `ELSE` value in the `CASE` expression. String `ELSE` value will be quoted
86+ * before usage in the SQL statement.
8687 */
8788 public function __construct (
8889 public readonly mixed $ value = null ,
8990 public readonly string |ColumnInterface $ valueType = '' ,
9091 mixed ...$ args ,
9192 ) {
92- $ when = [];
93+ $ whenThen = [];
9394
9495 foreach ($ args as $ arg ) {
95- if ($ arg instanceof When ) {
96- $ when [] = $ arg ;
96+ if ($ arg instanceof WhenThen ) {
97+ $ whenThen [] = $ arg ;
9798 } elseif ($ this ->hasElse ()) {
9899 throw new InvalidArgumentException ('`CASE` expression can have only one `ELSE` value. ' );
99100 } else {
100101 $ this ->else = $ arg ;
101102 }
102103 }
103104
104- if (empty ($ when )) {
105- throw new InvalidArgumentException ('`CASE` expression must have at least one `WHEN` clause. ' );
105+ if (empty ($ whenThen )) {
106+ throw new InvalidArgumentException ('`CASE` expression must have at least one `WHEN-THEN ` clause. ' );
106107 }
107108
108- $ this ->when = $ when ;
109+ $ this ->whenThen = $ whenThen ;
109110 }
110111
111112 /**
0 commit comments