|
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 FlexPHP\Inputs\Input; |
|
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
|
14
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
|
15
|
|
|
use Symfony\Component\Form\FormInterface; |
|
16
|
|
|
use Symfony\Component\Form\Forms; |
|
17
|
|
|
|
|
18
|
|
|
class FormBuilder extends AbstractBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var array<string> |
|
22
|
|
|
*/ |
|
23
|
|
|
private array $data = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array<string> |
|
27
|
|
|
*/ |
|
28
|
|
|
private array $inputs = []; |
|
29
|
|
|
|
|
30
|
|
|
private string $template; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array<string, mixed> $inputs |
|
34
|
|
|
* @param array<string> $data |
|
35
|
|
|
* @param array<string> $options |
|
36
|
|
|
*/ |
|
37
|
7 |
|
public function __construct(array $inputs, array $data = [], array $options = [], string $template = '') |
|
38
|
|
|
{ |
|
39
|
7 |
|
$this->inputs = $this->parseInputs($inputs); |
|
40
|
7 |
|
$this->data = $data; |
|
41
|
7 |
|
$this->options = $options; |
|
42
|
7 |
|
$this->template = $template; |
|
43
|
7 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return FormInterface<string> |
|
47
|
|
|
*/ |
|
48
|
7 |
|
public function build(): FormInterface |
|
49
|
|
|
{ |
|
50
|
|
|
return $this |
|
51
|
7 |
|
->factory() |
|
52
|
7 |
|
->getForm(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
7 |
|
public function render(): string |
|
56
|
|
|
{ |
|
57
|
7 |
|
return $this->twig()->createTemplate($this->getTemplate(), $this->getName())->render([ |
|
58
|
7 |
|
'form' => $this->build()->createView(), |
|
59
|
7 |
|
'inputs' => $this->getInputs(), |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return FormBuilderInterface<string> |
|
65
|
|
|
*/ |
|
66
|
7 |
|
protected function factory(): FormBuilderInterface |
|
67
|
|
|
{ |
|
68
|
7 |
|
return Forms::createFormFactory()->createBuilder(FormType::class, $this->getData(), $this->getOptions()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return array<string> |
|
73
|
|
|
*/ |
|
74
|
7 |
|
private function getInputs(): array |
|
75
|
|
|
{ |
|
76
|
7 |
|
return $this->inputs; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return array<string> |
|
81
|
|
|
*/ |
|
82
|
7 |
|
private function getData(): array |
|
83
|
|
|
{ |
|
84
|
7 |
|
return $this->data; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param array<mixed> $inputs |
|
89
|
|
|
* |
|
90
|
|
|
* @return array<mixed> |
|
91
|
|
|
*/ |
|
92
|
7 |
|
private function parseInputs($inputs): array |
|
93
|
|
|
{ |
|
94
|
7 |
|
foreach ($inputs as $name => $options) { |
|
95
|
3 |
|
if (\is_array($options)) { |
|
96
|
2 |
|
$inputs[$name] = Input::create($options['type'] ?? 'text', $name, $options); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
7 |
|
return $inputs; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
7 |
|
private function getTemplate(): string |
|
104
|
|
|
{ |
|
105
|
|
|
$_template = <<<T |
|
106
|
7 |
|
{{ form_start(form) }} |
|
107
|
|
|
{% for input in inputs %} |
|
108
|
|
|
{{ input|raw }} |
|
109
|
|
|
{% endfor %} |
|
110
|
|
|
{{ form_end(form) }} |
|
111
|
|
|
T; |
|
112
|
|
|
|
|
113
|
7 |
|
if (\is_file($this->template)) { |
|
114
|
1 |
|
$_template = (string)\file_get_contents($this->template); |
|
115
|
6 |
|
} elseif (!empty($this->template)) { |
|
116
|
1 |
|
$_template = $this->template; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
7 |
|
return $_template; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|