1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Services; |
4
|
|
|
|
5
|
|
|
use Akibatech\Crud\Exceptions\DuplicateFieldIdentifierException; |
6
|
|
|
use Akibatech\Crud\Exceptions\NoFieldsException; |
7
|
|
|
use Akibatech\Crud\Fields\Field; |
8
|
|
|
use Illuminate\Support\MessageBag; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CrudFields |
12
|
|
|
* |
13
|
|
|
* @package Akibatech\Crud\Services |
14
|
|
|
*/ |
15
|
|
|
class CrudFields |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Field[] |
19
|
|
|
*/ |
20
|
|
|
protected $fields = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var CrudEntry |
24
|
|
|
*/ |
25
|
|
|
protected $entry; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var MessageBag |
29
|
|
|
*/ |
30
|
|
|
protected $errors; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var MessageBag |
34
|
|
|
*/ |
35
|
|
|
protected $old_input; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Make staticly a new instance. |
39
|
|
|
* |
40
|
|
|
* @param array $fields |
41
|
|
|
* @return CrudFields |
42
|
|
|
*/ |
43
|
|
|
public static function make(array $fields) |
44
|
|
|
{ |
45
|
|
|
return (new static)->add($fields); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param CrudEntry $entry |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
|
|
public function setEntry(CrudEntry $entry) |
53
|
|
|
{ |
54
|
|
|
$this->entry = $entry; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param void |
61
|
|
|
* @return CrudEntry |
62
|
|
|
*/ |
63
|
|
|
public function getEntry() |
64
|
|
|
{ |
65
|
|
|
return $this->entry; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Field[] $fields |
70
|
|
|
* @return self |
71
|
|
|
* @throws DuplicateFieldIdentifierException |
72
|
|
|
*/ |
73
|
|
|
public function add(array $fields) |
74
|
|
|
{ |
75
|
|
|
if (is_array($fields) && count($fields) > 0) |
76
|
|
|
{ |
77
|
|
|
/** @var Field $field */ |
78
|
|
|
foreach ($fields as $field) |
79
|
|
|
{ |
80
|
|
|
$this->set($field); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Field $field |
89
|
|
|
* @return self |
90
|
|
|
* @throws DuplicateFieldIdentifierException |
91
|
|
|
*/ |
92
|
|
|
public function set(Field $field) |
93
|
|
|
{ |
94
|
|
|
$identifier = $field->getIdentifier(); |
95
|
|
|
|
96
|
|
|
if ($this->has($identifier)) |
97
|
|
|
{ |
98
|
|
|
throw new DuplicateFieldIdentifierException("$identifier was already set."); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$field->setFields($this); |
102
|
|
|
$this->fields[$identifier] = $field; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param void |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
|
|
public function count() |
112
|
|
|
{ |
113
|
|
|
return count($this->fields); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $identifier |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function has($identifier) |
121
|
|
|
{ |
122
|
|
|
return array_key_exists($identifier, $this->fields) === true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param null|string $identifier |
127
|
|
|
* @return Field[]|Field|null |
128
|
|
|
*/ |
129
|
|
|
public function get($identifier = null) |
130
|
|
|
{ |
131
|
|
|
if (is_null($identifier)) |
132
|
|
|
{ |
133
|
|
|
return $this->fields; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ($this->has($identifier)) |
137
|
|
|
{ |
138
|
|
|
return $this->fields[$identifier]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return null; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param void |
146
|
|
|
* @return \Generator |
147
|
|
|
*/ |
148
|
|
|
public function columns() |
149
|
|
|
{ |
150
|
|
|
foreach ($this->fields as $field) |
151
|
|
|
{ |
152
|
|
|
yield $field->getLabel(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param void |
158
|
|
|
* @return \Generator |
159
|
|
|
*/ |
160
|
|
|
public function loop() |
161
|
|
|
{ |
162
|
|
|
foreach ($this->fields as $field) |
163
|
|
|
{ |
164
|
|
|
yield $field; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns all fields keys. |
170
|
|
|
* |
171
|
|
|
* @param void |
172
|
|
|
* @return integer[] |
173
|
|
|
*/ |
174
|
|
|
public function keys() |
175
|
|
|
{ |
176
|
|
|
return array_keys($this->fields); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param void |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
|
|
public function getFieldsScripts() |
184
|
|
|
{ |
185
|
|
|
$scripts = []; |
186
|
|
|
|
187
|
|
|
foreach ($this->fields as $field) |
188
|
|
|
{ |
189
|
|
|
$scripts = array_merge($scripts, $field->getScripts()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $scripts; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param void |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
|
|
public function getFieldsCss() |
200
|
|
|
{ |
201
|
|
|
$css = []; |
202
|
|
|
|
203
|
|
|
foreach ($this->fields as $field) |
204
|
|
|
{ |
205
|
|
|
$css = array_merge($css, $field->getCss()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $css; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Gets fields validation rules. |
213
|
|
|
* |
214
|
|
|
* @param void |
215
|
|
|
* @return array |
216
|
|
|
* @throws NoFieldsException |
217
|
|
|
*/ |
218
|
|
|
public function validationRules() |
219
|
|
|
{ |
220
|
|
|
$rules = []; |
221
|
|
|
|
222
|
|
|
if ($this->count() === 0) |
223
|
|
|
{ |
224
|
|
|
throw new NoFieldsException(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
foreach ($this->fields as $key => $field) |
228
|
|
|
{ |
229
|
|
|
$field_rules = $field->getRules(); |
230
|
|
|
|
231
|
|
|
if (!empty($field_rules)) |
232
|
|
|
{ |
233
|
|
|
$rules[$key] = $field_rules; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $rules; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Hydrate fields with new data. |
242
|
|
|
* |
243
|
|
|
* @param array $data |
244
|
|
|
* @return self |
245
|
|
|
*/ |
246
|
|
|
public function hydrateFormData(array $data) |
247
|
|
|
{ |
248
|
|
|
if (count($data) === 0) |
249
|
|
|
{ |
250
|
|
|
return $this; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
foreach ($this->fields as $field) |
254
|
|
|
{ |
255
|
|
|
if (array_key_exists($field->getIdentifier(), $data)) |
256
|
|
|
{ |
257
|
|
|
$field->newValue($data[$field->getIdentifier()]); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param void |
266
|
|
|
* @return MessageBag |
267
|
|
|
*/ |
268
|
|
|
public function getErrors() |
269
|
|
|
{ |
270
|
|
|
if (is_null($this->errors)) |
271
|
|
|
{ |
272
|
|
|
$this->errors = new MessageBag(); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $this->errors; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param void |
280
|
|
|
* @return MessageBag |
281
|
|
|
*/ |
282
|
|
|
public function getOldInput() |
283
|
|
|
{ |
284
|
|
|
if (is_null($this->old_input)) |
285
|
|
|
{ |
286
|
|
|
$this->old_input = new MessageBag(); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $this->old_input; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Hydrates the instance with previous validation errors. |
294
|
|
|
* |
295
|
|
|
* @param MessageBag $errors |
296
|
|
|
* @return self |
297
|
|
|
*/ |
298
|
|
View Code Duplication |
public function hydrateErrorsFromSession(MessageBag $errors = null) |
|
|
|
|
299
|
|
|
{ |
300
|
|
|
if (!is_null($errors)) |
301
|
|
|
{ |
302
|
|
|
$this->errors = $errors; |
303
|
|
|
return $this; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
$request = app()->make('request'); |
307
|
|
|
|
308
|
|
|
if ($request->session()->has('errors')) |
309
|
|
|
{ |
310
|
|
|
$this->errors = $request->session()->get('errors', MessageBag::class); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return $this; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Hydrates the instance with previous input. |
318
|
|
|
* |
319
|
|
|
* @param MessageBag $old_input |
320
|
|
|
* @return self |
321
|
|
|
*/ |
322
|
|
View Code Duplication |
public function hydrateFieldsFromSession(MessageBag $old_input = null) |
|
|
|
|
323
|
|
|
{ |
324
|
|
|
if (!is_null($old_input)) |
325
|
|
|
{ |
326
|
|
|
$this->old_input = $old_input; |
327
|
|
|
return $this; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$request = app()->make('request'); |
331
|
|
|
|
332
|
|
|
if ($request->session()->has('_old_input')) |
333
|
|
|
{ |
334
|
|
|
$this->old_input = new MessageBag($request->old()); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $this; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.