-
Notifications
You must be signed in to change notification settings - Fork 77
/
AbstractTransformer.php
149 lines (125 loc) · 4.37 KB
/
AbstractTransformer.php
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
<?php
/*
* This file is part of the Limenius\Liform package.
*
* (c) Limenius <https://github.com/Limenius/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Limenius\Liform\Transformer;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @author Nacho Martín <nacho@limenius.com>
*/
abstract class AbstractTransformer implements TransformerInterface
{
protected TranslatorInterface $translator;
protected ?FormTypeGuesserInterface $validatorGuesser = null;
public function __construct(TranslatorInterface $translator, FormTypeGuesserInterface $validatorGuesser = null)
{
$this->translator = $translator;
$this->validatorGuesser = $validatorGuesser;
}
public function isRequired(FormInterface $form): bool
{
return $form->getConfig()->getOption('required');
}
/** @param ExtensionInterface[] $extensions */
protected function applyExtensions(array $extensions, FormInterface $form, array $schema): array
{
$newSchema = $schema;
foreach ($extensions as $extension) {
$newSchema = $extension->apply($form, $newSchema);
}
return $newSchema;
}
/** @param ExtensionInterface[] $extensions */
protected function addCommonSpecs(
FormInterface $form,
array $schema,
array $extensions = [],
?string $widget = null
): array {
$schema = $this->addLabel($form, $schema);
$schema = $this->addAttr($form, $schema);
$schema = $this->addPattern($form, $schema);
$schema = $this->addDisabled($form, $schema);
$schema = $this->addDescription($form, $schema);
$schema = $this->addWidget($form, $schema, $widget);
$schema = $this->applyExtensions($extensions, $form, $schema);
return $schema;
}
/**
* @param FormInterface $form
* @param array $schema
*
* @return array
*/
protected function addDisabled(FormInterface $form, array $schema): array
{
if ($form->getConfig()->getOption('disabled')) {
$schema['disabled'] = true;
}
return $schema;
}
/**
* @param FormInterface $form
* @param array $schema
*
* @return array
*/
protected function addPattern(FormInterface $form, array $schema): array
{
if ($attr = $form->getConfig()->getOption('attr')) {
if (isset($attr['pattern'])) {
$schema['pattern'] = $attr['pattern'];
}
}
return $schema;
}
protected function addLabel(FormInterface $form, array $schema): array
{
$translationDomain = $form->getConfig()->getOption('translation_domain');
if ($label = $form->getConfig()->getOption('label')) {
$schema['title'] = $this->translator->trans($label, [], $translationDomain);
} else {
$schema['title'] = $this->translator->trans($form->getName(), [], $translationDomain);
}
return $schema;
}
protected function addAttr(FormInterface $form, array $schema): array
{
if ($attr = $form->getConfig()->getOption('attr')) {
$schema['attr'] = $attr;
}
return $schema;
}
protected function addDescription(FormInterface $form, array $schema): array
{
$formConfig = $form->getConfig();
if ($help = $formConfig->getOption('help', '')) {
$schema['description'] = $this->translator->trans($help);
}
if ($liform = $formConfig->getOption('liform')) {
if (isset($liform['description']) && $description = $liform['description']) {
$schema['description'] = $this->translator->trans($description);
}
}
return $schema;
}
/** @param mixed $configWidget */
protected function addWidget(FormInterface $form, array $schema, $configWidget): array
{
if ($liform = $form->getConfig()->getOption('liform')) {
if (isset($liform['widget']) && $widget = $liform['widget']) {
$schema['widget'] = $widget;
}
} elseif ($configWidget) {
$schema['widget'] = $configWidget;
}
return $schema;
}
}