Skip to content

Commit 880f648

Browse files
committed
Allow more operations on panel array
1 parent fed5304 commit 880f648

File tree

1 file changed

+64
-21
lines changed

1 file changed

+64
-21
lines changed

DashboardPanelInstance.class.php

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,34 @@ public function add($config)
120120
// keys, so numerical keys indicate an array of configs
121121
if (is_array($config) && is_int(array_key_first($config))) {
122122
return $this->import($config);
123+
} else {
124+
return parent::add($this->createInstance($config));
123125
}
124-
$instance = $this->createInstance($config);
125-
126-
return parent::add($instance);
127126
}
128127

129128
public function set($key, $value)
130129
{
131-
$instance = $this->createInstance($value);
130+
return parent::set($key, $this->createInstance($value));
131+
}
132+
133+
public function prepend($item)
134+
{
135+
return parent::prepend($this->createInstance($item));
136+
}
137+
138+
public function unshift($item)
139+
{
140+
return parent::unshift($this->createInstance($item));
141+
}
142+
143+
public function insertBefore($item, $existingItem)
144+
{
145+
return parent::insertBefore($this->createInstance($item), $existingItem);
146+
}
132147

133-
return parent::set($key, $instance);
148+
public function insertAfter($item, $existingItem)
149+
{
150+
return parent::insertAfter($this->createInstance($item), $existingItem);
134151
}
135152

136153
public function import($items)
@@ -143,18 +160,46 @@ public function import($items)
143160
return parent::import($instances);
144161
}
145162

163+
/**
164+
* Create a panel and return it.
165+
*
166+
* @param array $config
167+
*
168+
* @return DashboardPanelGroup
169+
*/
170+
public function createPanel(array $config)
171+
{
172+
if (!is_array($config)) {
173+
return;
174+
}
175+
if (!($config['panel'] ?? false)) {
176+
throw new \Exception('Missing required `panel` parameter');
177+
}
178+
179+
$config['type'] = 'panel';
180+
if ($config['data'] ?? false) {
181+
$config['dataArray'] = $config['data'];
182+
unset($config['data']);
183+
}
184+
$instance = new DashboardPanelInstance();
185+
$instance->setArray($config);
186+
187+
return $instance;
188+
}
189+
146190
/**
147191
* Create a group of panels and return it.
148192
*
149193
* @param array $config
150194
*
151195
* @return DashboardPanelGroup
152196
*/
153-
public function createGroup($config)
197+
public function createGroup(array $config)
154198
{
155199
if (!is_array($config)) {
156200
return;
157201
}
202+
158203
$config['type'] = 'group';
159204
$config['panels'] = new self();
160205
$group = new DashboardPanelGroup();
@@ -170,11 +215,12 @@ public function createGroup($config)
170215
*
171216
* @return DashboardPanelTab
172217
*/
173-
public function createTab($config)
218+
public function createTab(array $config)
174219
{
175220
if (!is_array($config)) {
176221
return;
177222
}
223+
178224
$config['type'] = 'tab';
179225
$config['panels'] = new self();
180226
$tab = new DashboardPanelTab();
@@ -192,21 +238,18 @@ public function createTab($config)
192238
*/
193239
public function createInstance($config)
194240
{
195-
if (is_array($config)) {
196-
if (!($config['panel'] ?? false)) {
197-
throw new \Exception('Missing required `panel` parameter');
198-
}
199-
$config['type'] = 'panel';
200-
if ($config['data'] ?? false) {
201-
$config['dataArray'] = $config['data'];
202-
unset($config['data']);
203-
}
204-
$instance = new DashboardPanelInstance();
205-
$instance->setArray($config);
206-
207-
return $instance;
208-
} else {
241+
if (!is_array($config)) {
209242
return $config;
210243
}
244+
245+
switch ($config['type'] ?? null) {
246+
case 'tab':
247+
return $this->createTab($config);
248+
case 'group':
249+
return $this->createGroup($config);
250+
case 'panel':
251+
default:
252+
return $this->createPanel($config);
253+
}
211254
}
212255
}

0 commit comments

Comments
 (0)