Completed
Pull Request — master (#15)
by Yaro
08:09 queued 01:44
created

anonymous//src/Jarboe/Table/Fields/Repeater.php$0   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
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
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
            array_set($errors, $key, $value);
0 ignored issues
show
Deprecated Code introduced by
The function array_set() has been deprecated with message: Arr::set() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
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)) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (offsetExists() instead of offsetGet()). Are you sure this is correct? If so, you might want to change this to $this->offsetExists().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
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