Repeater   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 77
c 2
b 1
f 0
dl 0
loc 172
rs 10
wmc 8

20 Methods

Rating   Name   Duplication   Size   Complexity  
heading() 0 5 ?
A getCreateFormView() 0 5 1
A hp$0 ➔ offsetGet() 0 6 2
A hp$0 ➔ getHeadingName() 0 3 1
A getEditFormView() 0 6 1
A hp$0 ➔ heading() 0 5 1
A hp$0 ➔ prepare() 0 7 2
A addField() 0 10 2
A beforeUpdate() 0 3 1
prepare() 0 7 ?
B value() 0 39 8
A errors() 0 13 3
A isSortable() 0 3 1
A hp$0 ➔ getRepeaterItemFormView() 0 16 2
A sortable() 0 5 1
A getListView() 0 6 1
getHeadingName() 0 3 ?
A fields() 0 7 2
getRepeaterItemFormView() 0 16 ?
A getFields() 0 3 1
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\CRUD;
8
use Yaro\Jarboe\Table\Fields\Traits\Translatable;
9
10
class Repeater extends AbstractField
0 ignored issues
show
Bug introduced by
The type Yaro\Jarboe\Table\Fields\AbstractField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
{
12
    use Translatable;
13
14
    private $fields = [];
15
    private $sortable = false;
16
    private $modelObject;
17
    private $headingName = '';
18
19
    public function sortable(bool $enable = true)
20
    {
21
        $this->sortable = $enable;
22
23
        return $this;
24
    }
25
26
    public function isSortable(): bool
27
    {
28
        return $this->sortable;
29
    }
30
31
    public function fields(array $fields)
32
    {
33
        foreach ($fields as $field) {
34
            $this->addField($field);
35
        }
36
37
        return $this;
38
    }
39
40
    public function addField(AbstractField $field)
41
    {
42
        $adapter = $field::$repeaterAdapterClass;
43
        if ($adapter) {
44
            $field = new $adapter($field);
45
        }
46
47
        $this->fields[] = $field;
48
49
        return $this;
50
    }
51
52
    public function beforeUpdate($model)
53
    {
54
        $this->modelObject = $model;
55
    }
56
57
    public function value(Request $request)
58
    {
59
        $repeaterItems = Arr::get($request->all($this->name()), $this->name());
60
        $repeaterItems = is_array($repeaterItems) ? $repeaterItems : [];
61
62
        $data = [];
63
        if ($this->isTranslatable()) {
64
            foreach ($repeaterItems as $locale => $items) {
65
                $localeData = [];
66
                foreach ($items as $item) {
67
                    $itemData = [];
68
                    /** @var AbstractField $field */
69
                    foreach ($this->getFields() as $field) {
70
                        $itemData[$field->name()] = $field->value(
71
                            (new Request([], [], [], [], array_filter($item, function ($item) {
72
                                return is_object($item);
73
                            })))->replace($item)
74
                        );
75
                    }
76
                    $localeData[] = $itemData;
77
                }
78
                $data[$locale] = $localeData;
79
            }
80
        } else {
81
            $index = 0;
82
            foreach ($repeaterItems as $item) {
83
                $itemData = [];
84
                /** @var AbstractField $field */
85
                foreach ($this->getFields() as $field) {
86
                    $itemData[$field->name()] = $field->value(
87
                        (new Request())->replace($item)
88
                    );
89
                }
90
                $data[] = $itemData;
91
                $index++;
92
            }
93
        }
94
95
        return $data;
96
    }
97
98
    public function getFields(): array
99
    {
100
        return $this->fields;
101
    }
102
103
    public function getListView($model)
104
    {
105
        return view('jarboe::crud.fields.repeater.list', [
106
            'crud' => $this->crud(),
107
            'repeater' => $this,
108
            'model' => $model,
109
        ]);
110
    }
111
112
    public function getEditFormView($model)
113
    {
114
        return view('jarboe::crud.fields.repeater.edit', [
115
            'crud' => $this->crud(),
116
            'repeater' => $this,
117
            'model' => $model,
118
        ]);
119
    }
120
121
    public function getCreateFormView()
122
    {
123
        return view('jarboe::crud.fields.repeater.create', [
124
            'crud' => $this->crud(),
125
            'repeater' => $this,
126
        ]);
127
    }
128
129
    public function errors(array $messages): array
130
    {
131
        $errors = [];
132
        foreach ($messages as $key => $value) {
133
            foreach ($value as &$error) {
134
                $parts = explode('.', $key);
135
                $name = array_pop($parts);
136
                $error = str_replace($key, $name, $error);
137
            }
138
            Arr::set($errors, $key, $value);
139
        }
140
141
        return $errors[$this->name()] ?? [];
142
    }
143
144
    public function getRepeaterItemFormView(array $data)
145
    {
146
        $model = new class($data, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS) extends \ArrayObject {
147
            public function offsetGet($index)
148
            {
149
                if (!parent::offsetExists($index)) {
150
                    return null;
151
                }
152
                return parent::offsetGet($index);
153
            }
154
        };
155
156
        return view('jarboe::crud.fields.repeater.inc.item_edit', [
157
            'repeater' => $this,
158
            'model' => $model,
159
            'rowsLeft' => 12,
160
        ]);
161
    }
162
163
    public function heading(string $fieldName)
164
    {
165
        $this->headingName = $fieldName;
166
167
        return $this;
168
    }
169
170
    public function getHeadingName(): string
171
    {
172
        return $this->headingName;
173
    }
174
175
    public function prepare(CRUD $crud)
176
    {
177
        parent::prepare($crud);
178
179
        /** @var AbstractField $field */
180
        foreach ($this->getFields() as $field) {
181
            $field->prepare($crud);
182
        }
183
    }
184
}
185