|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of FlexPHP. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Freddie Gar <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace FlexPHP\Inputs\Builder; |
|
11
|
|
|
|
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
|
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
15
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
16
|
|
|
use Symfony\Component\Form\FormInterface; |
|
17
|
|
|
use Symfony\Component\Form\Forms; |
|
18
|
|
|
|
|
19
|
|
|
class InputBuilder extends AbstractBuilder |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $type; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param array<string, mixed> $options |
|
28
|
|
|
*/ |
|
29
|
61 |
|
public function __construct(string $name, array $options) |
|
30
|
|
|
{ |
|
31
|
61 |
|
$this->name = $name; |
|
32
|
61 |
|
$this->options = $this->parseOptions($this->getDefaultOptions($options)); |
|
33
|
61 |
|
} |
|
34
|
|
|
|
|
35
|
61 |
|
public function getOptions(): array |
|
36
|
|
|
{ |
|
37
|
61 |
|
return $this->options; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
61 |
|
public function build(): FormInterface |
|
41
|
|
|
{ |
|
42
|
61 |
|
$options = $this->getOptions(); |
|
43
|
61 |
|
$classType = \trim($this->getType()); |
|
44
|
|
|
|
|
45
|
61 |
|
if (\strpos($classType, 'Symfony') === false) { |
|
46
|
|
|
// Not symfony type |
|
47
|
18 |
|
$type = \preg_replace('/type$/i', '', $classType) ?? $classType; |
|
48
|
18 |
|
$classType = \sprintf('\Symfony\Component\Form\Extension\Core\Type\%1$sType', \ucwords($type)); |
|
49
|
43 |
|
} elseif (!empty($options['type']) && \stripos($classType, $options['type']) === false) { |
|
50
|
|
|
// Symfony type, but its diff in options type contraint |
|
51
|
4 |
|
$type = $options['type']; |
|
52
|
4 |
|
$classType = \sprintf('\Symfony\Component\Form\Extension\Core\Type\%1$sType', \ucwords($type)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
61 |
|
unset($options['type']); |
|
56
|
|
|
|
|
57
|
61 |
|
if (!\class_exists($classType)) { |
|
58
|
2 |
|
throw new InvalidArgumentException(\sprintf('Type [%1$s] is not supported', $classType)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this |
|
62
|
59 |
|
->factory() |
|
63
|
59 |
|
->add($this->getName(), $classType, $options) |
|
64
|
59 |
|
->getForm(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
61 |
|
public function render(): string |
|
68
|
|
|
{ |
|
69
|
61 |
|
return $this->twig()->createTemplate(\sprintf('{{ form_row(form.%1$s) }}', $this->getName()))->render([ |
|
70
|
61 |
|
'form' => $this->build()->createView(), |
|
71
|
|
|
]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return FormBuilderInterface<string> |
|
76
|
|
|
*/ |
|
77
|
59 |
|
protected function factory(): FormBuilderInterface |
|
78
|
|
|
{ |
|
79
|
59 |
|
return Forms::createFormFactory()->createBuilder(FormType::class, null); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
43 |
|
protected function getType(): string |
|
83
|
|
|
{ |
|
84
|
43 |
|
return TextType::class; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param array<string> $options |
|
89
|
|
|
* |
|
90
|
|
|
* @return array<mixed> |
|
91
|
|
|
*/ |
|
92
|
61 |
|
private function getDefaultOptions(array $options = []): array |
|
93
|
|
|
{ |
|
94
|
61 |
|
return (array)\array_merge([ |
|
95
|
61 |
|
'required' => false, |
|
96
|
61 |
|
], $options); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param array<mixed> $options |
|
101
|
|
|
* |
|
102
|
|
|
* @return array<string> |
|
103
|
|
|
*/ |
|
104
|
61 |
|
private function parseOptions(array $options): array |
|
105
|
|
|
{ |
|
106
|
61 |
|
$_options = []; |
|
107
|
|
|
|
|
108
|
61 |
|
if (!empty($options['attr']['type'])) { |
|
109
|
1 |
|
$options['type'] = $options['attr']['type']; |
|
110
|
1 |
|
unset($options['attr']['type']); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
61 |
|
foreach ($options as $option => $value) { |
|
114
|
61 |
|
switch ($option) { |
|
115
|
61 |
|
case 'default': |
|
116
|
1 |
|
$_options['data'] = $value; |
|
117
|
|
|
|
|
118
|
1 |
|
break; |
|
119
|
61 |
|
case 'constraints': |
|
120
|
21 |
|
$_options = $this->parseOptionConstraints($_options, $value); |
|
121
|
|
|
|
|
122
|
21 |
|
break; |
|
123
|
61 |
|
case 'empty_data': |
|
124
|
3 |
|
$_options = $this->parseOptionEmptyData($_options, $option, $value); |
|
125
|
|
|
|
|
126
|
3 |
|
break; |
|
127
|
|
|
default: |
|
128
|
61 |
|
$_options = $this->parseOption($_options, $option, $value); |
|
129
|
|
|
|
|
130
|
61 |
|
break; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
61 |
|
unset($options[$option]); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
61 |
|
return $this->parseOptionTypeSpecial($_options); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
3 |
|
private function parseOptionEmptyData(array $_options, string $option, string $value): array |
|
140
|
|
|
{ |
|
141
|
3 |
|
$_options[$option] = $value; |
|
142
|
|
|
|
|
143
|
3 |
|
if (empty($_options['attr'])) { |
|
144
|
2 |
|
$_options['attr'] = []; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
3 |
|
$_options['attr']['placeholder'] = $value; |
|
148
|
|
|
|
|
149
|
3 |
|
return $_options; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
21 |
|
private function parseOptionConstraints(array $_options, array $value): array |
|
153
|
|
|
{ |
|
154
|
21 |
|
foreach ($value as $attribute => $_value) { |
|
155
|
21 |
|
if (\is_int($attribute)) { |
|
156
|
1 |
|
$attribute = $_value; |
|
157
|
1 |
|
$_value = true; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
21 |
|
if ($attribute === 'required') { |
|
161
|
6 |
|
$_options['required'] = $_value && !\preg_match('/^false$/i', (string)$_value); |
|
162
|
|
|
} elseif (\in_array($attribute, ['length', 'mincheck', 'maxcheck', 'check', 'equalto'])) { |
|
163
|
5 |
|
$_options['attr']['data-parsley-' . $attribute] = $_value; |
|
164
|
10 |
|
} elseif ($attribute === 'range') { |
|
165
|
1 |
|
$_options['type'] = $attribute; |
|
166
|
1 |
|
[$min, $max] = \explode(',', $_value); |
|
167
|
1 |
|
$_options['attr'] = \compact('min', 'max'); |
|
168
|
9 |
|
} elseif ($attribute === 'type') { |
|
169
|
3 |
|
$_options['type'] = $_value; |
|
170
|
|
|
} else { |
|
171
|
6 |
|
$_options['attr'][$attribute] = $_value; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
21 |
|
return $_options; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
61 |
|
private function parseOptionTypeSpecial(array $_options): array |
|
179
|
|
|
{ |
|
180
|
61 |
|
if (!empty($_options['type'])) { |
|
181
|
8 |
|
$_type = \strtolower($_options['type']); |
|
182
|
|
|
|
|
183
|
8 |
|
if (\in_array($_type, ['digits', 'alphanum'])) { |
|
184
|
2 |
|
$_options['attr']['data-parsley-type'] = $_type; |
|
185
|
2 |
|
$_options['type'] = 'text'; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
61 |
|
return $_options; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param mixed $value |
|
194
|
|
|
*/ |
|
195
|
61 |
|
private function parseOption(array $_options, string $option, $value): array |
|
196
|
|
|
{ |
|
197
|
61 |
|
if (\is_array($value) && !empty($_options[$option])) { |
|
198
|
1 |
|
$_options[$option] = \array_merge_recursive($_options[$option], $value); |
|
199
|
|
|
} else { |
|
200
|
61 |
|
$_options[$option] = $value; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
61 |
|
return $_options; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|