Completed
Push — master ( fe60d0...675c3f )
by wen
11:50
created

Tree::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
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 Tree extends NamedElement
10
{
11
    protected $type = 'tree';
12
13
    protected $options = [];
14
15
    protected $defaultValue = [];
16
17
    protected $optionsLabelAttribute;
18
19
    protected $optionsValueAttribute;
20
21
    public function __construct($name, $title, $options)
22
    {
23
        parent::__construct($name, $title);
24
25
        $this->setOptions($options);
26
    }
27
28 View Code Duplication
    public function getValue()
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...
29
    {
30
        $value = $this->getValueFromModel();
31
        if (empty($value)) {
32
            return $value;
33
        }
34
35
        if ($this->isOptionsModel() && $this->isRelation()) {
36
            $model = $this->getOptionsModel();
37
            $key = $this->getOptionsValueAttribute() ?: $model->getKeyName();
38
39
            $value = $value->pluck($key)->map(function ($item) {
40
                return (string)$item;
41
            });
42
        }
43
        return $value;
44
    }
45
46
    protected function isOptionsModel()
47
    {
48
        return is_string($this->options) || $this->options instanceof Model;
49
    }
50
51
    protected function isRelation()
52
    {
53
        return method_exists($this->getModel(), $this->getName());
54
    }
55
56 View Code Duplication
    public function getOptions()
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...
57
    {
58
        if ($this->options instanceof \Closure) {
59
            $options = ($this->options)();
60
        } elseif (is_string($this->options) || $this->options instanceof Model) {
61
            $options = $this->setOptionsFromModel();
62
        } elseif (is_array($this->options)) {
63
            $options = $this->options;
64
        } else {
65
            throw new InvalidArgumentException(
66
                sprintf(
67
                    "The form %s element's options must be return array(key=>value)",
68
                    $this->getType()
69
                )
70
            );
71
        }
72
73
        return collect($options)->mapWithKeys(function ($value, $key) {
74
            return [
75
                $key => [
76
                    'id' => (string)$key,
77
                    'label' => $value,
78
                ],
79
            ];
80
        })->values();
81
    }
82
83
    /**
84
     * @param mixed $options
85
     *
86
     * @return $this
87
     */
88
    public function setOptions($options)
89
    {
90
        $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...
91
92
        return $this;
93
    }
94
95
    /**
96
     *
97
     * @return array
98
     */
99 View Code Duplication
    protected function setOptionsFromModel()
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...
100
    {
101
        $model = $this->getOptionsModel();
102
103
        $key = $this->getOptionsValueAttribute() ?: $model->getKeyName();
104
105
        $repository = app(RepositoryInterface::class);
106
        $repository->setModel($model);
107
        $query = $repository->getQuery();
108
109
        $results = $query->get();
110
        if (is_null(($label = $this->getOptionsLabelAttribute()))) {
111
            throw new InvalidArgumentException('Form select element must set label attribute');
112
        }
113
        return $results->pluck($this->getOptionsLabelAttribute(), $key);
114
    }
115
116
    /**
117
     * @return Model
118
     */
119 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...
120
    {
121
        $model = $this->options;
122
123
        if (is_string($model)) {
124
            $model = app($model);
125
        }
126
127
        if (!($model instanceof Model)) {
128
            throw new InvalidArgumentException(
129
                sprintf(
130
                    'Form select element options class must be instanced of "%s".',
131
                    Model::class
132
                )
133
            );
134
        }
135
136
        return $model;
137
    }
138
139
    /**
140
     * 获取 options 标题字段
141
     *
142
     * @return string
143
     */
144
    public function getOptionsLabelAttribute()
145
    {
146
        return $this->optionsLabelAttribute;
147
    }
148
149
    /**
150
     * 设置 options 标题字段
151
     *
152
     * @param string $value
153
     *
154
     * @return $this
155
     */
156
    public function setOptionsLabelAttribute($value)
157
    {
158
        $this->optionsLabelAttribute = $value;
159
160
        return $this;
161
    }
162
163
    /**
164
     * 获取 options value 字段
165
     *
166
     * @return string
167
     */
168
    public function getOptionsValueAttribute()
169
    {
170
        return $this->optionsValueAttribute;
171
    }
172
173
    /**
174
     * 设置 options value 字段
175
     *
176
     * @param string $value
177
     *
178
     * @return $this
179
     */
180
    public function setOptionsValueAttribute($value)
181
    {
182
        $this->optionsValueAttribute = $value;
183
184
        return $this;
185
    }
186
187
    public function toArray()
188
    {
189
        return parent::toArray() + [
190
            'options' => $this->getOptions(),
191
        ];
192
    }
193
}
194