-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFormGroup.php
More file actions
109 lines (85 loc) · 2.27 KB
/
FormGroup.php
File metadata and controls
109 lines (85 loc) · 2.27 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace TypiCMS\BootForms\Elements;
use TypiCMS\Form\Elements\Element;
use TypiCMS\Form\Elements\Label;
class FormGroup extends Element
{
protected Label $label;
protected Element $control;
protected ?FormText $formText = null;
protected ?InvalidFeedback $invalidFeedback = null;
protected bool $isFloating = false;
public function __construct(Label $label, Element $control)
{
$this->label = $label;
$this->label->addClass('form-label');
$this->control = $control;
$this->addClass('mb-3');
}
public function render(): string
{
$html = '<div';
$html .= $this->renderAttributes();
$html .= '>';
if ($this->isFloating) {
$html .= $this->control;
$html .= $this->label;
} else {
$html .= $this->label;
$html .= $this->control;
}
$html .= $this->renderInvalidFeedback();
$html .= $this->renderFormText();
$html .= '</div>';
return $html;
}
public function floating(): ?self
{
$this->isFloating = true;
$this->addClass('form-floating');
return $this;
}
public function formText(string $text): ?self
{
if (isset($this->formText)) {
return null;
}
$this->formText = new FormText($text);
return $this;
}
protected function renderFormText(): string
{
if ($this->formText) {
return $this->formText->render();
}
return '';
}
public function invalidFeedback(string $text): ?self
{
if (isset($this->invalidFeedback)) {
return null;
}
$this->invalidFeedback = new InvalidFeedback($text);
return $this;
}
protected function renderInvalidFeedback(): string
{
if ($this->invalidFeedback) {
return $this->invalidFeedback->render();
}
return '';
}
public function control(): Element
{
return $this->control;
}
public function label(): Label
{
return $this->label;
}
public function __call($method, $parameters): self
{
call_user_func_array([$this->control, $method], $parameters);
return $this;
}
}