Completed
Push — develop ( 168e99...0122be )
by
unknown
18:37 queued 07:11
created

WizardContainer::getForm()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 5
eloc 6
nc 2
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\Form;
12
13
use Zend\Form\Element;
14
use Zend\Stdlib\PriorityList;
15
16
/**
17
 * ${CARET}
18
 * 
19
 * @author Mathias Gelhausen <[email protected]>
20
 * @todo write test 
21
 */
22
class WizardContainer extends Container implements HeadscriptProviderInterface, \IteratorAggregate
23
{
24
    protected $tabs = [];
25
    protected $scripts = [
26
        '/js/jquery.bootstrapwizard.min.js',
27
    ];
28
29
    protected $tabContainerPrototype;
30
31
    /**
32
     * Sets the array of script names.
33
     *
34
     * @param string[] $scripts
35
     *
36
     * @return self
37
     */
38
    public function setHeadscripts(array $scripts)
39
    {
40
        $this->scripts = $scripts;
41
42
        return $this;
43
    }
44
45
    /**
46
     * Gets the array of script names.
47
     *
48
     * @return string[]
49
     */
50
    public function getHeadscripts()
51
    {
52
        return $this->scripts;
53
    }
54
55
    public function setForm($key, $spec, $enabled = true)
56
    {
57
        if (is_object($spec)) {
58
            if (!$spec instanceOf Container) {
59
                throw new \InvalidArgumentException('Tab container must be of the type \Core\Form\Container');
60
            }
61
62
            if (!$spec->getLabel()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $spec->getLabel() of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
63
                throw new \InvalidArgumentException('Container instances must have a label.');
64
            }
65
        }
66
67
        if (is_array($spec)) {
68
            if (!isset($spec['type'])) {
69
                $spec['type'] = 'Core/Container';
70
            }
71
72
            /*
73
             * For convenience, forms may be specified outside the options array.
74
             * But in order to be passed through to the form element manager,
75
             * we must move it to the options.
76
             */
77
            if (!isset($spec['options']['forms']) && isset($spec['forms'])) {
78
                $spec['options']['forms'] = $spec['forms'];
79
                unset($spec['forms']);
80
            }
81
        }
82
83
        return parent::setForm($key, $spec, $enabled);
84
    }
85
86
    public function getForm($key, $asInstance = true)
87
    {
88
        $form = parent::getForm($key, $asInstance);
89
90
        /*
91
         * We must check here, if a lazy loaded top level form is an
92
         * instance of Container.
93
         */
94
        if ($asInstance && false === strpos($key, '.') && (!$form instanceOf Container || !$form->getLabel())) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $form->getLabel() of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
95
            throw new \UnexpectedValueException(sprintf(
96
                'The registered form with key "%s" is not an instance of \Core\Form\Container or does not have a label.', $key
97
            ));
98
        }
99
100
        return $form;
101
    }
102
}