Test Failed
Branch develop (ac2838)
by Daniel
09:09
created

FormView::isRendered()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Entity\Component\Form;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Symfony\Component\Serializer\Annotation\Groups;
7
8
/**
9
 * Class FormView
10
 * @package Silverback\ApiComponentBundle\Entity\Component\Form
11
 * @author Daniel West <[email protected]>
12
 */
13
class FormView
14
{
15
    /**
16
     * @Groups({"page"})
17
     * @var array
18
     */
19
    private $vars;
20
21
    /**
22
     * @Groups({"page"})
23
     * @var ArrayCollection
24
     */
25
    private $children;
26
27
    /**
28
     * @Groups({"page"})
29
     * @var bool
30
     */
31
    private $rendered;
32
33
    /**
34
     * @Groups({"page"})
35
     * @var bool
36
     */
37
    private $methodRendered;
38
39
    public function __construct(\Symfony\Component\Form\FormView $formViews, bool $children = true)
40
    {
41
        $varsToArray = ['choices', 'preferred_choices', 'errors', 'is_selected'];
42
43
        $outputVars = array_merge($varsToArray, [
44
            'value',
45
            'attr',
46
            'id',
47
            'name',
48
            'full_name',
49
            'disabled',
50
            'label',
51
            'block_prefixes',
52
            'unique_block_prefix',
53
            'valid',
54
            'required',
55
            'label_attr',
56
            'expanded',
57
            'submitted',
58
            'placeholder',
59
            'is_selected',
60
            'placeholder_in_choices',
61
            'checked',
62
            'action',
63
            'multiple'
64
        ]);
65
        foreach ($outputVars as $var) {
66
            if (isset($formViews->vars[$var])) {
67
                $this->vars[$var] = $formViews->vars[$var];
68
69
                if (in_array($var, $varsToArray)) {
70
                    $choices = $this->vars[$var];
71
                    $this->vars[$var] = [];
72
                    foreach ($choices as $choice) {
73
                        if (method_exists($choice, 'getMessage')) {
74
                            $this->vars[$var][] = $choice->getMessage();
75
                        } else {
76
                            $this->vars[$var][] = (array) $choice;
77
                        }
78
                    }
79
                }
80
            }
81
        }
82
83
        if ($children) {
84
            $this->children = new ArrayCollection();
85
            foreach ($formViews as $formView) {
86
                $this->addChild($formView);
87
            }
88
        }
89
        $this->rendered = $formViews->isRendered();
90
        $this->methodRendered = $formViews->isMethodRendered();
91
    }
92
93
    public function addChild(\Symfony\Component\Form\FormView $formViews)
94
    {
95
        $formView = new FormView($formViews);
96
        $this->children->add($formView);
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function getVars(): array
103
    {
104
        return $this->vars;
105
    }
106
107
    /**
108
     * @return ArrayCollection
109
     */
110
    public function getChildren(): ArrayCollection
111
    {
112
        return $this->children;
113
    }
114
115
    /**
116
     * @return bool
117
     */
118
    public function isRendered(): bool
119
    {
120
        return $this->rendered;
121
    }
122
123
    /**
124
     * @return bool
125
     */
126
    public function isMethodRendered(): bool
127
    {
128
        return $this->methodRendered;
129
    }
130
}
131