Completed
Push — develop ( 3d2ac1...0e6c87 )
by
unknown
13:24
created

SettingsFieldset::factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Settings\Form;
4
5
use Zend\Form\Fieldset;
6
use Settings\Entity\SettingsContainerInterface;
7
use Settings\Entity\ModuleSettingsContainerInterface;
8
use Settings\Entity\Hydrator\SettingsEntityHydrator;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
11
class SettingsFieldset extends Fieldset
12
{
13
    /**
14
     * @var ServiceLocatorInterface
15
     */
16
    protected $formManager;
17
    
18
    protected $isBuild = false;
19
    protected $labelMap = [];
20
    
21
    /**
22
     * @param ServiceLocatorInterface $formManager
23
     */
24
    public function __construct(ServiceLocatorInterface $formManager)
25
    {
26
        parent::__construct();
27
        $this->formManager = $formManager;
28
    }
29
30
    public function getHydrator()
31
    {
32
        if (!$this->hydrator) {
33
            $hydrator = new SettingsEntityHydrator();
34
            $this->setHydrator($hydrator);
35
        }
36
        return $this->hydrator;
37
    }
38
    
39
    public function setObject($object)
40
    {
41
        parent::setObject($object);
42
        $this->build();
43
        return $this;
44
    }
45
    
46
    public function build()
47
    {
48
        
49
        if ($this->isBuild) {
50
            return;
51
        }
52
        
53
        $settings = $this->getObject();
54
        $reflection = new \ReflectionClass($settings);
55
        $properties = $reflection->getProperties();
56
        
57
        $skipProperties = array('_settings', 'isWritable');
58
        if ($settings instanceof ModuleSettingsContainerInterface) {
59
            $skipProperties[] = '_module';
60
        }
61
        $children = array();
62
        foreach ($properties as $property) {
63
            if (in_array($property->getName(), $skipProperties) || $this->has($property->getName())) {
64
                continue;
65
            }
66
            $property->setAccessible(true);
67
            $value = $property->getValue($settings);
68
            if ($value instanceof SettingsContainerInterface) {
69
                $children[$property->getName()] = $value;
70
                continue;
71
            }
72
73
            $inputName = $property->getName();
74
75
            $inputLabel = isset($this->labelMap[$inputName]) ? $this->labelMap[$inputName] : $inputName;
76
77
            if (is_array($inputLabel)){
78
                $priority = isset($inputLabel[1])?$inputLabel[1]:0;
79
                $inputLabel = $inputLabel[0];
80
            }else{
81
                $priority = 0;
82
            }
83
84
            $input = array(
85
                    'name' => $inputName,
86
                    'options' => array(
87
88
                        'label' => $inputLabel
89
                    ),
90
            );
91
            if (is_bool($value)) {
92
                $input['type']= 'Checkbox';
93
                $input['attributes']['checked'] = $value;
94
            } else {
95
                $input['attributes']['value'] = $value;
96
            }
97
            $this->add($input,['priority'=>$priority]);
98
            
99
        }
100
        
101
        foreach ($children as $name => $child) {
102
            $objectClass  = ltrim(get_class($settings), '\\');
103
            $moduleName   = substr($objectClass, 0, strpos($objectClass, '\\'));
104
            $fieldsetName = $moduleName . '/' . ucfirst($name) . 'SettingsFieldset';
105
            
106
            if ($this->formManager->has($fieldsetName)) {
107
                $fieldset = $this->formManager->get($fieldsetName);
108
                if (!$fieldset->getHydrator() instanceof SettingsEntityHydrator) {
109
                    $fieldset->setHydrator($this->getHydrator());
110
                }
111
            } else {
112
                $fieldset = new self();
0 ignored issues
show
Bug introduced by
The call to SettingsFieldset::__construct() misses a required argument $formManager.

This check looks for function calls that miss required arguments.

Loading history...
113
                $label = preg_replace('~([A-Z])~', ' $1', $name);
114
                $fieldset->setLabel(ucfirst($label));
115
            }
116
            $fieldset->setName($name)
117
                     ->setObject($child);
118
            
119
            
120
            $this->add($fieldset);
0 ignored issues
show
Bug introduced by
It seems like $fieldset defined by $this->formManager->get($fieldsetName) on line 107 can also be of type object; however, Zend\Form\Fieldset::add() does only seem to accept array|object<Traversable...\Form\ElementInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
121
        }
122
        $this->isBuild = true;
123
    }
124
}
125