Completed
Push — master ( 0be8e1...585ca2 )
by Daniel
59:26 queued 46:02
created

FormView::getVars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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