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) { |
|
|
|
|
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
|
|
|
|
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.