Passed
Push — develop ( b22678...97e476 )
by Daniel
08:45
created

FormView::isMethodRendered()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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