1 | <?php |
||
2 | |||
3 | namespace ProtoneMedia\LaravelFormComponents; |
||
4 | |||
5 | use Illuminate\Support\Arr; |
||
6 | |||
7 | class FormDataBinder |
||
8 | { |
||
9 | /** |
||
10 | * Tree of bound targets. |
||
11 | */ |
||
12 | private array $bindings = []; |
||
13 | |||
14 | /** |
||
15 | * Wired to a Livewire component. |
||
16 | */ |
||
17 | private $wire = false; |
||
18 | |||
19 | /** |
||
20 | * Bind a target to the current instance |
||
21 | * |
||
22 | * @param mixed $target |
||
23 | * @return void |
||
24 | */ |
||
25 | public function bind($target): void |
||
26 | { |
||
27 | $this->bindings[] = $target; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Get the latest bound target. |
||
32 | * |
||
33 | * @return mixed |
||
34 | */ |
||
35 | public function get() |
||
36 | { |
||
37 | return Arr::last($this->bindings); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Remove the last binding. |
||
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | public function pop(): void |
||
46 | { |
||
47 | array_pop($this->bindings); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Returns wether the form is bound to a Livewire model. |
||
52 | * |
||
53 | * @return boolean |
||
54 | */ |
||
55 | public function isWired(): bool |
||
56 | { |
||
57 | return $this->wire !== false; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Returns the modifier, if set. |
||
62 | * |
||
63 | * @return string|null |
||
64 | */ |
||
65 | public function getWireModifier(): ?string |
||
66 | { |
||
67 | return is_string($this->wire) ? $this->wire : null; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Enable Livewire binding with an optional modifier. |
||
72 | * |
||
73 | * @param string $modifier |
||
74 | * @return void |
||
75 | */ |
||
76 | public function wire($modifier = null): void |
||
77 | { |
||
78 | $this->wire = $modifier ?: null; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Disable Livewire binding. |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | public function endWire(): void |
||
87 | { |
||
88 | $this->wire = false; |
||
89 | } |
||
90 | } |
||
91 |