Passed
Push — master ( 3e24bf...d48eaa )
by Daniel
05:16
created

FormView::getVars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components 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\ApiComponentsBundle\Model\Form;
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
    ];
59
60
    /**
61
     * @Groups({"component", "content"})
62
     */
63
    private array $vars;
64
65
    /**
66
     * @Groups({"component", "content"})
67
     */
68
    private DoctrineCollection $children;
69
70
    /**
71
     * @Groups({"component", "content"})
72
     */
73
    private bool $rendered;
74
75
    /**
76
     * @Groups({"component", "content"})
77
     */
78
    private bool $methodRendered;
79
80
    private FormInterface $form;
81
82
    public function __construct(FormInterface $form, ?SymfonyFormView $formView = null, bool $children = true)
83
    {
84
        $isRoot = !$formView;
85
        if (!$formView) {
86
            $formView = $form->createView();
87
        }
88
        $this->init($formView, $form, $children, $isRoot);
89
    }
90
91
    private function init(SymfonyFormView $formView, FormInterface $form, bool $children = true, bool $isRoot = false): void
92
    {
93
        $this->form = $form;
94
        $this->rendered = $formView->isRendered();
95
        $this->methodRendered = $formView->isMethodRendered();
96
        $this->processViewVars($formView, $isRoot);
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, $isRoot): void
109
    {
110
        $outputVars = array_merge(self::ARRAY_OUTPUT_VARS, self::OUTPUT_VARS);
111
        if (!$isRoot) {
112
            $outputVars[] = 'value';
113
        }
114
        foreach ($outputVars as $var) {
115
            if (isset($formView->vars[$var])) {
116
                $this->vars[$var] = $formView->vars[$var];
117
                $this->convertVarToArray($var);
118
            }
119
        }
120
    }
121
122
    private function convertVarToArray($var): void
123
    {
124
        if (\in_array($var, self::ARRAY_OUTPUT_VARS, true)) {
125
            /** @var iterable $choices */
126
            $choices = $this->vars[$var];
127
            $this->vars[$var] = [];
128
            foreach ($choices as $choice) {
129
                if (method_exists($choice, 'getMessage')) {
130
                    $this->vars[$var][] = $choice->getMessage();
131
                } else {
132
                    $this->vars[$var][] = (array) $choice;
133
                }
134
            }
135
        }
136
    }
137
138
    private function addChild(SymfonyFormView $symfonyFormView): void
139
    {
140
        $formView = new self($this->form, $symfonyFormView);
141
        $this->children->add($formView);
142
    }
143
144
    public function getVars(): array
145
    {
146
        return $this->vars;
147
    }
148
149
    public function getChildren(): DoctrineCollection
150
    {
151
        return $this->children;
152
    }
153
154
    public function isRendered(): bool
155
    {
156
        return $this->rendered;
157
    }
158
159
    public function isMethodRendered(): bool
160
    {
161
        return $this->methodRendered;
162
    }
163
164
    public function getForm(): FormInterface
165
    {
166
        return $this->form;
167
    }
168
169
    public function setForm(FormInterface $form): self
170
    {
171
        $this->init($form->createView(), $form, true);
172
173
        return $this;
174
    }
175
}
176