Passed
Push — develop ( 28c299...928e2a )
by Mathias
12:40
created

FormRowCombined::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
c 0
b 0
f 0
ccs 5
cts 6
cp 0.8333
rs 10
cc 3
nc 3
nop 4
crap 3.0416
1
<?php
2
3
namespace Core\Form\View\Helper;
4
5
use Zend\Form\View\Helper\FormRow as ZendFormRow;
6
use Zend\Form\View\Helper\AbstractHelper;
7
use Zend\Form\ElementInterface;
8
9
class FormRowCombined extends AbstractHelper
10
{
11
    protected $layout;
12
     
13 1
    public function getFormRowHelper()
14
    {
15 1
        $helper = clone $this->view->plugin('formRow');
0 ignored issues
show
Bug introduced by
The method plugin() does not exist on Zend\View\Renderer\RendererInterface. It seems like you code against a sub-type of Zend\View\Renderer\RendererInterface such as Zend\View\Renderer\PhpRenderer. ( Ignorable by Annotation )

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

15
        $helper = clone $this->view->/** @scrutinizer ignore-call */ plugin('formRow');
Loading history...
16 1
        $helper->setShouldWrap(false);
17 1
        return $helper;
18
    }
19
    
20
    /**
21
     * Utility form helper that renders a label (if it exists), an element and errors
22
     *
23
     * @param ElementInterface $elements
24
     * @return string
25
     * @throws \Zend\Form\Exception\DomainException
26
     */
27 1
    public function render(array $elements)
28
    {
29 1
        $formRowHelper = $this->getFormRowHelper();
30 1
        $labels = array();
31 1
        $markups = '';
32 1
        $totalSpanWidth = 0;
33 1
        foreach ($elements as $spanWidth => $element) {
34 1
            $elementErrors = $element->getMessages();
35 1
            $markup = $formRowHelper->render($element);
36 1
            if (preg_match('~<label.*</label>~isU', $markup, $match)) {
37 1
                $labels[] = $match[0];
38 1
                $markup = str_replace($match[0], '', $markup);
39
            } else {
40
                //$labels[] = false;
41
            }
42 1
            $markups .= sprintf(
43 1
                '<div class="col-md-%d%s">%s</div>',
44 1
                $spanWidth,
45 1
                empty($elementErrors) ? '' : ' input-error',
46 1
                $markup
47
            );
48 1
            $totalSpanWidth += $spanWidth;
49
        }
50 1
        $labelSpanWidth = 12 - $totalSpanWidth;
51
        
52 1
        $labelMarkup = sprintf(
53 1
            '<div class="col-md-%d yk-label">%s</div>',
54 1
            $labelSpanWidth,
55 1
            implode(' / ', $labels)
56
        );
57
        
58 1
        $form_row_class = 'row';
59 1
        if ($this->layout == 'form-horizontal') {
60 1
            $form_row_class = 'form-group';
61
        }
62
        
63
        
64 1
        return sprintf(
65 1
            '<div class="controls controls-row ' . $form_row_class . '">%s%s</div>',
66 1
            $labelMarkup,
67 1
            $markups
68
        );
69
    }
70
    
71
    /**
72
     * Invoke helper as function
73
     *
74
     * Proxies to {@link render()}.
75
     *
76
     * @param null|ElementInterface $element
77
     * @param null|string           $labelPosition
78
     * @param bool                  $renderErrors
79
     * @return string|FormRow
80
     */
81 1
    public function __invoke(array $elements = array(), $labelPosition = null, $renderErrors = null, $layout = null)
82
    {
83 1
        if (empty($elements)) {
84
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Core\Form\View\Helper\FormRowCombined which is incompatible with the documented return type Core\Form\View\Helper\FormRow|string.
Loading history...
85
        }
86
        
87 1
        if (null !== $layout) {
88 1
            $this->layout = $layout;
89
        }
90
        
91 1
        return $this->render($elements);
92
    }
93
}
94