Completed
Push — master ( adb78f...0848e3 )
by Song
03:00
created

Text::json_encode_options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Form\Field;
6
7
class Text extends Field
8
{
9
    use PlainInput;
10
11
    /**
12
     * Render this filed.
13
     *
14
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
15
     */
16
    public function render()
17
    {
18
        $this->initPlainInput();
19
20
        $this->prepend('<i class="fa fa-pencil fa-fw"></i>')
21
            ->defaultAttribute('type', 'text')
22
            ->defaultAttribute('id', $this->id)
23
            ->defaultAttribute('name', $this->elementName ?: $this->formatName($this->column))
0 ignored issues
show
Bug introduced by
It seems like $this->column can also be of type array; however, Encore\Admin\Form\Field::formatName() 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...
24
            ->defaultAttribute('value', old($this->column, $this->value()))
0 ignored issues
show
Bug introduced by
It seems like $this->column can also be of type array; however, old() does only seem to accept string|null, 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...
25
            ->defaultAttribute('class', 'form-control '.$this->getElementClassString())
26
            ->defaultAttribute('placeholder', $this->getPlaceholder());
27
28
        $this->addVariables([
29
            'prepend' => $this->prepend,
30
            'append'  => $this->append,
31
        ]);
32
33
        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 33 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
34
    }
35
36
    /**
37
     * Add inputmask to an elements.
38
     *
39
     * @param array $options
40
     *
41
     * @return $this
42
     */
43
    public function inputmask($options)
44
    {
45
        $options = $this->json_encode_options($options);
46
47
        $this->script = "$('{$this->getElementClassSelector()}').inputmask($options);";
48
49
        return $this;
50
    }
51
52
    /**
53
     * Encode options to Json.
54
     *
55
     * @param array $options
56
     *
57
     * @return $json
0 ignored issues
show
Documentation introduced by
The doc-type $json could not be parsed: Unknown type name "$json" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
58
     */
59
    protected function json_encode_options($options)
60
    {
61
        $data = $this->prepare_options($options);
62
63
        $json = json_encode($data['options']);
64
65
        $json = str_replace($data['toReplace'], $data['original'], $json);
66
67
        return $json;
68
    }
69
70
    /**
71
     * Prepare options.
72
     *
73
     * @param array $options
74
     *
75
     * @return array
76
     */
77
    protected function prepare_options($options)
78
    {
79
        $original = [];
80
        $toReplace = [];
81
82
        foreach ($options as $key => &$value) {
83
            if (is_array($value)) {
84
                $subArray = $this->prepare_options($value);
85
                $value = $subArray['options'];
86
                $original = array_merge($original, $subArray['original']);
87
                $toReplace = array_merge($toReplace, $subArray['toReplace']);
88
            } elseif (preg_match('/function.*?/', $value)) {
89
                $original[] = $value;
90
                $value = "%{$key}%";
91
                $toReplace[] = "\"{$value}\"";
92
            }
93
        }
94
95
        return compact('original', 'toReplace', 'options');    
96
    }
97
  
98
    public function datalist( $entries = [] )
99
    {
100
        $this->defaultAttribute('list', "list-{$this->id}");
101
102
        $datalist = "<datalist id=\"list-{$this->id}\">";
103
        foreach($entries as $k => $v) {
104
            $datalist .= "<option value=\"{$k}\">{$v}</option>";
105
        }
106
        $datalist .= "</datalist>";
107
        $this->append($datalist);
108
    }
109
}
110