Completed
Push — master ( a41be1...cd8884 )
by wen
12:14
created

Select   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 166
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSize() 0 4 1
A setSize() 0 6 1
A getValue() 0 4 1
B getOptions() 0 26 5
A setOptions() 0 6 1
B setOptionsFromModel() 0 27 5
A getOptionsLabelAttribute() 0 4 1
A setOptionsLabelAttribute() 0 6 1
A getOptionsValueAttribute() 0 4 1
A setOptionsValueAttribute() 0 6 1
A toArray() 0 7 1
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Illuminate\Database\Eloquent\Model;
6
use InvalidArgumentException;
7
use Sco\Admin\Contracts\RepositoryInterface;
8
9
class Select extends NamedElement
10
{
11
    protected $type = 'select';
12
13
    protected $options = [];
14
15
    protected $optionsLabelAttribute;
16
17
    protected $optionsValueAttribute;
18
19
    protected $size = '';
20
21
    public function __construct($name, $title, $options)
22
    {
23
        parent::__construct($name, $title);
24
        $this->setOptions($options);
25
    }
26
27
    public function getSize()
28
    {
29
        return $this->size;
30
    }
31
32
    public function setSize($value)
33
    {
34
        $this->size = $value;
35
36
        return $this;
37
    }
38
39
    public function getValue()
40
    {
41
        return (string)parent::getValue();
42
    }
43
44
    /**
45
     * @return \Illuminate\Support\Collection
46
     */
47
    public function getOptions()
48
    {
49
        if ($this->options instanceof \Closure) {
50
            $options = ($this->options)();
51
        } elseif (is_string($this->options) || $this->options instanceof Model) {
52
            $options = $this->setOptionsFromModel($this->options);
53
        } elseif (is_array($this->options)) {
54
            $options = $this->options;
55
        } else {
56
            throw new InvalidArgumentException(
57
                sprintf(
58
                    "The form %s element's options must be return array(key=>value)",
59
                    $this->getType()
60
                )
61
            );
62
        }
63
64
        return collect($options)->mapWithKeys(function ($value, $key) {
65
            return [
66
                $key => [
67
                    'label' => $value,
68
                    'value' => (string)$key,
69
                ],
70
            ];
71
        })->values();
72
    }
73
74
    /**
75
     * @param mixed $options
76
     *
77
     * @return $this
78
     */
79
    public function setOptions($options)
80
    {
81
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options of type * is incompatible with the declared type array of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param Model|string $options
88
     *
89
     * @return array
90
     */
91
    protected function setOptionsFromModel($options)
92
    {
93
        if (is_string($options)) {
94
            $options = app($options);
95
        }
96
97
        if (!($options instanceof Model)) {
98
            throw new InvalidArgumentException(
99
                sprintf(
100
                    'Form select element options class must be instanced of "%s".',
101
                    Model::class
102
                )
103
            );
104
        }
105
106
        $key = $this->getOptionsValueAttribute() ?: $options->getKeyName();
107
108
        $repository = app(RepositoryInterface::class);
109
        $repository->setModel($options);
110
        $query = $repository->getQuery();
111
112
        $options = $query->get();
113
        if (is_null(($label = $this->getOptionsLabelAttribute()))) {
114
            throw new InvalidArgumentException('Form select element must set label attribute');
115
        }
116
        return $options->pluck($this->getOptionsLabelAttribute(), $key);
117
    }
118
119
    /**
120
     * 获取 options 标题字段
121
     *
122
     * @return string
123
     */
124
    public function getOptionsLabelAttribute()
125
    {
126
        return $this->optionsLabelAttribute;
127
    }
128
129
    /**
130
     * 设置 options 标题字段
131
     *
132
     * @param string $value
133
     *
134
     * @return $this
135
     */
136
    public function setOptionsLabelAttribute($value)
137
    {
138
        $this->optionsLabelAttribute = $value;
139
140
        return $this;
141
    }
142
143
    /**
144
     * 获取 options value 字段
145
     *
146
     * @return string
147
     */
148
    public function getOptionsValueAttribute()
149
    {
150
        return $this->optionsValueAttribute;
151
    }
152
153
    /**
154
     * 设置 options value 字段
155
     *
156
     * @param string $value
157
     *
158
     * @return $this
159
     */
160
    public function setOptionsValueAttribute($value)
161
    {
162
        $this->optionsValueAttribute = $value;
163
164
        return $this;
165
    }
166
167
    public function toArray()
168
    {
169
        return parent::toArray() + [
170
                'options' => $this->getOptions(),
171
                'size'    => $this->getSize(),
172
            ];
173
    }
174
}
175