1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Column; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Mockery\Matcher\Closure; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
9
|
|
|
use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface; |
10
|
|
|
|
11
|
|
|
class OrderByClause implements OrderByClauseInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string|Closure |
15
|
|
|
*/ |
16
|
|
|
protected $name; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* OrderByClause constructor. |
20
|
|
|
* |
21
|
|
|
* @param string|Closure $name |
22
|
|
|
*/ |
23
|
17 |
|
public function __construct($name) |
24
|
|
|
{ |
25
|
17 |
|
$this->setName($name); |
26
|
17 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Builder $query |
30
|
|
|
* @param string $direction |
31
|
|
|
*/ |
32
|
|
|
public function modifyQuery(Builder $query, $direction = 'asc') |
33
|
|
|
{ |
34
|
|
|
$this->name instanceof \Closure |
35
|
|
|
? $this->callCallable($query, $direction) |
36
|
|
|
: $this->callDefaultClause($query, $direction); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string|Closure $name |
41
|
|
|
* |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
17 |
|
public function setName($name) |
45
|
|
|
{ |
46
|
17 |
|
$this->name = $name; |
47
|
|
|
|
48
|
17 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Builder $query |
53
|
|
|
* @param string $direction |
54
|
|
|
*/ |
55
|
|
|
protected function callCallable(Builder $query, $direction) |
56
|
|
|
{ |
57
|
|
|
call_user_func_array($this->name, [$query, $direction]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $name |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
protected function isRelationName($name) |
65
|
|
|
{ |
66
|
|
|
return Str::contains($name, '.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* TODO: EagerLoad. |
71
|
|
|
*/ |
72
|
|
|
protected function eagerLoad() |
73
|
|
|
{ |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Load Relations by this->name. |
78
|
|
|
* @param Builder $query |
79
|
|
|
* @param $direction |
80
|
|
|
*/ |
81
|
|
|
protected function loadRelationOrder(Builder $query, $direction) |
82
|
|
|
{ |
83
|
|
|
$relations = collect(explode('.', $this->name)); |
84
|
|
|
|
85
|
|
|
//Without Eager Load |
86
|
|
|
//TODO: With Eager Load |
87
|
|
|
if ($relations->count() == 2) { |
88
|
|
|
$model = $query->getModel(); |
89
|
|
|
$relation = $relations->first(); |
90
|
|
|
|
91
|
|
|
if (method_exists($model, $relation)) { |
92
|
|
|
|
93
|
|
|
/** @var Relation $relationClass */ |
94
|
|
|
$relationClass = $model->{$relation}(); |
95
|
|
|
$relationModel = $relationClass->getRelated(); |
96
|
|
|
$foreignKey = $relationClass->getOwnerKey(); |
97
|
|
|
$ownerKey = $relationClass->getForeignKey(); |
98
|
|
|
$ownerTable = $model->getTable(); |
99
|
|
|
$foreignTable = $relationModel->getTable(); |
100
|
|
|
|
101
|
|
|
$ownerColumn = implode('.', [$ownerTable, $ownerKey]); |
102
|
|
|
$foreignColumn = implode('.', [$foreignTable, $foreignKey]); |
103
|
|
|
$sortedColumn = implode('.', [$foreignTable, $relations->last()]); |
|
|
|
|
104
|
|
|
|
105
|
|
|
$query->select([$ownerTable.'.*', $foreignTable.'.'.$relations->last()]) |
|
|
|
|
106
|
|
|
->join($foreignTable, $foreignColumn, '=', $ownerColumn, 'left') |
107
|
|
|
->orderBy($foreignColumn, $direction); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Builder $query |
114
|
|
|
* @param string $direction |
115
|
|
|
*/ |
116
|
|
|
protected function callDefaultClause(Builder $query, $direction) |
117
|
|
|
{ |
118
|
|
|
if ($this->isRelationName($this->name)) { |
119
|
|
|
$this->loadRelationOrder($query, $direction); |
120
|
|
|
} else { |
121
|
|
|
$query->orderBy($this->name, $direction); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.