|
2 | 2 |
|
3 | 3 | use Exception; |
4 | 4 | use GeneaLabs\LaravelModelCaching\Traits\CachePrefixing; |
| 5 | +use Illuminate\Database\Query\Expression; |
5 | 6 | use Illuminate\Support\Arr; |
6 | 7 | use Illuminate\Support\Collection; |
7 | 8 | use Illuminate\Support\Str; |
@@ -101,6 +102,14 @@ protected function getColumnClauses(array $where) : string |
101 | 102 | if ($where["type"] !== "Column") { |
102 | 103 | return ""; |
103 | 104 | } |
| 105 | + |
| 106 | + if ($where["first"] instanceof Expression) { |
| 107 | + $where["first"] = $this->expressionToString($where["first"]); |
| 108 | + } |
| 109 | + |
| 110 | + if ($where["second"] instanceof Expression) { |
| 111 | + $where["second"] = $this->expressionToString($where["second"]); |
| 112 | + } |
104 | 113 |
|
105 | 114 | return "-{$where["boolean"]}_{$where["first"]}_{$where["operator"]}_{$where["second"]}"; |
106 | 115 | } |
@@ -205,14 +214,19 @@ protected function getOrderByClauses() : string |
205 | 214 | } |
206 | 215 |
|
207 | 216 | $orders = collect($this->query->orders); |
208 | | - |
| 217 | + |
209 | 218 | return $orders |
210 | 219 | ->reduce(function ($carry, $order) { |
211 | 220 | if (($order["type"] ?? "") === "Raw") { |
212 | 221 | return $carry . "_orderByRaw_" . (new Str)->slug($order["sql"]); |
213 | 222 | } |
214 | 223 |
|
215 | | - return $carry . "_orderBy_" . $order["column"] . "_" . $order["direction"]; |
| 224 | + return sprintf( |
| 225 | + '%s_orderBy_%s_%s', |
| 226 | + $carry, |
| 227 | + $this->expressionToString($order["column"]), |
| 228 | + $order["direction"] |
| 229 | + ); |
216 | 230 | }) |
217 | 231 | ?: ""; |
218 | 232 | } |
@@ -246,7 +260,11 @@ protected function getQueryColumns(array $columns) : string |
246 | 260 | if (property_exists($this->query, "columns") |
247 | 261 | && $this->query->columns |
248 | 262 | ) { |
249 | | - return "_" . implode("_", $this->query->columns); |
| 263 | + $columns = array_map(function ($column) { |
| 264 | + return $this->expressionToString($column); |
| 265 | + }, $this->query->columns); |
| 266 | + |
| 267 | + return "_" . implode("_", $columns); |
250 | 268 | } |
251 | 269 |
|
252 | 270 | return "_" . implode("_", $columns); |
@@ -315,14 +333,18 @@ protected function getValuesFromWhere(array $where) : string |
315 | 333 | } |
316 | 334 |
|
317 | 335 | if (is_array((new Arr)->get($where, "values"))) { |
318 | | - return implode("_", collect($where["values"])->flatten()->toArray()); |
| 336 | + $values = collect($where["values"])->flatten()->toArray(); |
| 337 | + return implode("_", $this->processEnums($values)); |
319 | 338 | } |
320 | 339 |
|
321 | 340 | if (is_array((new Arr)->get($where, "value"))) { |
322 | | - return implode("_", collect($where["value"])->flatten()->toArray()); |
| 341 | + $values = collect($where["value"])->flatten()->toArray(); |
| 342 | + return implode("_", $this->processEnums($values)); |
323 | 343 | } |
324 | 344 |
|
325 | | - return (new Arr)->get($where, "value", ""); |
| 345 | + $value = (new Arr)->get($where, "value", ""); |
| 346 | + |
| 347 | + return $this->processEnum($value); |
326 | 348 | } |
327 | 349 |
|
328 | 350 | protected function getValuesFromBindings(array $where, string $values) : string |
@@ -423,4 +445,31 @@ protected function recursiveImplode(array $items, string $glue = ",") : string |
423 | 445 |
|
424 | 446 | return $result; |
425 | 447 | } |
| 448 | + |
| 449 | + private function processEnum(\BackedEnum|\UnitEnum|Expression|string $value): string |
| 450 | + { |
| 451 | + if ($value instanceof \BackedEnum) { |
| 452 | + return $value->value; |
| 453 | + } elseif ($value instanceof \UnitEnum) { |
| 454 | + return $value->name; |
| 455 | + } elseif ($value instanceof Expression) { |
| 456 | + return $this->expressionToString($value); |
| 457 | + } |
| 458 | + |
| 459 | + return $value; |
| 460 | + } |
| 461 | + |
| 462 | + private function processEnums(array $values): array |
| 463 | + { |
| 464 | + return array_map(fn($value) => $this->processEnum($value), $values); |
| 465 | + } |
| 466 | + |
| 467 | + private function expressionToString(Expression|string $value): string |
| 468 | + { |
| 469 | + if (is_string($value)) { |
| 470 | + return $value; |
| 471 | + } |
| 472 | + |
| 473 | + return $value->getValue($this->query->getConnection()->getQueryGrammar()); |
| 474 | + } |
426 | 475 | } |
0 commit comments