|
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(); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
$this->isBuild = true; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check looks for function calls that miss required arguments.