Completed
Push — develop ( dfd31d...f6e11d )
by
unknown
25:04 queued 17:04
created

setServiceLocator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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 Settings\Form;
12
13
use Zend\Form\Fieldset;
14
use Settings\Entity\Hydrator\SettingsEntityHydrator;
15
use Settings\Entity\Hydrator\Strategy\DisableElementsCapableFormSettings as DisableElementsStrategy;
16
use Zend\ServiceManager\ServiceLocatorInterface;
17
18
/**
19
 * Fieldset for toggling form elements of DisableCapable forms.
20
 *
21
 * @author Mathias Gelhausen <[email protected]>
22
 */
23
class DisableElementsCapableFormSettingsFieldset extends Fieldset
24
{
25
    /**
26
     * The form elements manager.
27
     *
28
     * @var ServiceLocatorInterface
29
     */
30
    protected $formManager;
31
32
    /**
33
     * Flag if this fieldset is build.
34
     * @var bool
35
     */
36
    protected $isBuild = false;
37
    
38
    /**
39
     * @param ServiceLocatorInterface $formManager
40
     */
41
    public function __construct(ServiceLocatorInterface $formManager)
42
    {
43
        parent::__construct();
44
        $this->formManager = $formManager;
45
    }
46
47
    public function getHydrator()
48
    {
49
        if (!$this->hydrator) {
50
            $hydrator = new SettingsEntityHydrator();
51
            $hydrator->addStrategy('disableElements', new DisableElementsStrategy());
52
            $this->setHydrator($hydrator);
53
        }
54
        return $this->hydrator;
55
    }
56
57
    /**
58
     * @uses build()
59
     */
60
    public function setObject($object)
61
    {
62
        parent::setObject($object);
63
        $this->build();
64
        return $this;
65
    }
66
67
    /**
68
     * Builds this fieldset.
69
     *
70
     * Adds the disableElements element and populate its values,
71
     * which is only possible, if the bound object is set.
72
     */
73
    public function build()
74
    {
75
        
76
        if ($this->isBuild) {
77
            return;
78
        }
79
80
        $settings = $this->getObject();
81
        $form     = $this->formManager->get($settings->getForm());
82
83
        $this->setLabel(
84
            $form->getOption('settings_label') ?: sprintf(
85
                /*@translate*/ 'Customize enabled elements for %s',
86
                get_class($form)
87
            )
88
        );
89
90
        $this->add(
91
            array(
92
            'type' => 'Checkbox',
93
            'name' => 'isActive',
94
            'options' => array(
95
                'label' => /*@translate*/ 'Activate',
96
                'long_label' => /*@translate*/ 'Enables the form element customization.',
97
            ),
98
            'attributes' => array(
99
                'class' => 'decfs-active-toggle',
100
            ),
101
            )
102
        );
103
        $element = new \Settings\Form\Element\DisableElementsCapableFormSettings('disableElements');
104
        $element->setForm($form);
0 ignored issues
show
Documentation introduced by
$form is of type object|array, but the function expects a object<Zend\Form\FormInt...ct<Core\Form\Container>.

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...
105
        $this->add($element);
106
107
        $this->isBuild = true;
108
    }
109
    
110
    /**
111
     * @param ServiceLocatorInterface $formManager
112
     * @return AbstractSettingsForm
0 ignored issues
show
Documentation introduced by
Should the return type not be DisableElementsCapableFormSettingsFieldset?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
113
     */
114
    public static function factory(ServiceLocatorInterface $formManager)
115
    {
116
        return new static($formManager);
117
    }
118
}
119