Passed
Push — v2 ( 5505d3...8372d3 )
by Daniel
04:27 queued 22s
created

FormView::convertVarToArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 20
rs 10
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\Entity\Component;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Silverback\ApiComponentB...ty\Component\Collection. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use Symfony\Component\Form\FormInterface;
19
use Symfony\Component\Form\FormView as SymfonyFormView;
20
use Symfony\Component\Serializer\Annotation\Groups;
21
22
/**
23
 * Class FormView.
24
 *
25
 * @author Daniel West <[email protected]>
26
 */
27
class FormView
28
{
29
    private const ARRAY_OUTPUT_VARS = [
30
        'choices',
31
        'preferred_choices',
32
        'errors',
33
        'is_selected',
34
    ];
35
36
    private const OUTPUT_VARS = [
37
        'action',
38
        'api_request',
39
        'attr',
40
        'block_prefixes',
41
        'checked',
42
        'disabled',
43
        'expanded',
44
        'full_name',
45
        'help',
46
        'id',
47
        'is_selected',
48
        'label',
49
        'label_attr',
50
        'multiple',
51
        'name',
52
        'placeholder',
53
        'placeholder_in_choices',
54
        'post_app_proxy',
55
        'realtime_validate',
56
        'required',
57
        'submitted',
58
        'unique_block_prefix',
59
        'valid',
60
        'value',
61
    ];
62
63
    /**
64
     * @Groups({"component", "content"})
65
     *
66
     * @var array
67
     */
68
    private $vars;
69
70
    /**
71
     * @Groups({"component", "content"})
72
     *
73
     * @var Collection
74
     */
75
    private $children;
76
77
    /**
78
     * @Groups({"component", "content"})
79
     *
80
     * @var bool
81
     */
82
    private $rendered;
83
84
    /**
85
     * @Groups({"component", "content"})
86
     *
87
     * @var bool
88
     */
89
    private $methodRendered;
90
91
    private $form;
92
93
    public function __construct(SymfonyFormView $formView, FormInterface $form, bool $children = true)
94
    {
95
        $this->init($formView, $form, $children);
96
    }
97
98
    private function init(SymfonyFormView $formView, FormInterface $form, bool $children = true)
99
    {
100
        $this->form = $form;
101
        $this->rendered = $formView->isRendered();
102
        $this->methodRendered = $formView->isMethodRendered();
103
        $this->processViewVars($formView);
104
        if ($children) {
105
            $this->children = new ArrayCollection();
106
            foreach ($formView->getIterator() as $view) {
107
                $this->addChild($view);
108
            }
109
            if (\array_key_exists('prototype', $formView->vars)) {
110
                $this->addChild($formView->vars['prototype']);
111
            }
112
        }
113
    }
114
115
    private function processViewVars(SymfonyFormView $formView): void
116
    {
117
        $outputVars = array_merge(self::ARRAY_OUTPUT_VARS, self::OUTPUT_VARS);
118
        foreach ($outputVars as $var) {
119
            if (isset($formView->vars[$var])) {
120
                $this->vars[$var] = $formView->vars[$var];
121
                $this->convertVarToArray($var);
122
            }
123
        }
124
    }
125
126
    private function convertVarToArray($var): void
127
    {
128
        if (\in_array($var, self::ARRAY_OUTPUT_VARS, true)) {
129
            /** @var iterable $choices */
130
            $choices = $this->vars[$var];
131
            $this->vars[$var] = [];
132
            foreach ($choices as $choice) {
133
                if (method_exists($choice, 'getMessage')) {
134
                    $this->vars[$var][] = $choice->getMessage();
135
                } else {
136
                    $this->vars[$var][] = (array) $choice;
137
                }
138
            }
139
        }
140
    }
141
142
    private function addChild(SymfonyFormView $formViews): void
143
    {
144
        $formView = new self($formViews, $this->form);
145
        $this->children->add($formView);
146
    }
147
148
    public function getVars(): array
149
    {
150
        return $this->vars;
151
    }
152
153
    public function getChildren(): Collection
154
    {
155
        return $this->children;
156
    }
157
158
    public function isRendered(): bool
159
    {
160
        return $this->rendered;
161
    }
162
163
    public function isMethodRendered(): bool
164
    {
165
        return $this->methodRendered;
166
    }
167
168
    public function getForm(): FormInterface
169
    {
170
        return $this->form;
171
    }
172
173
    public function setForm(FormInterface $form): self
174
    {
175
        $this->init($form->createView(), $form, true);
176
177
        return $this;
178
    }
179
}
180