Completed
Push — master ( c30e79...8e747a )
by wen
13:09
created

Select::addOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use InvalidArgumentException;
8
use Sco\Admin\Contracts\RepositoryInterface;
9
10
class Select extends NamedElement
11
{
12
    protected $type = 'select';
13
14
    protected $size = '';
15
16
    protected $options;
17
18
    protected $extraOptions;
19
20
    protected $optionsLabelAttribute;
21
22
    protected $optionsValueAttribute;
23
24
    public function __construct($name, $title, $options)
25
    {
26
        parent::__construct($name, $title);
27
28
        $this->setOptions($options);
29
30
        $this->extraOptions = new Collection();
31
    }
32
33
    public function getSize()
34
    {
35
        return $this->size;
36
    }
37
38
    public function setSize($value)
39
    {
40
        $this->size = $value;
41
42
        return $this;
43
    }
44
45
    public function getValue()
46
    {
47
        return (string)parent::getValue();
48
    }
49
50
    protected function isOptionsModel()
51
    {
52
        return is_string($this->options) || $this->options instanceof Model;
53
    }
54
55
    protected function isRelation()
56
    {
57
        return method_exists($this->getModel(), $this->getName());
58
    }
59
60
    public function addOptions($options)
61
    {
62
        foreach ($options as $key => $option) {
63
            $this->addOption($key, $option);
64
        }
65
66
        return $this;
67
    }
68
69
    public function addOption($key, $value)
70
    {
71
        $this->extraOptions->put($key, $value);
72
73
        return $this;
74
    }
75
76
    public function getOptions()
77
    {
78 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...
79
            $options = ($this->options)();
80
        } elseif (is_string($this->options) || $this->options instanceof Model) {
81
            $options = $this->setOptionsFromModel();
82
        } elseif (is_array($this->options)) {
83
            $options = $this->options;
84
        } else {
85
            throw new InvalidArgumentException(
86
                "The form select element's options must be return array(key=>value)"
87
            );
88
        }
89
90
        //$options = collect($options)->merge();
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
92
        return collect($options)->mapWithKeys(function ($value, $key) {
93
            return [
94
                $key => [
95
                    'label' => $value,
96
                    'value' => (string)$key,
97
                ],
98
            ];
99
        })->values();
100
    }
101
102
    /**
103
     * @param mixed $options
104
     *
105
     * @return $this
106
     */
107
    public function setOptions($options)
108
    {
109
        $this->options = $options;
110
111
        return $this;
112
    }
113
114
    /**
115
     *
116
     * @return array
117
     */
118
    protected function setOptionsFromModel()
119
    {
120
        if (is_null(($label = $this->getOptionsLabelAttribute()))) {
121
            throw new InvalidArgumentException(
122
                sprintf(
123
                    'Form %s element must set label attribute',
124
                    $this->getType()
125
                )
126
            );
127
        }
128
129
        $model = $this->getOptionsModel();
130
131
        $repository = app(RepositoryInterface::class);
132
        $repository->setModel($model);
133
134
        $results = $repository->getQuery()->get();
135
136
        $key = $this->getOptionsValueAttribute() ?: $model->getKeyName();
137
138
        return $results->pluck($label, $key);
139
    }
140
141
    /**
142
     * @return Model
143
     */
144 View Code Duplication
    public function getOptionsModel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
145
    {
146
        $model = $this->options;
147
148
        if (is_string($model)) {
149
            $model = app($model);
150
        }
151
152
        if (!($model instanceof Model)) {
153
            throw new InvalidArgumentException(
154
                sprintf(
155
                    'Form %s element options class must be instanced of "%s".',
156
                    $this->getType(),
157
                    Model::class
158
                )
159
            );
160
        }
161
162
        return $model;
163
    }
164
165
    /**
166
     * 获取 options 标题字段
167
     *
168
     * @return string
169
     */
170
    public function getOptionsLabelAttribute()
171
    {
172
        return $this->optionsLabelAttribute;
173
    }
174
175
    /**
176
     * 设置 options 标题字段
177
     *
178
     * @param string $value
179
     *
180
     * @return $this
181
     */
182
    public function setOptionsLabelAttribute($value)
183
    {
184
        $this->optionsLabelAttribute = $value;
185
186
        return $this;
187
    }
188
189
    /**
190
     * 获取 options value 字段
191
     *
192
     * @return string
193
     */
194
    public function getOptionsValueAttribute()
195
    {
196
        return $this->optionsValueAttribute;
197
    }
198
199
    /**
200
     * 设置 options value 字段
201
     *
202
     * @param string $value
203
     *
204
     * @return $this
205
     */
206
    public function setOptionsValueAttribute($value)
207
    {
208
        $this->optionsValueAttribute = $value;
209
210
        return $this;
211
    }
212
213
    public function toArray()
214
    {
215
        return parent::toArray() + [
216
                'options' => $this->getOptions(),
217
                'size'    => $this->getSize(),
218
            ];
219
    }
220
}
221