Completed
Push — master ( ca14f0...c2f74e )
by Song
02:26
created

QuickCreate::password()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Tools;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Form\Field;
7
use Encore\Admin\Form\Field\MultipleSelect;
8
use Encore\Admin\Form\Field\Select;
9
use Encore\Admin\Form\Field\Text;
10
use Encore\Admin\Grid;
11
use Illuminate\Contracts\Support\Renderable;
12
use Illuminate\Support\Collection;
13
14
class QuickCreate implements Renderable
15
{
16
    /**
17
     * @var Grid
18
     */
19
    protected $parent;
20
21
    /**
22
     * @var Collection
23
     */
24
    protected $fields;
25
26
    /**
27
     * QuickCreate constructor.
28
     *
29
     * @param Grid $grid
30
     */
31
    public function __construct(Grid $grid)
32
    {
33
        $this->parent = $grid;
34
        $this->fields = Collection::make();
35
    }
36
37
    /**
38
     * @param string $column
39
     * @param string $placeholder
40
     *
41
     * @return Text
42
     */
43
    public function text($column, $placeholder = '')
44
    {
45
        $field = new Text($column, [$placeholder]);
46
47
        $this->addField($field->width('200px'));
48
49
        return $field;
50
    }
51
52
    /**
53
     * @param string $column
54
     * @param string $placeholder
55
     *
56
     * @return Text
57
     */
58
    public function number($column, $placeholder = '')
59
    {
60
        return $this->text($column, $placeholder)
61
            ->inputmask(['alias' => 'integer'])
62
            ->width('100px');
63
    }
64
65
    /**
66
     * @param string $column
67
     * @param string $placeholder
68
     *
69
     * @return Text
70
     */
71
    public function email($column, $placeholder = '')
72
    {
73
        return $this->text($column, $placeholder)
74
            ->inputmask(['alias' => 'email']);
75
    }
76
77
    /**
78
     * @param string $column
79
     * @param string $placeholder
80
     *
81
     * @return Text
82
     */
83
    public function ip($column, $placeholder = '')
84
    {
85
        return $this->text($column, $placeholder)
86
            ->inputmask(['alias' => 'ip'])
87
            ->width('120px');
88
    }
89
90
    /**
91
     * @param string $column
92
     * @param string $placeholder
93
     *
94
     * @return Text
95
     */
96
    public function url($column, $placeholder = '')
97
    {
98
        return $this->text($column, $placeholder)
99
            ->inputmask(['alias' => 'url']);
100
    }
101
102
    /**
103
     * @param string $column
104
     * @param string $placeholder
105
     *
106
     * @return Text
107
     */
108
    public function password($column, $placeholder = '')
109
    {
110
        return $this->text($column, $placeholder)
111
            ->attribute('type', 'password')
112
            ->width('100px');
113
    }
114
115
    /**
116
     * @param string $column
117
     * @param string $placeholder
118
     *
119
     * @return Text
120
     */
121
    public function mobile($column, $placeholder = '')
122
    {
123
        return $this->text($column, $placeholder)
124
            ->inputmask(['mask' => '99999999999'])
125
            ->width('100px');
126
    }
127
128
    /**
129
     * @param string $column
130
     * @param string $placeholder
131
     *
132
     * @return Text
133
     */
134
    public function integer($column, $placeholder = '')
135
    {
136
        return $this->text($column, $placeholder)
137
            ->inputmask(['alias' => 'integer'])
138
            ->width('120px');
139
    }
140
141
    /**
142
     * @param string $column
143
     * @param string $placeholder
144
     *
145
     * @return Select
146
     */
147
    public function select($column, $placeholder = '')
148
    {
149
        $field = new Select($column, [$placeholder]);
150
151
        $this->addField($field);
152
153
        return $field;
154
    }
155
156
    /**
157
     * @param string $column
158
     * @param string $placeholder
159
     *
160
     * @return MultipleSelect
161
     */
162
    public function multipleSelect($column, $placeholder = '')
163
    {
164
        $field = new MultipleSelect($column, [$placeholder]);
165
166
        $this->addField($field);
167
168
        return $field;
169
    }
170
171
    /**
172
     * @param string $column
173
     * @param string $placeholder
174
     *
175
     * @return Field\Date
176
     */
177
    public function datetime($column, $placeholder = '')
178
    {
179
        return $this->date($column, $placeholder)->format('YYYY-MM-DD HH:mm:ss');
180
    }
181
182
    /**
183
     * @param string $column
184
     * @param string $placeholder
185
     *
186
     * @return Field\Date
187
     */
188
    public function time($column, $placeholder = '')
189
    {
190
        return $this->date($column, $placeholder)->format('HH:mm:ss');
191
    }
192
193
    /**
194
     * @param string $column
195
     * @param string $placeholder
196
     *
197
     * @return Field\Date
198
     */
199
    public function date($column, $placeholder = '')
200
    {
201
        $field = new Field\Date($column, [$placeholder]);
202
203
        $this->addField($field);
204
205
        return $field;
206
    }
207
208
    /**
209
     * @param Field $field
210
     *
211
     * @return Field
212
     */
213
    protected function addField(Field $field)
214
    {
215
        $field->setView($this->resolveView(get_class($field)));
216
217
        $this->fields->push($field);
218
219
        return $field;
220
    }
221
222
    /**
223
     * @param string $class
224
     *
225
     * @return string
226
     */
227
    protected function resolveView($class)
228
    {
229
        $path = explode('\\', $class);
230
231
        $name = strtolower(array_pop($path));
232
233
        return "admin::grid.quick-create.{$name}";
234
    }
235
236
    protected function script()
237
    {
238
        $url = request()->url();
239
240
        $script = <<<SCRIPT
241
242
(function () {
243
244
    $('.quick-create .create').click(function () {
245
        $('.quick-create .create-form').show();
246
        $(this).hide();
247
    });
248
    
249
    $('.quick-create .cancel').click(function () {
250
        $('.quick-create .create-form').hide();
251
        $('.quick-create .create').show();
252
    });
253
    
254
    $('.quick-create .create-form').submit(function (e) {
255
    
256
        e.preventDefault();
257
    
258
        $.ajax({
259
            url: '{$url}',
260
            type: 'POST',
261
            data: $(this).serialize(),
262
            success: function(data, textStatus, jqXHR) {
263
                console.info(data);
264
                
265
                if (data.status == true) {
266
                    $.admin.toastr.success(data.message, '', {positionClass:"toast-top-center"});
267
                    $.admin.reload();
268
                    return;
269
                }
270
                
271
                if (typeof data.validation !== 'undefined') {
272
                    $.admin.toastr.warning(data.message, '', {positionClass:"toast-top-center"})
273
                }
274
            },
275
            error:function(XMLHttpRequest, textStatus){
276
                if (typeof XMLHttpRequest.responseJSON === 'object') {
277
                    $.admin.toastr.error(XMLHttpRequest.responseJSON.message, '', {positionClass:"toast-top-center", timeOut: 10000});
278
                }
279
            }
280
        });
281
        
282
        return false;
283
    });
284
285
})();
286
287
SCRIPT;
288
289
        Admin::script($script);
290
291
    }
292
293
    /**
294
     * @param int $columnCount
295
     *
296
     * @return array|string
297
     */
298
    public function render($columnCount = 0)
299
    {
300
        if ($this->fields->isEmpty()) {
301
            return '';
302
        }
303
304
        $this->script();
305
306
        $vars = [
307
            'columnCount' => $columnCount,
308
            'fields'      => $this->fields,
309
        ];
310
311
        return view('admin::grid.quick-create.form', $vars)->render();
0 ignored issues
show
Bug introduced by
The method render 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...
312
    }
313
}