-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldView.php
More file actions
154 lines (136 loc) · 3.97 KB
/
Copy pathFieldView.php
File metadata and controls
154 lines (136 loc) · 3.97 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Quatrevieux\Form\View;
use Quatrevieux\Form\Choice\View\ChoiceView;
use Quatrevieux\Form\Validator\FieldError;
use Stringable;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Structure of a single field view
* Note: Unlike most form components, this class is mutable. So it can be configured from the view.
*/
final class FieldView implements Stringable
{
public function __construct(
/**
* HTTP field name
* This name may be different from the form field name in case of embedded forms (e.g. "user[username]" for a "username" field in a "user" form).
*/
public readonly string $name,
/**
* Raw HTTP value
*/
public mixed $value,
/**
* Field error, if any
*/
public ?FieldError $error,
/**
* Input HTML attributes
*
* May contains HTML 5 validation attributes like "required" or "minlength".
* In case of boolean value, it will be used as a flag (e.g. "required" => true will be rendered as "required").
*
* @var array<string, scalar>
*/
public array $attributes = [],
/**
* List of available choices
*
* @var list<ChoiceView>|null
*/
public ?array $choices = null,
) {}
/**
* Define an attribute
*
* @param string $name Attribute name
* @param scalar $value Attribute value
*
* @return $this
*/
public function attr(string $name, mixed $value): self
{
$this->attributes[$name] = $value;
return $this;
}
/**
* Add "flag" attributes
* This is a shortcut for `$this->attr($name, true)`
*
* @param string ...$flags List of flags to enable
*
* @return $this
*/
public function is(string... $flags): self
{
foreach ($flags as $flag) {
$this->attributes[$flag] = true;
}
return $this;
}
/**
* Add a CSS class on the field, by appending it to the "class" attribute
*
* @param string $class CSS class name
*
* @return $this
*/
public function class(string $class): self
{
$previousClasses = $this->attributes['class'] ?? null;
$this->attributes['class'] = $previousClasses ? $previousClasses . ' ' . $class : $class;
return $this;
}
/**
* Define the input type
* This is a shortcut for `$this->attr('type', $type)`
*
* @param string $type
*
* @return $this
*/
public function type(string $type): self
{
$this->attributes['type'] = $type;
return $this;
}
/**
* Define the field choices
*
* @param list<ChoiceView>|null $choices List of choices to set on the field.
* @param TranslatorInterface|null $translator Translator to set on each choice. If null, the translator will not be set.
*
* @return $this
*/
public function choices(?array $choices, ?TranslatorInterface $translator = null): self
{
if ($choices && $translator) {
foreach ($choices as $choice) {
$choice->setTranslator($translator);
}
}
$this->choices = $choices;
return $this;
}
/**
* Render the field view as an HTML string
*
* @param callable(self, string|null):string $renderer Renderer function. It will receive the current field view as first argument, and the locale as second argument.
* @param string|null $locale If the input has translated data (e.g. choice labels), define the locale to use.
*
* @return string
*/
public function render(callable $renderer, ?string $locale = null): string
{
return $renderer($this, $locale);
}
/**
* Render the field as <input/> HTML tag
*
* @return string
*/
public function __toString(): string
{
return $this->render(FieldTemplate::Input);
}
}