1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\SettingsBundle\Settings\Personal; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Manages settings structure that is displayed for the user. |
16
|
|
|
* |
17
|
|
|
* Class is not aware of user selected values, only of structure. |
18
|
|
|
*/ |
19
|
|
|
class SettingsStructure |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $settingsParameter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $categoriesParameter; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $structure = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $settingsParameter |
38
|
|
|
* @param array $categoriesParameter |
39
|
|
|
*/ |
40
|
|
|
public function __construct(array $settingsParameter, array $categoriesParameter) |
41
|
|
|
{ |
42
|
|
|
$this->settingsParameter = $settingsParameter; |
43
|
|
|
$this->categoriesParameter = $categoriesParameter; |
44
|
|
|
|
45
|
|
|
foreach ($settingsParameter as $settingId => $setting) { |
46
|
|
|
$this->addSetting($settingId, $setting); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Call service's method and store returned additional structure. |
52
|
|
|
* |
53
|
|
|
* @param object $service |
54
|
|
|
* @param string $method |
55
|
|
|
* |
56
|
|
|
* @throws \InvalidArgumentException |
57
|
|
|
*/ |
58
|
|
|
public function extractSettings($service, $method) |
59
|
|
|
{ |
60
|
|
|
$newSettings = $service->$method(); |
61
|
|
|
foreach ($newSettings as $settingId => $setting) { |
62
|
|
|
$this->addSetting($settingId, $setting); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add setting to structure recursively. |
68
|
|
|
* |
69
|
|
|
* E.g. |
70
|
|
|
* |
71
|
|
|
* self::addSetting('setting_id', [ |
72
|
|
|
* 'name' => 'Setting label', |
73
|
|
|
* 'category' => 'category_id', |
74
|
|
|
* 'description' => 'Description', |
75
|
|
|
* 'type' => ['choice', ['expanded' => true, 'multiple' => true], # or 'type' => [$customType, [$options]] |
76
|
|
|
* ]); |
77
|
|
|
* |
78
|
|
|
* @param string $id |
79
|
|
|
* @param array $setting |
80
|
|
|
*/ |
81
|
|
|
public function addSetting($id, array $setting) |
82
|
|
|
{ |
83
|
|
|
$this->structure[$id] = $this->ensureSettingStash($setting); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Ensure 'stash' key is set. If not, default value is set. |
88
|
|
|
* |
89
|
|
|
* @param array $setting |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function ensureSettingStash(array $setting) |
94
|
|
|
{ |
95
|
|
|
if (!isset($setting['stash'])) { |
96
|
|
|
$setting['stash'] = 'ongr_settings.settings.';//.$setting['id']; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $setting; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function getStructure() |
106
|
|
|
{ |
107
|
|
|
return $this->structure; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $structure |
112
|
|
|
*/ |
113
|
|
|
public function setStructure($structure) |
114
|
|
|
{ |
115
|
|
|
$this->structure = []; |
116
|
|
|
|
117
|
|
|
foreach ($structure as $id => $setting) { |
118
|
|
|
$this->addSetting($id, $setting); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function getCategoriesStructure() |
126
|
|
|
{ |
127
|
|
|
return $this->categoriesParameter; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array $categoriesParameter |
132
|
|
|
*/ |
133
|
|
|
public function setCategoriesStructure($categoriesParameter) |
134
|
|
|
{ |
135
|
|
|
$this->categoriesParameter = $categoriesParameter; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.