Completed
Push — master ( 82be35...1537c0 )
by Song
02:28
created

src/Form/Field/Embeds.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Form\EmbeddedForm;
6
use Encore\Admin\Form\Field;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Str;
9
10
class Embeds extends Field
11
{
12
    /**
13
     * @var \Closure
14
     */
15
    protected $builder = null;
16
17
    /**
18
     * Create a new HasMany field instance.
19
     *
20
     * @param string $column
21
     * @param array  $arguments
22
     */
23 View Code Duplication
    public function __construct($column, $arguments = [])
24
    {
25
        $this->column = $column;
26
27
        if (count($arguments) == 1) {
28
            $this->label = $this->formatLabel();
29
            $this->builder = $arguments[0];
30
        }
31
32
        if (count($arguments) == 2) {
33
            list($this->label, $this->builder) = $arguments;
34
        }
35
    }
36
37
    /**
38
     * Prepare input data for insert or update.
39
     *
40
     * @param array $input
41
     *
42
     * @return array
43
     */
44
    public function prepare($input)
45
    {
46
        $form = $this->buildEmbeddedForm();
47
48
        return $form->setOriginal($this->original)->prepare($input);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getValidator(array $input)
55
    {
56
        if (!array_key_exists($this->column, $input)) {
57
            return false;
58
        }
59
60
        $input = Arr::only($input, $this->column);
61
62
        $rules = $attributes = [];
63
64
        /** @var Field $field */
65
        foreach ($this->buildEmbeddedForm()->fields() as $field) {
66
            if (!$fieldRules = $field->getRules()) {
67
                continue;
68
            }
69
70
            $column = $field->column();
71
72
            /*
73
             *
74
             * For single column field format rules to:
75
             * [
76
             *     'extra.name' => 'required'
77
             *     'extra.email' => 'required'
78
             * ]
79
             *
80
             * For multiple column field with rules like 'required':
81
             * 'extra' => [
82
             *     'start' => 'start_at'
83
             *     'end'   => 'end_at',
84
             * ]
85
             *
86
             * format rules to:
87
             * [
88
             *     'extra.start_atstart' => 'required'
89
             *     'extra.end_atend' => 'required'
90
             * ]
91
             */
92
            if (is_array($column)) {
93
                foreach ($column as $key => $name) {
94
                    $rules["{$this->column}.$name$key"] = $fieldRules;
95
                }
96
97
                $this->resetInputKey($input, $column);
98
            } else {
99
                $rules["{$this->column}.$column"] = $fieldRules;
100
            }
101
102
            /**
103
             * For single column field format attributes to:
104
             * [
105
             *     'extra.name' => $label
106
             *     'extra.email' => $label
107
             * ].
108
             *
109
             * For multiple column field with rules like 'required':
110
             * 'extra' => [
111
             *     'start' => 'start_at'
112
             *     'end'   => 'end_at',
113
             * ]
114
             *
115
             * format rules to:
116
             * [
117
             *     'extra.start_atstart' => "$label[start_at]"
118
             *     'extra.end_atend' => "$label[end_at]"
119
             * ]
120
             */
121
            $attributes = array_merge(
122
                $attributes,
123
                $this->formatValidationAttribute($input, $field->label(), $column)
124
            );
125
        }
126
127
        if (empty($rules)) {
128
            return false;
129
        }
130
131
        return \validator($input, $rules, $this->getValidationMessages(), $attributes);
132
    }
133
134
    /**
135
     * Format validation attributes.
136
     *
137
     * @param array  $input
138
     * @param string $label
139
     * @param string $column
140
     *
141
     * @return array
142
     */
143 View Code Duplication
    protected function formatValidationAttribute($input, $label, $column)
144
    {
145
        $new = $attributes = [];
146
147
        if (is_array($column)) {
148
            foreach ($column as $index => $col) {
149
                $new[$col.$index] = $col;
150
            }
151
        }
152
153
        foreach (array_keys(Arr::dot($input)) as $key) {
154
            if (is_string($column)) {
155
                if (Str::endsWith($key, ".$column")) {
156
                    $attributes[$key] = $label;
157
                }
158
            } else {
159
                foreach ($new as $k => $val) {
160
                    if (Str::endsWith($key, ".$k")) {
161
                        $attributes[$key] = $label."[$val]";
162
                    }
163
                }
164
            }
165
        }
166
167
        return $attributes;
168
    }
169
170
    /**
171
     * Reset input key for validation.
172
     *
173
     * @param array $input
174
     * @param array $column $column is the column name array set
175
     *
176
     * @return void.
177
     */
178
    public function resetInputKey(array &$input, array $column)
179
    {
180
        $column = array_flip($column);
181
182 View Code Duplication
        foreach ($input[$this->column] as $key => $value) {
183
            if (!array_key_exists($key, $column)) {
184
                continue;
185
            }
186
187
            $newKey = $key.$column[$key];
188
189
            /*
190
             * set new key
191
             */
192
            Arr::set($input, "{$this->column}.$newKey", $value);
193
            /*
194
             * forget the old key and value
195
             */
196
            Arr::forget($input, "{$this->column}.$key");
197
        }
198
    }
199
200
    /**
201
     * Get data for Embedded form.
202
     *
203
     * Normally, data is obtained from the database.
204
     *
205
     * When the data validation errors, data is obtained from session flash.
206
     *
207
     * @return array
208
     */
209
    protected function getEmbeddedData()
210
    {
211
        if ($old = old($this->column)) {
212
            return $old;
213
        }
214
215
        if (empty($this->value)) {
216
            return [];
217
        }
218
219
        if (is_string($this->value)) {
220
            return json_decode($this->value, true);
221
        }
222
223
        return (array) $this->value;
224
    }
225
226
    /**
227
     * Build a Embedded Form and fill data.
228
     *
229
     * @return EmbeddedForm
230
     */
231
    protected function buildEmbeddedForm()
232
    {
233
        $form = new EmbeddedForm($this->column);
234
235
        $form->setParent($this->form);
236
237
        call_user_func($this->builder, $form);
238
239
        $form->fill($this->getEmbeddedData());
240
241
        return $form;
242
    }
243
244
    /**
245
     * Render the form.
246
     *
247
     * @return \Illuminate\View\View
248
     */
249
    public function render()
250
    {
251
        return parent::render()->with(['form' => $this->buildEmbeddedForm()]);
0 ignored issues
show
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
252
    }
253
}
254