Completed
Push — master ( 4809fd...17a5ab )
by wen
02:54
created

Select::setOptionsValueAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
4
namespace Sco\Admin\Form\Elements;
5
6
use Illuminate\Database\Eloquent\Model;
7
use Sco\Admin\Contracts\RepositoryInterface;
8
use Sco\Admin\Exceptions\InvalidArgumentException;
9
10
class Select extends NamedElement
11
{
12
    protected $type = 'select';
13
14
    protected $options = [];
15
16
    protected $optionsLabelAttribute;
17
18
    protected $optionsValueAttribute;
19
20
    protected $size = '';
21
22
    public function __construct($name, $title, $options)
23
    {
24
        parent::__construct($name, $title);
25
        $this->setOptions($options);
26
    }
27
28
    public function getSize()
29
    {
30
        return $this->size;
31
    }
32
33
    public function setSize($value)
34
    {
35
        $this->size = $value;
36
37
        return $this;
38
    }
39
40
    /**
41
     * @return \Illuminate\Support\Collection
42
     */
43
    public function getOptions()
44
    {
45
        if ($this->options instanceof \Closure) {
46
            $options = $this->options();
0 ignored issues
show
Bug introduced by
The method options() does not exist on Sco\Admin\Form\Elements\Select. Did you maybe mean getOptions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
47
        } elseif (is_string($this->options) || $this->options instanceof Model) {
48
            $options = $this->setOptionsFromModel($this->options);
49
        }
50
51
        if (!is_array($options)) {
0 ignored issues
show
Bug introduced by
The variable $options does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
52
            throw new InvalidArgumentException('Form select element options must be array(key=>value)');
53
        }
54
55
        return collect($options)->mapWithKeys(function ($value, $key) {
56
            return [
57
                $key => [
58
                    'label' => $value,
59
                    'value' => $key,
60
                ],
61
            ];
62
        })->values();
63
    }
64
65
    /**
66
     * @param mixed $options
67
     *
68
     * @return $this
69
     */
70
    public function setOptions($options)
71
    {
72
        $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...
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param Model|string $options
79
     *
80
     * @return array
81
     */
82
    protected function setOptionsFromModel($options)
83
    {
84
        if (is_string($options)) {
85
            $options = app($options);
86
        }
87
88
        if (!($options instanceof Model)) {
89
            throw new InvalidArgumentException('Form select element options class must be instanced of Illuminate\Database\Eloquent\Model');
90
        }
91
92
        $key = $this->getOptionsValueAttribute() ?: $options->getKeyName();
93
94
        $repository = app(RepositoryInterface::class);
95
        $repository->setModel($options);
96
        $query = $repository->getQuery();
97
98
        $options = $query->get();
99
        if (is_null(($label = $this->getOptionsLabelAttribute()))) {
100
            throw new InvalidArgumentException('Form select element must set label attribute');
101
        }
102
103
        return array_pluck($options->all(), $this->getOptionsLabelAttribute(), $key);
104
    }
105
106
    /**
107
     * 获取 options 标题字段
108
     *
109
     * @return string
110
     */
111
    public function getOptionsLabelAttribute()
112
    {
113
        return $this->optionsLabelAttribute;
114
    }
115
116
    /**
117
     * 设置 options 标题字段
118
     *
119
     * @param string $value
120
     *
121
     * @return $this
122
     */
123
    public function setOptionsLabelAttribute($value)
124
    {
125
        $this->optionsLabelAttribute = $value;
126
127
        return $this;
128
    }
129
130
    /**
131
     * 获取 options value 字段
132
     *
133
     * @return string
134
     */
135
    public function getOptionsValueAttribute()
136
    {
137
        return $this->optionsValueAttribute;
138
    }
139
140
    /**
141
     * 设置 options value 字段
142
     *
143
     * @param string $value
144
     *
145
     * @return $this
146
     */
147
    public function setOptionsValueAttribute($value)
148
    {
149
        $this->optionsValueAttribute = $value;
150
151
        return $this;
152
    }
153
154
    public function toArray()
155
    {
156
        return parent::toArray() + [
157
                'options' => $this->getOptions(),
158
                'size'    => $this->getSize(),
159
            ];
160
    }
161
}
162