1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MarcusCampos\Dealer; |
4
|
|
|
|
5
|
|
|
use MarcusCampos\Dealer\Parser; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
|
8
|
|
|
class Negotiation |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var Parser |
13
|
|
|
*/ |
14
|
|
|
private $parser; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $stack; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
23
|
|
|
*/ |
24
|
|
|
private $model; |
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->parser = new Parser(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Negotiate |
33
|
|
|
* |
34
|
|
|
* @param string $query |
35
|
|
|
* @return Collection |
36
|
|
|
*/ |
37
|
|
|
public function negotiate(string $query) |
38
|
|
|
{ |
39
|
|
|
$this->stack = $this->parser->parse($query); |
40
|
|
|
|
41
|
|
|
$namespace = config('dealer.models.namespace') ?? 'App\\'; |
42
|
|
|
$this->model = app($namespace.$this->stack['model']); |
43
|
|
|
|
44
|
|
|
$this->model = $this->relations() |
45
|
|
|
->filters() |
46
|
|
|
->limit() |
47
|
|
|
->groupBy() |
48
|
|
|
->orderBy() |
49
|
|
|
->get(); |
50
|
|
|
|
51
|
|
|
return $this->only($this->model, $this->stack['fields']['only']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Return only selected elements |
56
|
|
|
* |
57
|
|
|
* @param Collection $collection |
58
|
|
|
* @param array $fields |
59
|
|
|
* @return Collection |
60
|
|
|
*/ |
61
|
|
|
private function only(Collection $collection, array $fields) |
62
|
|
|
{ |
63
|
|
|
$fields = array_filter($fields); |
64
|
|
|
|
65
|
|
|
if(!$fields || $fields[0] == '*') { |
|
|
|
|
66
|
|
|
return $collection; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $collection->map(function ($model) use($fields) { |
70
|
|
|
return $model->only($fields); |
71
|
|
|
}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get relations |
76
|
|
|
* |
77
|
|
|
* @return Negotiation |
78
|
|
|
*/ |
79
|
|
|
private function relations() |
80
|
|
|
{ |
81
|
|
|
$relations = []; |
82
|
|
|
|
83
|
|
|
if (array_key_exists('extends', $this->stack['fields'])) { |
84
|
|
|
foreach ($this->stack['fields']['extends'] as $value) { |
85
|
|
|
$this->stack['fields']['only'][] = $value['name']; |
86
|
|
|
|
87
|
|
|
if (!$value['args'] || $value['args'][0] == '*') { |
88
|
|
|
$relations[] = $value['name']; |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
$relations[] = $value['name'] . ':' . implode(',', $value['args']); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->model = $this->model->with($relations); |
|
|
|
|
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set filters |
102
|
|
|
* |
103
|
|
|
* @return Negotiation |
104
|
|
|
*/ |
105
|
|
|
private function filters() |
106
|
|
|
{ |
107
|
|
|
$relations = []; |
|
|
|
|
108
|
|
|
|
109
|
|
|
if (array_key_exists('filters', $this->stack)) { |
110
|
|
|
if (array_key_exists('extends', $this->stack['filters'])) { |
111
|
|
|
foreach ($this->stack['filters']['extends'] as $value) { |
112
|
|
|
$methodName = $value['name']; |
113
|
|
|
|
114
|
|
|
if (!$value['args']) { |
115
|
|
|
$this->model = $this->model->$value['name'](); |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->model = $this->model->$methodName(...$value['args']); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set the sort |
129
|
|
|
* |
130
|
|
|
* @return Negotiation |
131
|
|
|
*/ |
132
|
|
|
private function orderBy() |
133
|
|
|
{ |
134
|
|
|
if (array_key_exists('orderBy', $this->stack)) { |
135
|
|
|
$this->model = $this->model->orderBy($this->stack['orderBy'][0],$this->stack['orderBy'][1]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set limit |
143
|
|
|
* |
144
|
|
|
* @return Negotiation |
145
|
|
|
*/ |
146
|
|
|
private function limit() |
147
|
|
|
{ |
148
|
|
|
if (array_key_exists('limit', $this->stack)) { |
149
|
|
|
$this->model = $this->model->limit($this->stack['limit']); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set groups |
157
|
|
|
* |
158
|
|
|
* @return Negotiation |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
private function groupBy() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
if (array_key_exists('groupBy', $this->stack)) { |
163
|
|
|
$this->model = $this->model->groupBy($this->stack['groupBy']); |
164
|
|
|
} |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get data |
170
|
|
|
* |
171
|
|
|
* @return Collection |
172
|
|
|
*/ |
173
|
|
View Code Duplication |
private function get() |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
if (array_key_exists('paginate', $this->stack)) { |
176
|
|
|
return $this->model->paginate($this->stack['paginate']); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $this->model->get(); |
180
|
|
|
} |
181
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.