1
|
|
|
<?php namespace Anomaly\Streams\Platform\Ui\Form; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Support\Decorator; |
4
|
|
|
use Anomaly\Streams\Platform\Support\Hydrator; |
5
|
|
|
use Anomaly\Streams\Platform\Traits\FiresCallbacks; |
6
|
|
|
use Illuminate\Contracts\Cache\Repository; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class FormCriteria |
11
|
|
|
* |
12
|
|
|
* @link http://anomaly.is/streams-platform |
13
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
14
|
|
|
* @author Ryan Thompson <[email protected]> |
15
|
|
|
* @package Anomaly\Streams\Platform\Ui\Form |
16
|
|
|
*/ |
17
|
|
|
class FormCriteria |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
use FiresCallbacks; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The cache repository. |
24
|
|
|
* |
25
|
|
|
* @var Repository |
26
|
|
|
*/ |
27
|
|
|
protected $cache; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The form builder. |
31
|
|
|
* |
32
|
|
|
* @var FormBuilder |
33
|
|
|
*/ |
34
|
|
|
protected $builder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The request object. |
38
|
|
|
* |
39
|
|
|
* @var Request |
40
|
|
|
*/ |
41
|
|
|
protected $request; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The hydrator utility. |
45
|
|
|
* |
46
|
|
|
* @var Hydrator |
47
|
|
|
*/ |
48
|
|
|
protected $hydrator; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The parameters. |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $parameters = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create a new FormCriteria instance. |
59
|
|
|
* |
60
|
|
|
* @param Repository $cache |
61
|
|
|
* @param Request $request |
62
|
|
|
* @param Hydrator $hydrator |
63
|
|
|
* @param FormBuilder $builder |
64
|
|
|
* @param array $parameters |
65
|
|
|
*/ |
66
|
|
|
public function __construct( |
67
|
|
|
Repository $cache, |
68
|
|
|
Request $request, |
|
|
|
|
69
|
|
|
Hydrator $hydrator, |
70
|
|
|
FormBuilder $builder, |
71
|
|
|
array $parameters = [] |
72
|
|
|
) { |
73
|
|
|
$this->cache = $cache; |
74
|
|
|
$this->builder = $builder; |
75
|
|
|
$this->request = $request; |
76
|
|
|
$this->hydrator = $hydrator; |
77
|
|
|
$this->parameters = $parameters; |
78
|
|
|
|
79
|
|
|
$this->setBuilder($builder); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the form. |
84
|
|
|
* |
85
|
|
|
* @return FormPresenter |
86
|
|
|
*/ |
87
|
|
|
public function get() |
88
|
|
|
{ |
89
|
|
|
$this->builder(); |
90
|
|
|
|
91
|
|
|
return (new Decorator())->decorate($this->builder->make()->getForm()); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return the hydrated builder. |
96
|
|
|
* |
97
|
|
|
* @return FormBuilder |
98
|
|
|
*/ |
99
|
|
|
public function builder() |
100
|
|
|
{ |
101
|
|
|
$this->build(); |
102
|
|
|
|
103
|
|
|
return $this->builder->make(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Build the builder. |
108
|
|
|
*/ |
109
|
|
|
protected function build() |
110
|
|
|
{ |
111
|
|
|
$this->fire('ready', ['criteria' => $this]); |
112
|
|
|
|
113
|
|
|
array_set($this->parameters, 'key', md5(json_encode($this->parameters))); |
114
|
|
|
|
115
|
|
|
array_set( |
116
|
|
|
$this->parameters, |
117
|
|
|
'options.url', |
118
|
|
|
array_get( |
119
|
|
|
$this->parameters, |
120
|
|
|
'options.url', |
121
|
|
|
$this->builder->getOption('url', 'form/handle/' . array_get($this->parameters, 'key')) |
122
|
|
|
) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$this->cache->remember( |
126
|
|
|
'form::' . array_get($this->parameters, 'key'), |
127
|
|
|
30, |
128
|
|
|
function () { |
129
|
|
|
return $this->parameters; |
130
|
|
|
} |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
$this->hydrator->hydrate($this->builder, $this->parameters); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set a parameter. |
138
|
|
|
* |
139
|
|
|
* @param $key |
140
|
|
|
* @param $value |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function setParameter($key, $value) |
144
|
|
|
{ |
145
|
|
|
$this->parameters[$key] = $value; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the builder. |
152
|
|
|
* |
153
|
|
|
* @return FormBuilder |
154
|
|
|
*/ |
155
|
|
|
public function getBuilder() |
156
|
|
|
{ |
157
|
|
|
return $this->builder; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Set the form builder. |
162
|
|
|
* |
163
|
|
|
* @param FormBuilder $builder |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
|
public function setBuilder($builder) |
167
|
|
|
{ |
168
|
|
|
if (!is_string($builder)) { |
169
|
|
|
$builder = get_class($builder); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
array_set($this->parameters, 'builder', $builder); |
173
|
|
|
|
174
|
|
|
$this->builder = app($builder); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Route through __get |
181
|
|
|
* |
182
|
|
|
* @param $name |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
|
|
public function __get($name) |
186
|
|
|
{ |
187
|
|
|
return $this->__call($name, []); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param $name |
192
|
|
|
* @param $arguments |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
|
|
public function __call($name, $arguments) |
196
|
|
|
{ |
197
|
|
|
|
198
|
|
View Code Duplication |
if (method_exists($this->builder, camel_case('set_' . $name))) { |
|
|
|
|
199
|
|
|
|
200
|
|
|
array_set($this->parameters, $name, array_shift($arguments)); |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
View Code Duplication |
if (method_exists($this->builder, camel_case('add_' . $name))) { |
|
|
|
|
206
|
|
|
|
207
|
|
|
array_set($this->parameters, $name, array_shift($arguments)); |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
View Code Duplication |
if (!method_exists($this->builder, camel_case($name)) && count($arguments) === 1) { |
|
|
|
|
213
|
|
|
|
214
|
|
|
$key = snake_case($name); |
215
|
|
|
|
216
|
|
|
array_set($this->parameters, "options.{$key}", array_shift($arguments)); |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
View Code Duplication |
if (!method_exists($this->builder, camel_case($name)) && count($arguments) === 0) { |
|
|
|
|
222
|
|
|
|
223
|
|
|
$key = snake_case($name); |
224
|
|
|
|
225
|
|
|
// Helpful for form.disableLabels().disableFoo() ... |
|
|
|
|
226
|
|
|
array_set($this->parameters, "options.{$key}", true); |
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Return the form. |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function __toString() |
240
|
|
|
{ |
241
|
|
|
return $this->get()->__toString(); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|