1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
trait SelectOptionsFromModel |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $optionsLabelAttribute; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $optionsValueAttribute; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get the options label attribute. |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public function getOptionsLabelAttribute() |
26
|
|
|
{ |
27
|
|
|
return $this->optionsLabelAttribute; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set the options label attribute. |
32
|
|
|
* |
33
|
|
|
* @param string $value |
34
|
|
|
* |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
public function setOptionsLabelAttribute($value) |
38
|
|
|
{ |
39
|
|
|
$this->optionsLabelAttribute = $value; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the options value attribute. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getOptionsValueAttribute() |
50
|
|
|
{ |
51
|
|
|
return $this->optionsValueAttribute; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the options value attribute. |
56
|
|
|
* |
57
|
|
|
* @param string $value |
58
|
|
|
* |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function setOptionsValueAttribute($value) |
62
|
|
|
{ |
63
|
|
|
$this->optionsValueAttribute = $value; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the options model. |
70
|
|
|
* |
71
|
|
|
* @return Model |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function getOptionsModel() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$model = $this->options; |
|
|
|
|
76
|
|
|
|
77
|
|
|
if (is_string($model)) { |
78
|
|
|
$model = app($model); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (! ($model instanceof Model)) { |
82
|
|
|
throw new InvalidArgumentException( |
83
|
|
|
sprintf( |
84
|
|
|
'The %s element[%s] options class must be instanced of "%s".', |
85
|
|
|
$this->getType(), |
|
|
|
|
86
|
|
|
$this->getName(), |
|
|
|
|
87
|
|
|
Model::class |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $model; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function isOptionsModel() |
96
|
|
|
{ |
97
|
|
|
return is_string($this->options) || $this->options instanceof Model; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the options from model. |
102
|
|
|
* |
103
|
|
|
* @return \Illuminate\Support\Collection |
104
|
|
|
*/ |
105
|
|
|
protected function getOptionsFromModel() |
106
|
|
|
{ |
107
|
|
|
if (is_null(($label = $this->getOptionsLabelAttribute()))) { |
108
|
|
|
throw new InvalidArgumentException( |
109
|
|
|
sprintf( |
110
|
|
|
'The %s element[%s] options must set label attribute', |
111
|
|
|
$this->getType(), |
|
|
|
|
112
|
|
|
$this->getName() |
|
|
|
|
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$model = $this->getOptionsModel(); |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @var \Illuminate\Support\Collection $results |
121
|
|
|
*/ |
122
|
|
|
$results = $model->get(); |
123
|
|
|
|
124
|
|
|
$key = $this->getOptionsValueAttribute() ?: $model->getKeyName(); |
125
|
|
|
|
126
|
|
|
return $results->pluck($label, $key); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.