Completed
Push — master ( 3a35d5...02819f )
by Gabriel
08:58
created

AbstractRenderer::getNewButtonRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 11
loc 11
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 9.9
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Nip\Form\Renderer;
4
5
use Nip\Form\AbstractForm;
6
use Nip\Helpers\View\Errors as ErrorsHelper;
7
use Nip\Helpers\View\Messages as MessagesHelper;
8
use Nip_Form_Button_Abstract;
9
use Nip\Form\Elements\AbstractElement;
10
use Nip_Form_Renderer_Button_Abstract as AbstractButtonRenderer;
11
use Nip_Form_Renderer_Elements_Abstract as AbstractElementRenderer;
12
13
/**
14
 * Class AbstractRenderer
15
 * @package Nip\Form\Renderer
16
 */
17
abstract class AbstractRenderer
18
{
19
    protected $form;
20
21
    protected $elements;
22
    protected $elementsRenderer;
23
24
    protected $buttonsRenderer = [];
25
26
    /**
27
     * AbstractRenderer constructor.
28
     */
29 7
    public function __construct()
30
    {
31 7
    }
32
33
    /**
34
     * @return array
35
     */
36
    public function getElements()
37
    {
38
        if (!$this->elements) {
39
            $this->elements = $this->getForm()->getElements();
40
        }
41
42
        return $this->elements;
43
    }
44
45
    /**
46
     * @param $elements
47
     */
48
    public function setElements($elements)
49
    {
50
        $this->elements = $elements;
51
    }
52
53
    /**
54
     * @return AbstractForm
55
     */
56
    public function getForm()
57
    {
58
        return $this->form;
59
    }
60
61
    /**
62
     * @param AbstractForm $form
63
     * @return $this
64
     */
65 7
    public function setForm(AbstractForm $form)
66
    {
67 7
        $this->form = $form;
68
69 7
        return $this;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function render()
76
    {
77
        $return = $this->openTag();
78
        $return .= $this->renderHidden();
79
80
        $renderErrors = $this->getForm()->getOption('render_messages');
81
        if ($renderErrors !== false) {
82
            $return .= $this->renderMessages();
83
        }
84
        $return .= $this->renderGroups();
85
        $return .= $this->renderElements();
86
        $return .= $this->renderButtons();
87
88
        $return .= $this->closeTag();
89
90
        return $return;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function openTag()
97
    {
98
        $return = '<form ';
99
        $atributes = $this->getForm()->getAttribs();
100
        foreach ($atributes as $name => $value) {
101
            $return .= $name . '="' . $value . '" ';
102
        }
103
        $return .= '>';
104
105
        return $return;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function renderHidden()
112
    {
113
        $hiddenElements = $this->getForm()->findElements(['type' => 'hidden']);
0 ignored issues
show
Documentation introduced by
array('type' => 'hidden') is of type array<string,string,{"type":"string"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
        $return = '';
115
        if ($hiddenElements) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $hiddenElements of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
116
            foreach ($hiddenElements as $element) {
117
                $return .= $this->renderElement($element);
118
            }
119
        }
120
121
        return $return;
122
    }
123
124
    /**
125
     * @param AbstractElement $element
126
     * @return mixed
127
     */
128
    public function renderElement(AbstractElement $element)
129
    {
130
        return $element->render();
131
    }
132
133
    /**
134
     * The errors are rendered using the Errors View Helper
135
     * @return string
136
     */
137
    public function renderMessages()
138
    {
139
        $return = '';
140
        $messages = $this->getForm()->getMessages();
141
        foreach ($messages as $type => $lines) {
142
            if ($type == "error") {
143
                $return .= ErrorsHelper::render($lines);
144
            } else {
145
                $return .= MessagesHelper::render($lines, $type);
0 ignored issues
show
Documentation introduced by
$type is of type integer|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
146
            }
147
        }
148
149
        return $return;
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function renderGroups()
156
    {
157
        $groups = $this->getForm()->getDisplayGroups();
158
        $return = '';
159
        foreach ($groups as $group) {
160
            $return .= $group->render();
161
        }
162
163
        return $return;
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function renderElements()
170
    {
171
        return '';
172
    }
173
174
    /**
175
     * @return string
176
     */
177
    public function renderButtons()
178
    {
179
        $return = '';
180
        $buttons = $this->getForm()->getButtons();
181
        if ($buttons) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $buttons of type Nip_Form_Button_Abstract[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
182
            $return .= '<div class="form-actions">';
183
            foreach ($buttons as $button) {
184
                $return .= $button->render() . "\n";
185
            }
186
            $return .= '    <div class="clear"></div>';
187
            $return .= '</div>';
188
        }
189
190
        return $return;
191
    }
192
193
    /**
194
     * @return string
195
     */
196
    public function closeTag()
197
    {
198
        $return = '</form>';
199
200
        return $return;
201
    }
202
203
    /**
204
     * @param string|AbstractElement $label
205
     * @param bool $required
206
     * @param bool $error
207
     * @return string
208
     */
209
    public function renderLabel($label, $required = false, $error = false)
210
    {
211
        if (is_object($label)) {
212
            $element = $label;
213
            $label = $element->getLabel();
214
            $required = $element->isRequired();
215
            $error = $element->isError();
216
        }
217
218
        $return = '<label class="col-sm-3 ' . ($error ? ' error' : '') . '">';
219
        $return .= $label . ':';
220
221
        if ($required) {
222
            $return .= '<span class="required">*</span>';
223
        }
224
225
        $return .= "</label>";
226
227
        return $return;
228
    }
229
230
    /**
231
     * @param AbstractElement $element
232
     * @return mixed
233
     */
234 4
    public function getElementRenderer(AbstractElement $element)
235
    {
236 4
        $name = $element->getUniqueId();
237 4
        if (!isset($this->elementsRenderer[$name])) {
238 4
            $this->elementsRenderer[$name] = $this->getNewElementRenderer($element);
239
        }
240
241 4
        return $this->elementsRenderer[$name];
242
    }
243
244
    /**
245
     * @param AbstractElement $element
246
     * @return mixed
247
     */
248 4 View Code Duplication
    protected function getNewElementRenderer(AbstractElement $element)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
249
    {
250 4
        $type = $element->getType();
251 4
        $name = 'Nip_Form_Renderer_Elements_' . ucfirst($type);
252
        /** @var AbstractElementRenderer $renderer */
253 4
        $renderer = new $name();
254 4
        $renderer->setRenderer($this);
255 4
        $renderer->setElement($element);
256
257 4
        return $renderer;
258
    }
259
260
    /**
261
     * @param Nip_Form_Button_Abstract $button
262
     * @return mixed
263
     */
264
    public function getButtonRenderer(Nip_Form_Button_Abstract $button)
265
    {
266
        $name = $button->getName();
267
        if (!isset($this->buttonsRenderer[$name])) {
268
            $this->buttonsRenderer[$name] = $this->getNewButtonRenderer($button);
269
        }
270
271
        return $this->buttonsRenderer[$name];
272
    }
273
274
    /**
275
     * @param Nip_Form_Button_Abstract $button
276
     * @return mixed
277
     */
278 View Code Duplication
    protected function getNewButtonRenderer(Nip_Form_Button_Abstract $button)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
279
    {
280
        $type = $button->getType();
281
        $name = 'Nip_Form_Renderer_Button_' . ucfirst($type);
282
        /** @var AbstractButtonRenderer $renderer */
283
        $renderer = new $name();
284
        $renderer->setRenderer($this);
285
        $renderer->setItem($button);
286
287
        return $renderer;
288
    }
289
}
290