Passed
Push — 3.x ( aabcdb...ec2fff )
by Enjoys
01:46
created

BaseRenderer::elementsRender()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 15
ccs 8
cts 9
cp 0.8889
rs 10
cc 3
nc 3
nop 1
crap 3.0123
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2020 Enjoys.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
declare(strict_types=1);
28
29
namespace Enjoys\Forms\Renderer;
30
31
use Enjoys\Forms\Form;
32
33
/**
34
 * Description of BaseRenderer
35
 *
36
 * @author Enjoys
37
 */
38
class BaseRenderer implements RendererInterface
39
{
40
41
    protected Form $form;
42
    protected array $elements = [];
43
44 1
    public function __construct(Form $form = null)
45
    {
46 1
        $this->form = $this->setForm($form ?? new Form());
47 1
    }
48
    /**
49
     *
50
     * @param Form $form
51
     * @return Form
52
     */
53 1
    public function setForm(Form $form): Form
54
    {
55 1
        $this->form = $form;
56 1
        $this->elements = $this->form->getElements();
57 1
        return $this->form;
58
    }
59
60
    /**
61
     *
62
     * @return string
63
     */
64 1
    protected function hiddenRender(): string
65
    {
66 1
        $html = '';
67
        /** @var \Enjoys\Forms\Element $element */
68 1
        foreach ($this->elements as $key => $element) {
69 1
            if ($element instanceof \Enjoys\Forms\Elements\Hidden) {
70 1
                unset($this->elements[$key]);
71 1
                $html .= $element->baseHtml();
72
            }
73 1
            continue;
74
        }
75 1
        return $html;
76
    }
77
78
    /**
79
     *
80
     * @param array|null $elements
81
     * @psalm-param null|array{\Enjoys\Forms\Element} $elements
82
     * @return string
83
     */
84 1
    protected function elementsRender(?array $elements = null): string
85
    {
86 1
        $elements ??= $this->elements;
87 1
        $html = '';
88
89 1
        foreach ($elements as $key => $element) {
90 1
            unset($elements[$key]);
91
92 1
            if (!($element instanceof \Enjoys\Forms\Element)) {
93
                continue;
94
            }
95
96 1
            $html .= $this->elementRender($element);
97
        }
98 1
        return $html;
99
    }
100
101
    /**
102
     *
103
     * @param \Enjoys\Forms\Element $element
104
     * @return string
105
     */
106 1
    public function elementRender(\Enjoys\Forms\Element $element): string
107
    {
108 1
        $elementRender = new BaseElementRender($element);
109 1
        return $elementRender->render();
110
    }
111
112
    /**
113
     *
114
     * @return string
115
     */
116 1
    public function render(): string
117
    {
118 1
        return "<form{$this->form->getAttributesString()}>"
119 1
                . $this->hiddenRender()
120 1
                . $this->elementsRender($this->elements)
121 1
                . "</form>";
122
    }
123
}
124