Completed
Push — develop ( 3fec75...18e071 )
by
unknown
07:26
created

CustomizableFieldsetTrait::addElementOrFieldset()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 6
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Form;
12
13
use Core\Options\FieldsetCustomizationOptions;
14
use Zend\Stdlib\ArrayUtils;
15
16
/**
17
 * ${CARET}
18
 * 
19
 * @author Mathias Gelhausen <[email protected]>
20
 * @todo write test 
21
 */
22
trait CustomizableFieldsetTrait
23
{
24
    /**
25
     * The customization options.
26
     *
27
     * @var FieldsetCustomizationOptions
28
     */
29
    protected $customizationOptions;
30
31
    public function setCustomizationOptions(FieldsetCustomizationOptions $options)
32
    {
33
        $this->customizationOptions = $options;
34
35
        return $this;
36
    }
37
38
    /**
39
     * @return \Core\Options\FieldsetCustomizationOptions
40
     */
41
    public function getCustomizationOptions()
42
    {
43
        if (!isset($this->customizationOptions)) {
44
            $this->setCustomizationOptions(new FieldsetCustomizationOptions());
45
        }
46
47
        return $this->customizationOptions;
48
    }
49
50
    protected function addElementOrFieldset($elementOrFieldset, array $flags = array())
51
    {
52
        if (!is_array($elementOrFieldset) || !$this->customizationOptions) {
53
            /** @noinspection PhpUndefinedClassInspection */
54
            return parent::add($elementOrFieldset, $flags);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (add() instead of addElementOrFieldset()). Are you sure this is correct? If so, you might want to change this to $this->add().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
55
        }
56
57
        if (isset($elementOrFieldset['name'])) {
58
            $name = $elementOrFieldset['name'];
59
        } else if (isset($flags['name'])) {
60
            $name = $flags['name'];
61
        } else {
62
            /** @noinspection PhpUndefinedClassInspection */
63
            return parent::add($elementOrFieldset, $flags);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (add() instead of addElementOrFieldset()). Are you sure this is correct? If so, you might want to change this to $this->add().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
64
        }
65
66
        /* @var FieldsetCustomizationOptions $customOpts */
67
        $customOpts = $this->getCustomizationOptions();
68
69
        if (!$customOpts->isEnabled($name)) {
70
            return $this;
71
        }
72
73
        $elementOrFieldset = ArrayUtils::merge($elementOrFieldset, $customOpts->getFieldOptions($name));
74
        $flags             = ArrayUtils::merge($flags, $customOpts->getFieldFlags($name));
75
76
        /** @noinspection PhpUndefinedClassInspection */
77
        return parent::add($elementOrFieldset, $flags);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (add() instead of addElementOrFieldset()). Are you sure this is correct? If so, you might want to change this to $this->add().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
78
    }
79
80
    public function add($elementOrFieldset, array $flags = array())
81
    {
82
        return $this->addElementOrFieldset($elementOrFieldset, $flags);
83
    }
84
85
    protected function mergeInputFilterSpecifications(array $specification)
86
    {
87
        /* @var FieldsetCustomizationOptions $customOpts */
88
        $customOpts = $this->getCustomizationOptions();
89
90
        foreach ($customOpts->getFieldNames() as $name) {
91
            if (!isset($specification[$name])) {
92
                $specification[$name] = [];
93
            }
94
95
            $specification[$name] = ArrayUtils::merge(
96
                $specification[$name],
97
                $customOpts->getFieldInputSpecification($name)
98
            );
99
        }
100
101
        return $specification;
102
    }
103
104
    protected function getDefaultInputFilterSpecification()
105
    {
106
        return [];
107
    }
108
109
    public function getInputFilterSpecification()
110
    {
111
        return $this->mergeInputFilterSpecifications($this->getDefaultInputFilterSpecification());
112
    }
113
114
}