1 | <?php |
||
12 | class Select extends NamedElement |
||
13 | { |
||
14 | use SelectOptionsFromModel; |
||
15 | |||
16 | protected $type = 'select'; |
||
17 | |||
18 | protected $cast = 'string'; |
||
19 | |||
20 | protected $options; |
||
21 | |||
22 | /** |
||
23 | * |
||
24 | * @param string $name |
||
25 | * @param string $title |
||
26 | * @param array|Model $options |
||
27 | */ |
||
28 | public function __construct(string $name, string $title, $options = null) |
||
29 | { |
||
30 | parent::__construct($name, $title); |
||
31 | |||
32 | if (! is_null($options)) { |
||
33 | $this->setOptions($options); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | public function getOptions() |
||
38 | { |
||
39 | if ($this->options instanceof \Closure) { |
||
40 | $options = ($this->options)(); |
||
41 | } elseif ($this->isOptionsModel()) { |
||
42 | $options = $this->getOptionsFromModel(); |
||
43 | } elseif (is_array($this->options)) { |
||
44 | $options = $this->options; |
||
45 | } else { |
||
46 | throw new InvalidArgumentException( |
||
47 | sprintf( |
||
48 | "The %s element[%s] options must be return array(key=>value)", |
||
49 | $this->getType(), |
||
50 | $this->getName() |
||
51 | ) |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | return collect($options)->mapWithKeys(function ($value, $key) { |
||
56 | return [ |
||
57 | $key => [ |
||
58 | 'label' => $value, |
||
59 | 'value' => (string) $key, |
||
60 | ], |
||
61 | ]; |
||
62 | })->values(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param mixed $options |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function setOptions($options) |
||
76 | |||
77 | public function toArray() |
||
78 | { |
||
83 | } |
||
84 |