|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Table\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Yaro\Jarboe\Table\Fields\Adapters\RepeaterFile; |
|
8
|
|
|
use Yaro\Jarboe\Table\Fields\Traits\Translatable; |
|
9
|
|
|
|
|
10
|
|
|
class Repeater extends AbstractField |
|
11
|
|
|
{ |
|
12
|
|
|
use Translatable; |
|
13
|
|
|
|
|
14
|
|
|
private $fields = []; |
|
15
|
|
|
private $sortable = false; |
|
16
|
|
|
|
|
17
|
|
|
public function sortable(bool $enable = true) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->sortable = $enable; |
|
20
|
|
|
|
|
21
|
|
|
return $this; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function isSortable(): bool |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->sortable; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function fields(array $fields) |
|
30
|
|
|
{ |
|
31
|
|
|
foreach ($fields as $field) { |
|
32
|
|
|
$this->addField($field); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function addField(AbstractField $field) |
|
39
|
|
|
{ |
|
40
|
|
|
if (is_a($field, File::class)) { |
|
41
|
|
|
$field = new RepeaterFile($field); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
$this->fields[] = $field; |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function value(Request $request) |
|
49
|
|
|
{ |
|
50
|
|
|
$repeaterItems = Arr::get($request->all($this->name()), $this->name()); |
|
51
|
|
|
$repeaterItems = is_array($repeaterItems) ? $repeaterItems : []; |
|
52
|
|
|
|
|
53
|
|
|
$data = []; |
|
54
|
|
|
if ($this->isTranslatable()) { |
|
55
|
|
|
foreach ($repeaterItems as $locale => $items) { |
|
56
|
|
|
$localeData = []; |
|
57
|
|
|
foreach ($items as $item) { |
|
58
|
|
|
$itemData = []; |
|
59
|
|
|
/** @var AbstractField $field */ |
|
60
|
|
|
foreach ($this->getFields() as $field) { |
|
61
|
|
|
$itemData[$field->name()] = $field->value( |
|
62
|
|
|
(new Request([], [], [], [], array_filter($item, function ($item) { |
|
63
|
|
|
return is_object($item); |
|
64
|
|
|
})))->replace($item) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
$localeData[] = $itemData; |
|
68
|
|
|
} |
|
69
|
|
|
$data[$locale] = $localeData; |
|
70
|
|
|
} |
|
71
|
|
|
} else { |
|
72
|
|
|
foreach ($repeaterItems as $item) { |
|
73
|
|
|
$itemData = []; |
|
74
|
|
|
/** @var AbstractField $field */ |
|
75
|
|
|
foreach ($this->getFields() as $field) { |
|
76
|
|
|
$itemData[$field->name()] = $field->value( |
|
77
|
|
|
(new Request())->replace($item) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
$data[] = $itemData; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $data; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getFields(): array |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->fields; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getListValue($model) |
|
93
|
|
|
{ |
|
94
|
|
|
return view('jarboe::crud.fields.repeater.list', [ |
|
95
|
|
|
'repeater' => $this, |
|
96
|
|
|
'model' => $model, |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function getEditFormValue($model) |
|
101
|
|
|
{ |
|
102
|
|
|
return view('jarboe::crud.fields.repeater.edit', [ |
|
103
|
|
|
'model' => $model, |
|
104
|
|
|
'repeater' => $this, |
|
105
|
|
|
]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getCreateFormValue() |
|
109
|
|
|
{ |
|
110
|
|
|
return view('jarboe::crud.fields.repeater.create', [ |
|
111
|
|
|
'repeater' => $this, |
|
112
|
|
|
]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function errors($messages) |
|
116
|
|
|
{ |
|
117
|
|
|
$repeaterMessages = []; |
|
118
|
|
|
foreach ($messages as $name => $errors) { |
|
119
|
|
|
$errors = array_map(function ($message) use ($name) { |
|
120
|
|
|
$parts = explode('.', $name); |
|
121
|
|
|
$trueName = array_pop($parts); |
|
122
|
|
|
return preg_replace('~' . preg_quote($name) . '~', $trueName, $message); |
|
123
|
|
|
}, $errors); |
|
124
|
|
|
|
|
125
|
|
|
$name = preg_replace('~\.~', '][', $name) . ']'; |
|
126
|
|
|
$name = preg_replace('~\]~', '', $name, 1); |
|
127
|
|
|
$repeaterMessages[$name] = $errors; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $repeaterMessages; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.