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