1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form\Elements\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
8
|
|
|
|
9
|
|
|
trait HasOptions |
10
|
|
|
{ |
11
|
|
|
protected $options = []; |
12
|
|
|
|
13
|
|
|
protected $optionsLabelAttribute; |
14
|
|
|
|
15
|
|
|
protected $optionsValueAttribute; |
16
|
|
|
|
17
|
|
|
protected function isOptionsModel() |
18
|
|
|
{ |
19
|
|
|
return is_string($this->options) || $this->options instanceof Model; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function isRelation() |
23
|
|
|
{ |
24
|
|
|
return method_exists($this->getModel(), $this->getName()); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getOptions() |
28
|
|
|
{ |
29
|
|
|
if ($this->options instanceof \Closure) { |
30
|
|
|
$options = ($this->options)(); |
31
|
|
|
} elseif (is_string($this->options) || $this->options instanceof Model) { |
32
|
|
|
$options = $this->setOptionsFromModel(); |
33
|
|
|
} elseif (is_array($this->options)) { |
34
|
|
|
$options = $this->options; |
35
|
|
|
} else { |
36
|
|
|
throw new InvalidArgumentException( |
37
|
|
|
sprintf( |
38
|
|
|
"The form %s element's options must be return array(key=>value)", |
39
|
|
|
$this->getType() |
|
|
|
|
40
|
|
|
) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->parseOptions($options); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function parseOptions($options) |
48
|
|
|
{ |
49
|
|
|
return $options; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param mixed $options |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function setOptions($options) |
58
|
|
|
{ |
59
|
|
|
$this->options = $options; |
|
|
|
|
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
protected function setOptionsFromModel() |
69
|
|
|
{ |
70
|
|
|
$model = $this->getOptionsModel(); |
71
|
|
|
|
72
|
|
|
$key = $this->getOptionsValueAttribute() ?: $model->getKeyName(); |
73
|
|
|
|
74
|
|
|
$repository = app(RepositoryInterface::class); |
75
|
|
|
$repository->setModel($model); |
76
|
|
|
$query = $repository->getQuery(); |
77
|
|
|
|
78
|
|
|
$results = $query->get(); |
79
|
|
|
if (is_null(($label = $this->getOptionsLabelAttribute()))) { |
80
|
|
|
throw new InvalidArgumentException( |
81
|
|
|
sprintf( |
82
|
|
|
'Form %s element must set label attribute', |
83
|
|
|
$this->getType() |
|
|
|
|
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
return $results->pluck($this->getOptionsLabelAttribute(), $key); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Model |
92
|
|
|
*/ |
93
|
|
|
public function getOptionsModel() |
94
|
|
|
{ |
95
|
|
|
$model = $this->options; |
96
|
|
|
|
97
|
|
|
if (is_string($model)) { |
98
|
|
|
$model = app($model); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!($model instanceof Model)) { |
102
|
|
|
throw new InvalidArgumentException( |
103
|
|
|
sprintf( |
104
|
|
|
'Form %s element options class must be instanced of "%s".', |
105
|
|
|
$this->getType(), |
|
|
|
|
106
|
|
|
Model::class |
107
|
|
|
) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $model; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* 获取 options 标题字段 |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function getOptionsLabelAttribute() |
120
|
|
|
{ |
121
|
|
|
return $this->optionsLabelAttribute; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* 设置 options 标题字段 |
126
|
|
|
* |
127
|
|
|
* @param string $value |
128
|
|
|
* |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function setOptionsLabelAttribute($value) |
132
|
|
|
{ |
133
|
|
|
$this->optionsLabelAttribute = $value; |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* 获取 options value 字段 |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getOptionsValueAttribute() |
144
|
|
|
{ |
145
|
|
|
return $this->optionsValueAttribute; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* 设置 options value 字段 |
150
|
|
|
* |
151
|
|
|
* @param string $value |
152
|
|
|
* |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
|
|
public function setOptionsValueAttribute($value) |
156
|
|
|
{ |
157
|
|
|
$this->optionsValueAttribute = $value; |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.