Completed
Push — develop ( 41c7f2...c75883 )
by
unknown
08:12
created

SearchForm   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 16
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 84
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 8 2
D render() 0 64 14
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Form\View\Helper;
12
13
use Zend\Form\FieldsetInterface;
14
use Zend\Form\FormInterface;
15
use Zend\Form\View\Helper\Form;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Core\Form\View\Helper\Form.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
17
/**
18
 * ${CARET}
19
 * 
20
 * @author Mathias Gelhausen <[email protected]>
21
 * @todo write test 
22
 */
23
class SearchForm extends Form
24
{
25
26
    /**
27
     * Invoke as function
28
     *
29
     * @param  null|FormInterface $form
30
     * @param array $colMap
31
     * @return Form|string
32
     */
33
    public function __invoke(FormInterface $form = null, $colMap=[])
34
    {
35
        if (!$form) {
36
            return $this;
37
        }
38
39
        return $this->render($form, $colMap);
40
    }
41
42
    public function render(FormInterface $form, $colMap=[])
43
    {
44
        if (method_exists($form, 'prepare')) {
45
            $form->prepare();
0 ignored issues
show
Bug introduced by
The method prepare() does not exist on Zend\Form\FormInterface. Did you maybe mean prepareElement()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
        }
47
48
        $params = $this->getView()->params()->fromQuery();
0 ignored issues
show
Bug introduced by
The method params() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
        $form->setAttribute('data-search-params', \Zend\Json\Json::encode($params));
50
51
        $formContent = '<div class="row" style="padding: 0 15px;">'; $buttonsRendered = false;
52
        $buttonsContent = '<input type="submit" class="btn btn-primary" name="submit" value="' . $this->getView()->translate('Search') . '">'
0 ignored issues
show
Bug introduced by
The method translate() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
                          . '<input type="reset" class="btn btn-default" name="clear" value="' . $this->getView()->translate('Clear') . '">';
0 ignored issues
show
Bug introduced by
The method translate() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
55
56
        if (empty($colMap)) {
57
            $c = count($form);
58
            $r = floor($c / 3);
59
60
            if (0 != $r) {
61
                for ($i=0; $i<$r; $i+=1) {
62
                    $colMap[] = 4;
63
                    $colMap[] = 4;
64
                    $colMap[] = 4;
65
                }
66
            }
67
            if ($l = $c % 3) {
68
                for ($i=0; $i<$l; $i+=1) {
69
                    $colMap[] = 12 / $l;
70
                }
71
            }
72
        }
73
74
        $i = 0;
75
        foreach ($form as $element) {
76
            if ($element instanceof FieldsetInterface) {
77
                trigger_error('Fieldsets are not allowed in a search form.', E_USER_NOTICE);
78
                continue;
79
            } else {
80
                $col = isset($colMap[$element->getName()])
81
                    ? $colMap[$element->getName()]
82
                    : (isset($colMap[$i]) ? $colMap[$i] : 4);
83
                $i += 1;
84
85
                if ($element->getName() == $form->getOption('button_element')) {
86
                    $formContent.='<div class="input-group col-md-' . $col . '">'
87
                                 . $this->getView()->formElement($element)
0 ignored issues
show
Bug introduced by
The method formElement() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
                                 . '<div class="input-group-btn search-form-buttons" style="width: 0px;">' . $buttonsContent . '</div>'
89
                                 . '</div>';
90
                    $buttonsRendered = true;
91
                } else {
92
                    $formContent .= '<div class="input-group col-md-' . $col . '">' . $this->getView()->formElement($element) . '</div>';
0 ignored issues
show
Bug introduced by
The method formElement() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
                }
94
            }
95
        }
96
97
        if (!$buttonsRendered) {
98
            $c = count($form);
99
            $col = isset($colMap[$c]) ? $colMap[$c]: 12;
100
            $formContent .= '<div class="input-group search-form-buttons col-md-' . $col . ' text-right">'
101
                          . '<div class="btn-group">' . $buttonsContent .'</div></div>';
102
        }
103
104
        return $this->openTag($form) . $formContent . $this->closeTag();
105
    }
106
}