Completed
Push — master ( d781ee...2f5f2b )
by wen
26:40
created

Select::setSize()   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 1
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7
use Illuminate\Support\Collection;
8
use InvalidArgumentException;
9
use Sco\Admin\Contracts\RepositoryInterface;
10
use Sco\Admin\Traits\SelectOptionsFromModel;
11
12
class Select extends NamedElement
13
{
14
    use SelectOptionsFromModel;
15
16
    protected $type = 'select';
17
18
    protected $size = '';
19
20
    protected $options;
21
22
    protected $extraOptions;
23
24
    public function __construct($name, $title, $options = null)
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 addOptions($options)
46
    {
47
        foreach ($options as $key => $option) {
48
            $this->addOption($key, $option);
49
        }
50
51
        return $this;
52
    }
53
54
    public function addOption($key, $value)
55
    {
56
        $this->extraOptions->put($key, $value);
57
58
        return $this;
59
    }
60
61
    public function getOptions()
62
    {
63 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...
64
            $options = ($this->options)();
65
        } elseif (is_string($this->options) || $this->options instanceof Model) {
66
            $options = $this->setOptionsFromModel();
67
        } elseif (is_array($this->options)) {
68
            $options = $this->options;
69
        } else {
70
            throw new InvalidArgumentException(
71
                "The form select element's options must be return array(key=>value)"
72
            );
73
        }
74
75
        $this->extraOptions->each(function ($value, $key) use ($options) {
76
            $options[$key] = $value;
77
        });
78
79
        return collect($options)->mapWithKeys(function ($value, $key) {
80
            return [
81
                $key => [
82
                    'label' => $value,
83
                    'value' => (string)$key,
84
                ],
85
            ];
86
        })->values();
87
    }
88
89
    /**
90
     * @param mixed $options
91
     *
92
     * @return $this
93
     */
94
    public function setOptions($options)
95
    {
96
        $this->options = $options;
97
98
        return $this;
99
    }
100
101
    public function getValue()
102
    {
103
        return (string)parent::getValue();
104
    }
105
106
    public function toArray()
107
    {
108
        return parent::toArray() + [
109
                'options' => $this->getOptions(),
110
                'size'    => $this->getSize(),
111
            ];
112
    }
113
}
114