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