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