@@ -409,7 +409,7 @@ protected function parseSelectPath()
409
409
{
410
410
$ selects = [];
411
411
foreach ($ this ->getSqlPart ($ this ->queryBuilder , 'select ' ) as $ part ) {
412
- $ selects = array_merge (array_map ( ' trim ' , explode ( ' , ' , $ part) ), $ selects );
412
+ $ selects = array_merge ($ this -> separateSelectString ( $ part ), $ selects );
413
413
}
414
414
$ selects = array_map (
415
415
function ($ str ) {
@@ -422,8 +422,41 @@ function ($str) {
422
422
$ selects
423
423
);
424
424
foreach ($ selects as $ select ) {
425
- $ array = explode (' ' , $ select );
426
- $ this ->aliasMapping [isset ($ array [1 ]) ? $ array [1 ] : $ array [0 ]] = $ array [0 ];
425
+ $ lastSpacePos = strrpos ($ select , ' ' );
426
+ $ body = substr ($ select , 0 , $ lastSpacePos );
427
+ $ alias = substr ($ select , $ lastSpacePos + 1 );
428
+ $ this ->aliasMapping [$ alias ?: $ body ] = $ body ;
427
429
}
428
430
}
431
+
432
+ /**
433
+ * @param string $string
434
+ *
435
+ * @return array
436
+ */
437
+ private function separateSelectString (string $ string )
438
+ {
439
+ if (false === strpos ($ string , '( ' )) {
440
+ return array_map ('trim ' , explode (', ' , $ string ));
441
+ }
442
+
443
+ $ opened = $ closed = 0 ;
444
+ $ cnt = mb_strlen ($ string );
445
+ $ arrayStrings = [];
446
+ $ substring = '' ;
447
+
448
+ for ($ i = 0 ; $ i < $ cnt ; $ i ++) {
449
+ $ substring .= $ string [$ i ];
450
+ if ('( ' === $ string [$ i ]) {
451
+ $ opened ++;
452
+ } elseif (') ' === $ string [$ i ]) {
453
+ $ closed ++;
454
+ } elseif (', ' === $ string [$ i ] && $ opened === $ closed ) {
455
+ $ arrayStrings [] = trim ($ substring );
456
+ }
457
+ }
458
+ $ arrayStrings [] = trim ($ substring );
459
+
460
+ return $ arrayStrings ;
461
+ }
429
462
}
0 commit comments