-
Notifications
You must be signed in to change notification settings - Fork 554
/
HasOptions.php
53 lines (42 loc) · 1.14 KB
/
HasOptions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace A17\Twill\Services\Forms\Fields\Traits;
use A17\Twill\Services\Forms\Option;
use A17\Twill\Services\Forms\Options;
use Closure;
trait HasOptions
{
protected Closure|Options|null $options = null;
protected int $columns = 0;
/**
* List of options to display in the field.
*/
public function options(Options|Closure|array $options): static
{
$this->options = is_array($options)
? Options::fromArray($options)
: $options;
return $this;
}
/**
* Adds a single option.
*/
public function addOption(Option $option): static
{
if ($this->options instanceof Closure) {
throw new \Exception('addOption cannot be used with an options closure.');
}
if ($this->options === null) {
$this->options = Options::make();
}
$this->options->add($option);
return $this;
}
protected function getOptions(): Options
{
if ($this->options instanceof Closure) {
$execute = $this->options;
return $execute();
}
return $this->options;
}
}