-
Notifications
You must be signed in to change notification settings - Fork 554
/
Fieldset.php
89 lines (73 loc) · 1.7 KB
/
Fieldset.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace A17\Twill\Services\Forms;
use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
class Fieldset implements CanHaveSubfields
{
protected function __construct(
public ?string $title = null,
public ?Collection $fields = null,
public ?string $id = null,
public ?bool $open = true,
) {
}
public static function make(): self
{
return new self();
}
/**
* Set the title of the fieldset.
*/
public function title(string $title): static
{
$this->title = $title;
if (! $this->id) {
$this->id(Str::slug($title));
}
return $this;
}
/**
* Set the id of the field.
*/
public function id(string $id): static
{
$this->id = $id;
return $this;
}
/**
* Marks the fielset as open.
*/
public function open(bool $open = true): static
{
$this->open = $open;
return $this;
}
/**
* Marks the fielset as closed.
*/
public function closed(): static
{
$this->open = false;
return $this;
}
/**
* Set the form fields of the fieldset.
*/
public function fields(array $fields): static
{
$this->fields = collect($fields);
return $this;
}
public function registerDynamicRepeaters(): void
{
foreach ($this as $field) {
if ($field instanceof InlineRepeater) {
$field->register();
}
if ($field instanceof CanHaveSubfields) {
$field->registerDynamicRepeaters();
}
}
}
}