1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sco\Admin\Form\Elements; |
5
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
8
|
|
|
use Sco\Admin\Exceptions\InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
class Select extends NamedElement |
11
|
|
|
{ |
12
|
|
|
protected $type = 'select'; |
13
|
|
|
|
14
|
|
|
protected $options = []; |
15
|
|
|
|
16
|
|
|
protected $optionsLabelAttribute; |
17
|
|
|
|
18
|
|
|
protected $optionsValueAttribute; |
19
|
|
|
|
20
|
|
|
protected $size = ''; |
21
|
|
|
|
22
|
|
|
public function __construct($name, $title, $options) |
23
|
|
|
{ |
24
|
|
|
parent::__construct($name, $title); |
25
|
|
|
$this->setOptions($options); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getSize() |
29
|
|
|
{ |
30
|
|
|
return $this->size; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setSize($value) |
34
|
|
|
{ |
35
|
|
|
$this->size = $value; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return \Illuminate\Support\Collection |
42
|
|
|
*/ |
43
|
|
|
public function getOptions() |
44
|
|
|
{ |
45
|
|
|
if ($this->options instanceof \Closure) { |
46
|
|
|
$options = $this->options(); |
|
|
|
|
47
|
|
|
} elseif (is_string($this->options) || $this->options instanceof Model) { |
48
|
|
|
$options = $this->setOptionsFromModel($this->options); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!is_array($options)) { |
|
|
|
|
52
|
|
|
throw new InvalidArgumentException('Form select element options must be array(key=>value)'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return collect($options)->mapWithKeys(function ($value, $key) { |
56
|
|
|
return [ |
57
|
|
|
$key => [ |
58
|
|
|
'label' => $value, |
59
|
|
|
'value' => $key, |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
})->values(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param mixed $options |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function setOptions($options) |
71
|
|
|
{ |
72
|
|
|
$this->options = $options; |
|
|
|
|
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Model|string $options |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
protected function setOptionsFromModel($options) |
83
|
|
|
{ |
84
|
|
|
if (is_string($options)) { |
85
|
|
|
$options = app($options); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!($options instanceof Model)) { |
89
|
|
|
throw new InvalidArgumentException('Form select element options class must be instanced of Illuminate\Database\Eloquent\Model'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$key = $this->getOptionsValueAttribute() ?: $options->getKeyName(); |
93
|
|
|
|
94
|
|
|
$repository = app(RepositoryInterface::class); |
95
|
|
|
$repository->setModel($options); |
96
|
|
|
$query = $repository->getQuery(); |
97
|
|
|
|
98
|
|
|
$options = $query->get(); |
99
|
|
|
if (is_null(($label = $this->getOptionsLabelAttribute()))) { |
100
|
|
|
throw new InvalidArgumentException('Form select element must set label attribute'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return array_pluck($options->all(), $this->getOptionsLabelAttribute(), $key); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* 获取 options 标题字段 |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getOptionsLabelAttribute() |
112
|
|
|
{ |
113
|
|
|
return $this->optionsLabelAttribute; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* 设置 options 标题字段 |
118
|
|
|
* |
119
|
|
|
* @param string $value |
120
|
|
|
* |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function setOptionsLabelAttribute($value) |
124
|
|
|
{ |
125
|
|
|
$this->optionsLabelAttribute = $value; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* 获取 options value 字段 |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getOptionsValueAttribute() |
136
|
|
|
{ |
137
|
|
|
return $this->optionsValueAttribute; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* 设置 options value 字段 |
142
|
|
|
* |
143
|
|
|
* @param string $value |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function setOptionsValueAttribute($value) |
148
|
|
|
{ |
149
|
|
|
$this->optionsValueAttribute = $value; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function toArray() |
155
|
|
|
{ |
156
|
|
|
return parent::toArray() + [ |
157
|
|
|
'options' => $this->getOptions(), |
158
|
|
|
'size' => $this->getSize(), |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.