|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace BoredProgrammers\LaravelEloquentMappable\Models\Scopes; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
| 6 | +use Illuminate\Database\Eloquent\Model; |
| 7 | +use Illuminate\Database\Eloquent\Scope; |
| 8 | +use Illuminate\Database\Query\Builder as QueryBuilder; |
| 9 | +use Illuminate\Database\Query\JoinClause; |
| 10 | + |
| 11 | +class MappedColumnsScope implements Scope |
| 12 | +{ |
| 13 | + |
| 14 | + public function apply(EloquentBuilder $builder, Model $model): void |
| 15 | + { |
| 16 | + $builder->beforeQuery(function (QueryBuilder $builder) use ($model) { |
| 17 | + if (!$mappedColToDbCol = $model->columns ?? null) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + $modelTable = $model->getTable(); |
| 22 | + $mappedColToDbColWithTable = collect($mappedColToDbCol) |
| 23 | + ->mapWithKeys(fn($value, $key) => [$modelTable . '.' . $key => $modelTable . '.' . $value]) |
| 24 | + ->merge($mappedColToDbCol); |
| 25 | + |
| 26 | + $dbColToMappedCol = $mappedColToDbColWithTable->flip(); |
| 27 | + |
| 28 | + $builder->columns = $this->mapColumns( |
| 29 | + $builder->columns, |
| 30 | + $mappedColToDbColWithTable, |
| 31 | + $dbColToMappedCol, |
| 32 | + $model |
| 33 | + ); |
| 34 | + |
| 35 | + $builder->wheres = $this->mapColumns( |
| 36 | + $builder->wheres, |
| 37 | + $mappedColToDbColWithTable, |
| 38 | + $dbColToMappedCol, |
| 39 | + $model |
| 40 | + ); |
| 41 | + |
| 42 | + $builder->joins = $this->mapJoins($builder->joins, $mappedColToDbColWithTable); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + private function mapColumns($array, $mappedColToDbColWithTable, $dbColToMappedCol, $model) |
| 47 | + { |
| 48 | + if (!$array) { |
| 49 | + return $array; |
| 50 | + } |
| 51 | + |
| 52 | + foreach ($array as $key => $column) { |
| 53 | + $columnName = is_array($column) ? $column['column'] : $column; |
| 54 | + |
| 55 | + if ($columnName === '*' || str_contains($columnName, ' as ')) { // todo: add support for column aliases |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + if ($newColumnName = $mappedColToDbColWithTable[$columnName] ?? null) { |
| 60 | + if (is_array($column)) { |
| 61 | + $array[$key]['column'] = $newColumnName; |
| 62 | + } else { |
| 63 | + $array[$key] = $newColumnName; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return $array; |
| 69 | + } |
| 70 | + |
| 71 | + private function mapJoins($joins, $mappedColToDbColWithTable) |
| 72 | + { |
| 73 | + if (!$joins) { |
| 74 | + return $joins; |
| 75 | + } |
| 76 | + |
| 77 | + /** @var JoinClause $joinClause */ |
| 78 | + foreach ($joins as $joinClause) { |
| 79 | + foreach ($joinClause->wheres as $whereKey => $whereData) { |
| 80 | + if ($whereData['type'] === 'Column') { |
| 81 | + if ($dbCol = $mappedColToDbColWithTable[$whereData['first']] ?? null) { |
| 82 | + $joinClause->wheres[$whereKey]['first'] = $dbCol; |
| 83 | + } |
| 84 | + |
| 85 | + if ($dbCol = $mappedColToDbColWithTable[$whereData['second']] ?? null) { |
| 86 | + $joinClause->wheres[$whereKey]['second'] = $dbCol; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if ($joinClause->joins) { |
| 92 | + $joinClause->joins = $this->mapJoins($joinClause->joins, $mappedColToDbColWithTable); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return $joins; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments