Completed
Pull Request — master (#14)
by
unknown
15:13 queued 07:44
created

Repeater::getRepeaterItemFormView()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 1
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Repeater.php$0 ➔ offsetGet() 0 7 2
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
        {
148
            public function offsetGet($index)
149
            {
150
                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...
151
                    return null;
152
                }
153
                return parent::offsetGet($index);
154
            }
155
        };
156
157
        return view('jarboe::crud.fields.repeater.inc.item_edit', [
158
            'repeater' => $this,
159
            'model' => $model,
160
            'rowsLeft' => 12,
161
        ]);
162
    }
163
164
    public function heading(string $fieldName)
165
    {
166
        $this->headingName = $fieldName;
167
168
        return $this;
169
    }
170
171
    public function getHeadingName(): string
172
    {
173
        return $this->headingName;
174
    }
175
176
    public function prepare(CRUD $crud)
177
    {
178
        parent::prepare($crud);
179
180
        /** @var AbstractField $field */
181
        foreach ($this->getFields() as $field) {
182
            $field->prepare($crud);
183
        }
184
    }
185
}
186