1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Micayael\Bundle\FormGeneratorBundle\Service; |
4
|
|
|
|
5
|
|
|
use Micayael\Bundle\FormGeneratorBundle\Config\SupportedTypes; |
6
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
7
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
8
|
|
|
use Symfony\Component\Form\FormInterface; |
9
|
|
|
use Symfony\Component\Validator\Constraints\Date; |
10
|
|
|
use Symfony\Component\Validator\Constraints\Email; |
11
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
12
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
13
|
|
|
use Symfony\Component\Validator\Constraints\Url; |
14
|
|
|
use Symfony\Component\Yaml\Yaml; |
15
|
|
|
|
16
|
|
|
class FormGenerator |
17
|
|
|
{ |
18
|
|
|
/** @var FormFactoryInterface */ |
19
|
|
|
public $formFactory; |
20
|
|
|
|
21
|
|
|
public function __construct(FormFactoryInterface $formFactory) |
22
|
|
|
{ |
23
|
|
|
$this->formFactory = $formFactory; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function createFormFromYaml(string $formConfigYaml, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): ?FormInterface |
27
|
|
|
{ |
28
|
|
|
$array = Yaml::parse($formConfigYaml); |
29
|
|
|
$json = json_encode($array); |
30
|
|
|
|
31
|
|
|
return $this->createFormFromJson($json, $formOptions, $data, $baseFormTypeClass, $groupName); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function createFormFromJson(string $formConfigJson, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): ?FormInterface |
35
|
|
|
{ |
36
|
|
|
$formConfig = json_decode($formConfigJson, true); |
37
|
|
|
|
38
|
|
|
return $this->createForm($formConfig, $formOptions, $data, $baseFormTypeClass, $groupName); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function createForm(array $formConfig, array $formOptions = [], $data = null, string $baseFormTypeClass = null, string $groupName = null): ?FormInterface |
42
|
|
|
{ |
43
|
|
|
if (empty($formConfig)) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$baseFormTypeClass = $baseFormTypeClass ?: FormType::class; |
48
|
|
|
|
49
|
|
|
$form = $this->formFactory->create($baseFormTypeClass, $data, $formOptions); |
50
|
|
|
|
51
|
|
|
if ($groupName) { |
52
|
|
|
$form->add($groupName, FormType::class); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
foreach ($formConfig as $inputName => $inputConfig) { |
56
|
|
|
$inputType = isset($inputConfig['type']) ? $inputConfig['type'] : 'text'; |
57
|
|
|
|
58
|
|
|
// Se obtiene la configuración predeterminada |
59
|
|
|
if (class_exists($inputType)) { |
60
|
|
|
$inputDefaultConfig = [ |
61
|
|
|
'class' => $inputType, |
62
|
|
|
'default_options' => [], |
63
|
|
|
]; |
64
|
|
|
} else { |
65
|
|
|
$inputDefaultConfig = SupportedTypes::getDefaultTypeConfig($inputType); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// se obtienen las opciones definidas para este input |
69
|
|
|
$inputOptions = isset($inputConfig['options']) ? $inputConfig['options'] : []; |
70
|
|
|
|
71
|
|
|
$inputOptions['constraints'] = $this->getConstraints($inputType, $inputDefaultConfig['default_options'], $inputOptions); |
72
|
|
|
|
73
|
|
|
// Se compilan las opciones default con las definidas |
74
|
|
|
$inputOptions = array_merge( |
75
|
|
|
$inputDefaultConfig['default_options'], |
76
|
|
|
$inputOptions |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
// Se crea el input de formulario |
80
|
|
|
if ($groupName) { |
81
|
|
|
$form->get($groupName)->add($inputName, $inputDefaultConfig['class'], $inputOptions); |
82
|
|
|
} else { |
83
|
|
|
$form->add($inputName, $inputDefaultConfig['class'], $inputOptions); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $form; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function getConstraints(string $inputType, array $inputDefaultOptions, array $inputOptions) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$ret = []; |
93
|
|
|
|
94
|
|
|
$validations = isset($inputDefaultOptions['constraints']) ? $inputDefaultOptions['constraints'] : []; |
95
|
|
|
$validations = array_merge($validations, isset($inputOptions['constraints']) ? $inputOptions['constraints'] : []); |
96
|
|
|
|
97
|
|
|
foreach ($validations as $validation => $validationOptions) { |
98
|
|
|
switch ($validation) { |
99
|
|
|
case 'not_blank': |
100
|
|
|
if (isset($inputOptions['required']) && false === $inputOptions['required']) { |
101
|
|
|
// no se setea la validación |
102
|
|
|
} else { |
103
|
|
|
$ret[] = new NotBlank($validationOptions); |
104
|
|
|
} |
105
|
|
|
break; |
106
|
|
|
case 'email': |
107
|
|
|
$ret[] = new Email($validationOptions); |
108
|
|
|
break; |
109
|
|
|
case 'url': |
110
|
|
|
$ret[] = new Url($validationOptions); |
111
|
|
|
break; |
112
|
|
|
case 'length': |
113
|
|
|
$ret[] = new Length($validationOptions); |
114
|
|
|
break; |
115
|
|
|
case 'date': |
116
|
|
|
$ret[] = new Date($validationOptions); |
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $ret; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.