Completed
Push — master ( 65925d...2d1b3d )
by Daniel
08:05
created

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