Completed
Push — master ( cdb0a6...0b3050 )
by Song
10:42 queued 03:50
created

Mobile::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
class Mobile extends Text
6
{
7
    protected static $js = [
8
        '/vendor/laravel-admin/AdminLTE/plugins/input-mask/jquery.inputmask.bundle.min.js',
9
    ];
10
11
    /**
12
     * @see https://github.com/RobinHerbots/Inputmask#options
13
     *
14
     * @var array
15
     */
16
    protected $options = [
17
        'mask' => '99999999999',
18
    ];
19
20
    public function render()
21
    {
22
        $options = $this->json_encode_options($this->options);
23
24
        $this->script = <<<EOT
25
26
$('{$this->getElementClassSelector()}').inputmask($options);
27
EOT;
28
29
        $this->prepend('<i class="fa fa-phone"></i>')
30
            ->defaultAttribute('style', 'width: 150px');
31
32
        return parent::render();
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::render(); of type Illuminate\Contracts\Vie...ry|Illuminate\View\View adds the type Illuminate\Contracts\View\Factory to the return on line 32 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
33
    }
34
35
    protected function json_encode_options($options)
36
    {
37
        $value_arr = [];
38
        $replace_keys = [];
39
40
        foreach ($options as $key => &$value) {
41
            // Look for values starting with 'function('
42
            if (strpos($value, 'function(') === 0) {
43
                // Store function string.
44
                $value_arr[] = $value;
45
                // Replace function string in $foo with a 'unique' special key.
46
                $value = '%'.$key.'%';
47
                // Later on, we'll look for the value, and replace it.
48
                $replace_keys[] = '"'.$value.'"';
49
            }
50
        }
51
52
        // Now encode the array to json format
53
        $json = json_encode($options);
54
55
        // Replace the special keys with the original string.
56
        $json = str_replace($replace_keys, $value_arr, $json);
57
58
        return $json;
59
    }
60
}
61