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

KeyValue::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 KeyValue extends Field
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $value = ['' => ''];
15
16
    /**
17
     * Fill data to the field.
18
     *
19
     * @param array $data
20
     *
21
     * @return void
22
     */
23 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...
24
    {
25
        $this->data = $data;
26
27
        $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...
28
29
        $this->formatValue();
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getValidator(array $input)
36
    {
37
        if ($this->validator) {
38
            return $this->validator->call($this, $input);
39
        }
40
41
        if (!is_string($this->column)) {
42
            return false;
43
        }
44
45
        $rules = $attributes = [];
46
47
        if (!$fieldRules = $this->getRules()) {
48
            return false;
49
        }
50
51
        if (!Arr::has($input, $this->column)) {
52
            return false;
53
        }
54
55
        $rules["{$this->column}.keys.*"] = 'distinct';
56
        $rules["{$this->column}.values.*"] = $fieldRules;
57
        $attributes["{$this->column}.keys.*"] = __('Key');
58
        $attributes["{$this->column}.values.*"] = __('Value');
59
60
        return validator($input, $rules, $this->getValidationMessages(), $attributes);
61
    }
62
63
    protected function setupScript()
64
    {
65
        $this->script = <<<SCRIPT
66
67
$('.{$this->column}-add').on('click', function () {
68
    var tpl = $('template.{$this->column}-tpl').html();
69
    $('tbody.kv-{$this->column}-table').append(tpl);
70
});
71
72
$('tbody').on('click', '.{$this->column}-remove', function () {
73
    $(this).closest('tr').remove();
74
});
75
76
SCRIPT;
77
    }
78
79
    public function prepare($value)
80
    {
81
        return array_combine($value['keys'], $value['values']);
82
    }
83
84
    public function render()
85
    {
86
        $this->setupScript();
87
88
        Admin::style('td .form-group {margin-bottom: 0 !important;}');
89
90
        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 90 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
91
    }
92
}
93