Completed
Push — master ( 650783...0c9838 )
by wen
15:06
created

Select::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Sco\Admin\Display\Filters;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Sco\Admin\Traits\SelectOptionsFromModel;
7
8
class Select extends Filter
9
{
10
    use SelectOptionsFromModel;
11
12
    protected $type = 'select';
13
14
    protected $options;
15
16
    protected $defaultValue = '';
17
18
    public function __construct($name, $title, $options = null)
19
    {
20
        parent::__construct($name, $title);
21
22
        $this->setOptions($options);
23
    }
24
25
    /**
26
     * @return mixed
27
     */
28
    public function getOptions()
29
    {
30 View Code Duplication
        if ($this->options instanceof \Closure) {
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...
31
            $options = ($this->options)();
32
        } elseif (is_string($this->options) || $this->options instanceof Model) {
33
            $options = $this->getOptionsFromModel();
34
        } elseif (is_array($this->options)) {
35
            $options = $this->options;
36
        } else {
37
            throw new InvalidArgumentException(
38
                "The select options must be return array(key=>value)"
39
            );
40
        }
41
42
        return collect($options)->mapWithKeys(function ($value, $key) {
43
            return [
44
                $key => [
45
                    'label' => $value,
46
                    'value' => (string) $key,
47
                ],
48
            ];
49
        })->values();
50
    }
51
52
    /**
53
     * @param mixed $options
54
     *
55
     * @return Select
56
     */
57
    public function setOptions($options)
58
    {
59
        $this->options = $options;
60
61
        return $this;
62
    }
63
64
    public function toArray()
65
    {
66
        return parent::toArray() + [
67
                'options' => $this->getOptions(),
68
            ];
69
    }
70
}
71