Completed
Push — master ( 88ab05...dae06d )
by Song
02:49
created

SwitchDisplay::display()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Displayers;
4
5
use Encore\Admin\Admin;
6
use Illuminate\Support\Arr;
7
8
class SwitchDisplay extends AbstractDisplayer
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $states = [
14
        'on'  => ['value' => 1, 'text' => 'ON', 'color' => 'primary'],
15
        'off' => ['value' => 0, 'text' => 'OFF', 'color' => 'default'],
16
    ];
17
18
    protected function overrideStates($states)
19
    {
20
        if (empty($states)) {
21
            return;
22
        }
23
24
        foreach (Arr::dot($states) as $key => $state) {
25
            Arr::set($this->states, $key, $state);
26
        }
27
    }
28
29
    public function display($states = [])
30
    {
31
        $this->overrideStates($states);
32
33
        return Admin::component('admin::grid.inline-edit.switch', [
34
            'class'    => 'grid-switch-' . str_replace('.', '-', $this->getName()),
35
            'key'      => $this->getKey(),
36
            'resource' => $this->getResource(),
37
            'name'     => $this->getPayloadName(),
38
            'states'   => $this->states,
39
            'checked'  => $this->states['on']['value'] == $this->getValue() ? 'checked' : '',
40
        ]);
41
    }
42
}
43