55namespace PhpDb \Sql ;
66
77use Override ;
8+ use PhpDb \Sql \Argument \Argument ;
9+ use PhpDb \Sql \Argument \ArgumentInterface ;
10+ use PhpDb \Sql \Argument \ArgumentType as NewArgumentType ;
11+ use PhpDb \Sql \ArgumentType as OldArgumentType ;
812
913use function array_slice ;
1014use function array_unique ;
1115use function count ;
16+ use function current ;
1217use function func_get_args ;
1318use function func_num_args ;
1419use function is_array ;
20+ use function key ;
1521use function preg_match_all ;
1622use function str_ireplace ;
1723use function str_replace ;
@@ -25,15 +31,15 @@ class Expression extends AbstractExpression
2531
2632 protected string $ expression = '' ;
2733
28- /** @var Argument [] */
34+ /** @var ArgumentInterface [] */
2935 protected array $ parameters = [];
3036
3137 /**
3238 * @todo Update documentation to show how parameters can be specifically typed
3339 */
3440 public function __construct (
3541 string $ expression = '' ,
36- null |bool |string |float |int |array |Argument |ExpressionInterface $ parameters = []
42+ null |bool |string |float |int |array |ArgumentInterface |ExpressionInterface $ parameters = []
3743 ) {
3844 if ($ expression !== '' ) {
3945 $ this ->setExpression ($ expression );
@@ -72,7 +78,7 @@ public function getExpression(): string
7278 /**
7379 * @throws Exception\InvalidArgumentException
7480 */
75- public function setParameters (null |bool |string |float |int |array |ExpressionInterface |Argument $ parameters = []): self
81+ public function setParameters (null |bool |string |float |int |array |ExpressionInterface |ArgumentInterface $ parameters = []): self
7682 {
7783 if (func_num_args () > 1 ) {
7884 /**
@@ -87,12 +93,43 @@ public function setParameters(null|bool|string|float|int|array|ExpressionInterfa
8793 $ parameters = [$ parameters ];
8894 }
8995
90- /** @var null|bool|string|float|int|array|ExpressionInterface|Argument $parameter */
96+ /** @var null|bool|string|float|int|array|ExpressionInterface|ArgumentInterface $parameter */
9197 foreach ($ parameters as $ key => $ parameter ) {
92- if ($ parameter instanceof ArgumentType) {
93- $ parameter = new Argument ($ key , $ parameter );
94- } elseif (! $ parameter instanceof Argument) {
95- $ parameter = new Argument ($ parameter );
98+ if ($ parameter instanceof ArgumentInterface) {
99+ // Already an ArgumentInterface, use as-is
100+ } elseif ($ parameter instanceof NewArgumentType || $ parameter instanceof OldArgumentType) {
101+ // Legacy support: ['name' => ArgumentType::Identifier] syntax
102+ // Handle both old and new ArgumentType enums
103+ $ parameter = match (true ) {
104+ $ parameter === NewArgumentType::Identifier || $ parameter === OldArgumentType::Identifier => Argument::identifier ($ key ),
105+ $ parameter === NewArgumentType::Literal || $ parameter === OldArgumentType::Literal => Argument::literal ($ key ),
106+ default => Argument::value ($ key ),
107+ };
108+ } elseif (is_array ($ parameter )) {
109+ // Legacy support: [['name' => ArgumentType::Identifier]] syntax
110+ // Check if this is a single-element array with ArgumentType as value
111+ if (count ($ parameter ) === 1 ) {
112+ $ arrayKey = key ($ parameter );
113+ $ arrayValue = current ($ parameter );
114+ if ($ arrayValue instanceof NewArgumentType || $ arrayValue instanceof OldArgumentType) {
115+ $ parameter = match (true ) {
116+ $ arrayValue === NewArgumentType::Identifier || $ arrayValue === OldArgumentType::Identifier => Argument::identifier ($ arrayKey ),
117+ $ arrayValue === NewArgumentType::Literal || $ arrayValue === OldArgumentType::Literal => Argument::literal ($ arrayKey ),
118+ default => Argument::value ($ arrayKey ),
119+ };
120+ } else {
121+ // Array of values
122+ $ parameter = Argument::values ($ parameter );
123+ }
124+ } else {
125+ // Array of values
126+ $ parameter = Argument::values ($ parameter );
127+ }
128+ } elseif ($ parameter instanceof ExpressionInterface) {
129+ // Handle expressions/subqueries
130+ $ parameter = Argument::select ($ parameter );
131+ } else {
132+ $ parameter = Argument::value ($ parameter );
96133 }
97134
98135 $ this ->parameters [] = $ parameter ;
0 commit comments