Completed
Push — master ( c7a24e...b9d51d )
by Song
03:23
created

ListField::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Form\Field;
7
use Illuminate\Support\Arr;
8
9
class ListField extends Field
10
{
11
    /**
12
     * Max list size.
13
     *
14
     * @var int
15
     */
16
    protected $max;
17
18
    /**
19
     * Minimum list size.
20
     *
21
     * @var int
22
     */
23
    protected $min = 0;
24
25
    /**
26
     * @var array
27
     */
28
    protected $value = [''];
29
30
    /**
31
     * Set Max list size.
32
     *
33
     * @param int $size
34
     *
35
     * @return $this
36
     */
37
    public function max(int $size)
38
    {
39
        $this->max = $size;
40
41
        return $this;
42
    }
43
44
    /**
45
     * Set Minimum list size.
46
     *
47
     * @param int $size
48
     *
49
     * @return $this
50
     */
51
    public function min(int $size)
52
    {
53
        $this->min = $size;
54
55
        return $this;
56
    }
57
58
    /**
59
     * Fill data to the field.
60
     *
61
     * @param array $data
62
     *
63
     * @return void
64
     */
65 View Code Duplication
    public function fill($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $this->data = $data;
68
69
        $this->value = Arr::get($data, $this->column, $this->value);
0 ignored issues
show
Bug introduced by
It seems like $this->column can also be of type array; however, Illuminate\Support\Arr::get() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Documentation Bug introduced by
It seems like \Illuminate\Support\Arr:...->column, $this->value) of type * is incompatible with the declared type array of property $value.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
71
        $this->formatValue();
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getValidator(array $input)
78
    {
79
        if ($this->validator) {
80
            return $this->validator->call($this, $input);
81
        }
82
83
        if (!is_string($this->column)) {
84
            return false;
85
        }
86
87
        $rules = $attributes = [];
88
89
        if (!$fieldRules = $this->getRules()) {
90
            return false;
91
        }
92
93
        if (!Arr::has($input, $this->column)) {
94
            return false;
95
        }
96
97
        $rules["{$this->column}.values.*"] = $fieldRules;
98
        $attributes["{$this->column}.values.*"] = __('Value');
99
100
        $rules["{$this->column}.values"][] = 'array';
101
102 View Code Duplication
        if (!is_null($this->max)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
            $rules["{$this->column}.values"][] = "max:$this->max";
104
        }
105
106 View Code Duplication
        if (!is_null($this->min)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
            $rules["{$this->column}.values"][] = "min:$this->min";
108
        }
109
110
        $attributes["{$this->column}.values"] = $this->label;
111
112
        return validator($input, $rules, $this->getValidationMessages(), $attributes);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    protected function setupScript()
119
    {
120
        $this->script = <<<SCRIPT
121
122
$('.{$this->column}-add').on('click', function () {
123
    var tpl = $('template.{$this->column}-tpl').html();
124
    $('tbody.list-{$this->column}-table').append(tpl);
125
});
126
127
$('tbody').on('click', '.{$this->column}-remove', function () {
128
    $(this).closest('tr').remove();
129
});
130
131
SCRIPT;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function prepare($value)
138
    {
139
        return array_values($value['values']);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function render()
146
    {
147
        $this->setupScript();
148
149
        Admin::style('td .form-group {margin-bottom: 0 !important;}');
150
151
        return parent::render();
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::render(); of type string|Illuminate\View\V...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 151 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
152
    }
153
}
154