Passed
Push — master ( b8e171...9e4d7b )
by Gabriel
04:14 queued 12s
created

AbstractElementRenderer::renderLabel()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.9666
cc 3
nc 4
nop 1
crap 3.009
1
<?php
2
3
namespace Nip\Form\Renderer\Elements;
4
5
use Nip\Form\Elements\AbstractElement;
6
use Nip\Form\Renderer\AbstractRenderer;
7
8
/**
9
 * Class AbstractElementRenderer
10
 * @package Nip\Form\Renderer\Elements
11
 */
12
abstract class AbstractElementRenderer
13
{
14
    protected $_renderer;
15
    protected $_element;
16
17
    /**
18
     * @return AbstractRenderer
19
     */
20 3
    public function getRenderer()
21
    {
22 3
        return $this->_renderer;
23
    }
24
25
    /**
26
     * @param AbstractRenderer $renderer
27
     *
28
     * @return $this
29
     */
30 6
    public function setRenderer(AbstractRenderer $renderer)
31
    {
32 6
        $this->_renderer = $renderer;
33
34 6
        return $this;
35
    }
36
37
    /**
38
     * @return string
39
     */
40 8
    public function render()
41
    {
42 8
        $return = '';
43 8
        $return .= $this->renderElement();
44 8
        $return .= $this->renderErrors();
45
46 8
        return $return;
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52 8
    public function renderElement()
53
    {
54 8
        $return = $this->renderDecorators($this->generateElement(), 'element');
0 ignored issues
show
Bug introduced by
'element' of type string is incompatible with the type boolean expected by parameter $position of Nip\Form\Renderer\Elemen...rer::renderDecorators(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $return = $this->renderDecorators($this->generateElement(), /** @scrutinizer ignore-type */ 'element');
Loading history...
55 8
        $this->getElement()->setRendered(true);
56
57 8
        return $return;
58
    }
59
60
    /**
61
     * @param string|array $classes
62
     * @return string
63
     */
64 2
    public function renderLabel($classes = null)
65
    {
66 2
        $label = $this->getElement()->getLabel();
67 2
        $required = $this->getElement()->isRequired();
68 2
        $classes = is_array($classes) ? implode(' ', $classes) : $classes;
69
70 2
        $return = '<label class="' . $classes . '">';
71 2
        $return .= $label . ':';
72
73 2
        if ($required) {
74
            $return .= '<span class="required">*</span>';
75
        }
76
77 2
        $return .= "</label>";
78
79 2
        return $return;
80
    }
81
82
    /**
83
     * @param $return
84
     * @param bool $position
85
     *
86
     * @return mixed
87
     */
88 8
    public function renderDecorators($return, $position = false)
89
    {
90 8
        if ($position) {
91 8
            $decorators = $this->getElement()->getDecoratorsByPosition($position);
92 8
            if (is_array($decorators)) {
93
                foreach ($decorators as $decorator) {
94
                    $return = $decorator->render($return);
95
                }
96
            }
97
        }
98
99 8
        return $return;
100
    }
101
102
    /**
103
     * @return Nip_Form_Element_Abstract
0 ignored issues
show
Bug introduced by
The type Nip\Form\Renderer\Elemen...p_Form_Element_Abstract was not found. Did you mean Nip_Form_Element_Abstract? If so, make sure to prefix the type with \.
Loading history...
104
     */
105 8
    public function getElement()
106
    {
107 8
        return $this->_element;
108
    }
109
110
    /**
111
     * @param AbstractElement $element
112
     *
113
     * @return $this
114
     */
115 8
    public function setElement(AbstractElement $element)
116
    {
117 8
        $this->_element = $element;
118
119 8
        return $this;
120
    }
121
122
    abstract public function generateElement();
123
124
    /**
125
     * @return string
126
     */
127 8
    public function renderErrors()
128
    {
129 8
        $return = '';
130 8
        if ($this->getElement()->isError() && $this->getElement()->getForm()->getOption('renderElementErrors') !== false) {
131
            $errors = $this->getElement()->getErrors();
132
            $errors_string = implode('<br />', $errors);
133
            $return .= '<span class="help-inline">' . $errors_string . '</span>';
134
        }
135
136 8
        return $return;
137
    }
138
139
    /**
140
     * @param array $overrides
141
     *
142
     * @return string
143
     */
144 5
    public function renderAttributes($overrides = [])
145
    {
146 5
        $attribs = $this->getElement()->getAttribs();
147 5
        if (!isset($attribs['title'])) {
148 5
            $attribs['title'] = strip_tags($this->getElement()->getLabel());
149
        }
150 5
        $elementAttribs = $this->getElementAttribs();
151 5
        $return = '';
152 5
        foreach ($attribs as $name => $value) {
153 5
            if (in_array($name, $elementAttribs)) {
154 5
                if (in_array($name, array_keys($overrides))) {
155
                    $value = $overrides[$name];
156
                }
157 5
                if ($name == "name" && $this->getElement()->isGroup()) {
158
                    $value = $value . "[]";
159
                }
160 5
                $return .= ' ' . $name . '="' . $value . '"';
161
            }
162
        }
163
164 5
        return $return;
165
    }
166
167
    /**
168
     * @return array
169
     */
170 5
    public function getElementAttribs()
171
    {
172 5
        return ['id', 'name', 'style', 'class', 'title', 'readonly', 'disabled'];
173
    }
174
}
175