Completed
Push — develop ( 7d6075...b57d28 )
by
unknown
23:57 queued 15:50
created

SearchForm::get()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 29
rs 8.439
cc 5
eloc 16
nc 9
nop 2
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\Controller\Plugin;
12
13
use Core\Form\TextSearchForm;
14
use Zend\Form\FormElementManager;
15
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
16
17
/**
18
 * Fetches a text search form.
19
 * 
20
 * @author Mathias Gelhausen <[email protected]>
21
 * @since 0.25
22
 */
23
class SearchForm extends AbstractPlugin
24
{
25
26
    /**
27
     * The form element manager.
28
     *
29
     * @var \Zend\Form\FormElementManager
30
     */
31
    protected $formElementManager;
32
33
    /**
34
     * Creates an instance.
35
     *
36
     * @param FormElementManager $forms
37
     */
38
    public function __construct(FormElementManager $forms)
39
    {
40
        $this->formElementManager = $forms;
41
    }
42
43
    /**
44
     * Direct invokation.
45
     *
46
     * Proxies to {@link get()}
47
     *
48
     * @param string|array     $elementsFieldset
49
     * @param null|string $buttonsFieldset
50
     *
51
     * @return \Core\Form\TextSearchForm
52
     */
53
    public function __invoke($elementsFieldset, $buttonsFieldset = null)
54
    {
55
        return $this->get($elementsFieldset, $buttonsFieldset);
56
    }
57
58
    /**
59
     * Fetches a text search form.
60
     *
61
     * If only the service for an element fieldset ist passed,
62
     * it will fetch a "Core/TextSearch" form and pass the
63
     * elements fieldset along.
64
     *
65
     * @param string|array     $elementsFieldset
66
     * @param string|null $buttonsFieldset
67
     *
68
     * @return \Core\Form\TextSearchForm
69
     */
70
    public function get($elementsFieldset, $buttonsFieldset = null)
71
    {
72
        if (is_array($elementsFieldset)) {
73
            $elementsOptions = isset($elementsFieldset[1]) ? $elementsFieldset[1] : [];
74
            $elementsFieldset = $elementsFieldset[0];
75
76
        } else {
77
            $elementsOptions = [];
78
        }
79
80
        $form             = $this->formElementManager->get($elementsFieldset, $elementsOptions);
81
        /** @noinspection PhpUndefinedMethodInspection */
82
        $params           = $this->getController()->getRequest()->getQuery()->toArray();
83
84
        if (!$form instanceOf TextSearchForm) {
85
86
            $options = [
87
                'elements_fieldset' => $form,
88
            ];
89
            if (null !== $buttonsFieldset) {
90
                $options['buttons_fieldset'] = $buttonsFieldset;
91
            }
92
93
            $form = $this->formElementManager->get('Core/TextSearch', $options);
94
        }
95
96
        $form->setSearchParams($params);
97
        return $form;
98
    }
99
}