|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Dto; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
17
|
|
|
use Doctrine\Common\Collections\Collection as DoctrineCollection; |
|
18
|
|
|
use Symfony\Component\Form\FormInterface; |
|
19
|
|
|
use Symfony\Component\Form\FormView as SymfonyFormView; |
|
20
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Daniel West <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class FormView |
|
26
|
|
|
{ |
|
27
|
|
|
private const ARRAY_OUTPUT_VARS = [ |
|
28
|
|
|
'choices', |
|
29
|
|
|
'preferred_choices', |
|
30
|
|
|
'errors', |
|
31
|
|
|
'is_selected', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
private const OUTPUT_VARS = [ |
|
35
|
|
|
'action', |
|
36
|
|
|
'api_request', |
|
37
|
|
|
'attr', |
|
38
|
|
|
'block_prefixes', |
|
39
|
|
|
'checked', |
|
40
|
|
|
'disabled', |
|
41
|
|
|
'expanded', |
|
42
|
|
|
'full_name', |
|
43
|
|
|
'help', |
|
44
|
|
|
'id', |
|
45
|
|
|
'is_selected', |
|
46
|
|
|
'label', |
|
47
|
|
|
'label_attr', |
|
48
|
|
|
'multiple', |
|
49
|
|
|
'name', |
|
50
|
|
|
'placeholder', |
|
51
|
|
|
'placeholder_in_choices', |
|
52
|
|
|
'post_app_proxy', |
|
53
|
|
|
'realtime_validate', |
|
54
|
|
|
'required', |
|
55
|
|
|
'submitted', |
|
56
|
|
|
'unique_block_prefix', |
|
57
|
|
|
'valid', |
|
58
|
|
|
'value', |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @Groups({"component", "content"}) |
|
63
|
|
|
*/ |
|
64
|
|
|
private array $vars; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @Groups({"component", "content"}) |
|
68
|
|
|
*/ |
|
69
|
|
|
private DoctrineCollection $children; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @Groups({"component", "content"}) |
|
73
|
|
|
*/ |
|
74
|
|
|
private bool $rendered; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @Groups({"component", "content"}) |
|
78
|
|
|
*/ |
|
79
|
|
|
private bool $methodRendered; |
|
80
|
|
|
|
|
81
|
|
|
private FormInterface $form; |
|
82
|
|
|
|
|
83
|
|
|
public function __construct(FormInterface $form, ?SymfonyFormView $formView = null, bool $children = true) |
|
84
|
|
|
{ |
|
85
|
|
|
if (!$formView) { |
|
86
|
|
|
$formView = $form->createView(); |
|
87
|
|
|
} |
|
88
|
|
|
$this->init($formView, $form, $children); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function init(SymfonyFormView $formView, FormInterface $form, bool $children = true): void |
|
92
|
|
|
{ |
|
93
|
|
|
$this->form = $form; |
|
94
|
|
|
$this->rendered = $formView->isRendered(); |
|
95
|
|
|
$this->methodRendered = $formView->isMethodRendered(); |
|
96
|
|
|
$this->processViewVars($formView); |
|
97
|
|
|
if ($children) { |
|
98
|
|
|
$this->children = new ArrayCollection(); |
|
99
|
|
|
foreach ($formView->getIterator() as $view) { |
|
100
|
|
|
$this->addChild($view); |
|
101
|
|
|
} |
|
102
|
|
|
if (\array_key_exists('prototype', $formView->vars)) { |
|
103
|
|
|
$this->addChild($formView->vars['prototype']); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function processViewVars(SymfonyFormView $formView): void |
|
109
|
|
|
{ |
|
110
|
|
|
$outputVars = array_merge(self::ARRAY_OUTPUT_VARS, self::OUTPUT_VARS); |
|
111
|
|
|
foreach ($outputVars as $var) { |
|
112
|
|
|
if (isset($formView->vars[$var])) { |
|
113
|
|
|
$this->vars[$var] = $formView->vars[$var]; |
|
114
|
|
|
$this->convertVarToArray($var); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private function convertVarToArray($var): void |
|
120
|
|
|
{ |
|
121
|
|
|
if (\in_array($var, self::ARRAY_OUTPUT_VARS, true)) { |
|
122
|
|
|
/** @var iterable $choices */ |
|
123
|
|
|
$choices = $this->vars[$var]; |
|
124
|
|
|
$this->vars[$var] = []; |
|
125
|
|
|
foreach ($choices as $choice) { |
|
126
|
|
|
if (method_exists($choice, 'getMessage')) { |
|
127
|
|
|
$this->vars[$var][] = $choice->getMessage(); |
|
128
|
|
|
} else { |
|
129
|
|
|
$this->vars[$var][] = (array) $choice; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
private function addChild(SymfonyFormView $symfonyFormView): void |
|
136
|
|
|
{ |
|
137
|
|
|
$formView = new self($this->form, $symfonyFormView); |
|
138
|
|
|
$this->children->add($formView); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function getVars(): array |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->vars; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function getChildren(): DoctrineCollection |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->children; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function isRendered(): bool |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->rendered; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function isMethodRendered(): bool |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->methodRendered; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function getForm(): FormInterface |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->form; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function setForm(FormInterface $form): self |
|
167
|
|
|
{ |
|
168
|
|
|
$this->init($form->createView(), $form, true); |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|