Skip to content

Commit 2079075

Browse files
committed
DRAFT: fix: getType(): flatten named parameters before calling init()
1 parent 2c82977 commit 2079075

File tree

2 files changed

+115
-42
lines changed

2 files changed

+115
-42
lines changed

lib/Horde/Form.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
*/
3333
class Horde_Form
3434
{
35+
protected static $init_params_cache = [];
36+
3537
protected $_name = '';
3638
protected $_title = '';
3739
protected $_extra = '';
@@ -163,7 +165,7 @@ public function getRenderer($params = [])
163165
*
164166
* @throws Horde_Exception
165167
*/
166-
public function getType($type, $params = [])
168+
private function getType($type, $params = [])
167169
{
168170
if (strpos($type, ':') !== false) {
169171
[$app, $type] = explode(':', $type);
@@ -178,6 +180,25 @@ public function getType($type, $params = [])
178180
if (!$params) {
179181
$params = [];
180182
}
183+
184+
// retrieve list of parameters
185+
$keys = self::$init_params_cache[$type_class] ?? null;
186+
if (is_null($keys)) {
187+
$keys = array_keys($type_ob->about()['params'] ?? []);
188+
self::$init_params_cache[$type_class] = $keys;
189+
}
190+
191+
// convert named keys to numeric indexes
192+
$i = 0;
193+
$modified = false;
194+
foreach ($keys as $key) {
195+
if (array_key_exists($key, $params)) {
196+
$params[$i] = $params[$key];
197+
unset($params[$key]);
198+
}
199+
++$i;
200+
}
201+
181202
call_user_func_array([$type_ob, 'init'], $params);
182203
return $type_ob;
183204
}

lib/Horde/Form/Type.php

Lines changed: 93 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public function invalid(string $message): bool
9191
return false;
9292
}
9393

94+
public function about()
95+
{
96+
return ['name' => $this->getTypeName()];
97+
}
98+
9499
}
95100

96101
class Horde_Form_Type_spacer extends Horde_Form_Type
@@ -244,7 +249,11 @@ public function getInfo($vars, $var, $info)
244249
*/
245250
public function about()
246251
{
247-
return ['name' => Horde_Form_Translation::t("Number")];
252+
return [
253+
'name' => Horde_Form_Translation::t("Number"),
254+
'params' => [
255+
'fraction' => ['label' => Horde_Form_Translation::t("Fraction"),
256+
'type' => 'int']]];
248257
}
249258

250259
}
@@ -1480,7 +1489,10 @@ public function about()
14801489
return [
14811490
'name' => Horde_Form_Translation::t("Link"),
14821491
'params' => [
1483-
'url' => [
1492+
'values' => [
1493+
'label' => Horde_Form_Translation::t("Values"),
1494+
'type' => 'array']
1495+
/* 'url' => [
14841496
'label' => Horde_Form_Translation::t("Link URL"),
14851497
'type' => 'text'],
14861498
'text' => [
@@ -1501,7 +1513,8 @@ public function about()
15011513
'class' => [
15021514
'label' => Horde_Form_Translation::t("Link CSS class"),
15031515
'type' => 'text'],
1504-
],
1516+
*/
1517+
]
15051518
];
15061519
}
15071520

@@ -2270,17 +2283,32 @@ public function getInfo($vars, $var, $info)
22702283
unset($values['n']);
22712284
}
22722285

2273-
$info = ($values['r'] ?? []);
2274-
return $info;
2286+
return $values['r'] ?? [];
22752287
}
22762288

22772289
public function about()
22782290
{
22792291
return [
22802292
'name' => Horde_Form_Translation::t("Field matrix"),
22812293
'params' => [
2282-
'cols' => ['label' => Horde_Form_Translation::t("Column titles"),
2283-
'type' => 'stringarray']]];
2294+
'cols' => [
2295+
'label' => Horde_Form_Translation::t("Column titles"),
2296+
'type' => 'stringarray'
2297+
],
2298+
'rows' => [
2299+
'label' => Horde_Form_Translation::t("Row titles"),
2300+
'type' => 'stringarray'
2301+
],
2302+
'matrix' => [
2303+
'label' => Horde_Form_Translation::t("Values"),
2304+
'type' => 'stringarray'
2305+
],
2306+
'new_input' => [
2307+
'label' => Horde_Form_Translation::t("New Input"),
2308+
'type' => 'boolean'
2309+
]
2310+
]
2311+
];
22842312
}
22852313

22862314
}
@@ -2523,7 +2551,7 @@ public function about()
25232551
'params' => [
25242552
'values' => ['label' => Horde_Form_Translation::t("Values to select from"),
25252553
'type' => 'stringarray'],
2526-
'prompt' => ['label' => Horde_Form_Translation::t("Prompt text"),
2554+
'prompts' => ['label' => Horde_Form_Translation::t("Prompt text"),
25272555
'type' => 'text']]];
25282556
}
25292557

@@ -2654,16 +2682,8 @@ class Horde_Form_Type_set extends Horde_Form_Type
26542682
*/
26552683
public function init(...$params)
26562684
{
2657-
if (is_array($params) && array_key_exists('values', $params)) {
2658-
$this->_values = $params['values'];
2659-
} else {
2660-
$this->_values = $params[0];
2661-
}
2662-
if (is_array($params) && array_key_exists('checkAll', $params)) {
2663-
$this->_checkAll = (bool) $params['checkAll'];
2664-
} else {
2665-
$this->_checkAll = $params[1] ?? false;
2666-
}
2685+
$this->_values = $params[0];
2686+
$this->_checkAll = $params[1] ?? false;
26672687
}
26682688

26692689
public function isValid($var, $vars, $value, $message)
@@ -2694,8 +2714,16 @@ public function about()
26942714
return [
26952715
'name' => Horde_Form_Translation::t("Set"),
26962716
'params' => [
2697-
'values' => ['label' => Horde_Form_Translation::t("Values"),
2698-
'type' => 'stringarray']]];
2717+
'values' => [
2718+
'label' => Horde_Form_Translation::t("Values"),
2719+
'type' => 'stringarray'
2720+
],
2721+
'checkAll' => [
2722+
'label' => Horde_Form_Translation::t("Check all"),
2723+
'type' => 'boolean'
2724+
]
2725+
]
2726+
];
26992727
}
27002728

27012729
}
@@ -2777,7 +2805,15 @@ public function getFormattedTime($timestamp, $format = null, $showago = true)
27772805
*/
27782806
public function about()
27792807
{
2780-
return ['name' => Horde_Form_Translation::t("Date")];
2808+
return [
2809+
'name' => Horde_Form_Translation::t("Date"),
2810+
'params' => [
2811+
'format' => [
2812+
'label' => Horde_Form_Translation::t("Format"),
2813+
'type' => 'string'
2814+
]
2815+
]
2816+
];
27812817
}
27822818

27832819
}
@@ -2917,7 +2953,7 @@ public function about()
29172953
return [
29182954
'name' => Horde_Form_Translation::t("Time selection"),
29192955
'params' => [
2920-
'seconds' => ['label' => Horde_Form_Translation::t("Show seconds?"),
2956+
'show_seconds' => ['label' => Horde_Form_Translation::t("Show seconds?"),
29212957
'type' => 'boolean']]];
29222958
}
29232959

@@ -2935,8 +2971,8 @@ class Horde_Form_Type_monthyear extends Horde_Form_Type
29352971
*/
29362972
public function init(...$params)
29372973
{
2938-
$start_year = $params['start_year'] ?? $params[0] ?? null;
2939-
$end_year = $params['end_year'] ?? $params[1] ?? null;
2974+
$start_year = $params[0] ?? null;
2975+
$end_year = $params[1] ?? null;
29402976

29412977
if (empty($start_year)) {
29422978
$start_year = 1920;
@@ -3013,11 +3049,11 @@ class Horde_Form_Type_monthdayyear extends Horde_Form_Type
30133049
*/
30143050
public function init(...$params)
30153051
{
3016-
$start_year = $params['start_year'] ?? $params[0] ?? '';
3017-
$end_year = $params['end_year'] ?? $params[1] ?? '';
3018-
$picker = $params['picker'] ?? $params[2] ?? true;
3019-
$format_in = $params['format_in'] ?? $params[3] ?? null;
3020-
$format_out = $params['format_out'] ?? $params[4] ?? '%x';
3052+
$start_year = $params[0] ?? '';
3053+
$end_year = $params[1] ?? '';
3054+
$picker = $params[2] ?? true;
3055+
$format_in = $params[3] ?? null;
3056+
$format_out = $params[4] ?? '%x';
30213057

30223058
if (empty($start_year)) {
30233059
$start_year = date('Y');
@@ -3251,12 +3287,12 @@ class Horde_Form_Type_datetime extends Horde_Form_Type
32513287
*/
32523288
public function init(...$params)
32533289
{
3254-
$start_year = $params['start_year'] ?? $params[0] ?? '';
3255-
$end_year = $params['end_year'] ?? $params[1] ?? '';
3256-
$picker = $params['picker'] ?? $params[2] ?? true;
3257-
$format_in = $params['format_in'] ?? $params[3] ?? null;
3258-
$format_out = $params['format_out'] ?? $params[4] ?? '%x';
3259-
$show_seconds = $params['show_seconds'] ?? $params[5] ?? false;
3290+
$start_year = $params[0] ?? '';
3291+
$end_year = $params[1] ?? '';
3292+
$picker = $params[2] ?? true;
3293+
$format_in = $params[3] ?? null;
3294+
$format_out = $params[4] ?? '%x';
3295+
$show_seconds = $params[5] ?? false;
32603296

32613297
$this->_mdy = new Horde_Form_Type_monthdayyear();
32623298
$this->_mdy->init($start_year, $end_year, $picker, $format_in, $format_out);
@@ -3404,7 +3440,7 @@ public function about()
34043440
'type' => 'text'],
34053441
'format_out' => ['label' => Horde_Form_Translation::t("Display format"),
34063442
'type' => 'text'],
3407-
'seconds' => ['label' => Horde_Form_Translation::t("Show seconds?"),
3443+
'show_seconds' => ['label' => Horde_Form_Translation::t("Show seconds?"),
34083444
'type' => 'boolean']]];
34093445
}
34103446

@@ -3484,9 +3520,9 @@ class Horde_Form_Type_sorter extends Horde_Form_Type
34843520
*/
34853521
public function init(...$params)
34863522
{
3487-
$values = $params['values'] ?? $params[0];
3488-
$size = $params['size'] ?? $params[1] ?? 8;
3489-
$header = $params['header'] ?? $params[2] ?? '';
3523+
$values = $params[0];
3524+
$size = $params[1] ?? 8;
3525+
$header = $params[2] ?? '';
34903526

34913527
static $horde_sorter_instance = 0;
34923528

@@ -3936,7 +3972,7 @@ public function about()
39363972
return [
39373973
'name' => Horde_Form_Translation::t("Database lookup"),
39383974
'params' => [
3939-
'dsn' => ['label' => Horde_Form_Translation::t("DSN (see http://pear.php.net/manual/en/package.database.db.intro-dsn.php)"),
3975+
'db' => ['label' => Horde_Form_Translation::t("DSN (see http://pear.php.net/manual/en/package.database.db.intro-dsn.php)"),
39403976
'type' => 'text'],
39413977
'sql' => ['label' => Horde_Form_Translation::t("SQL statement for value lookups"),
39423978
'type' => 'text'],
@@ -4065,12 +4101,28 @@ class Horde_Form_Type_invalid extends Horde_Form_Type
40654101
*/
40664102
public function init(...$params)
40674103
{
4068-
$this->message = $params['message'] ?? $params[0] ?? '';
4104+
$this->message = $params[0] ?? '';
40694105
}
40704106

40714107
public function isValid($var, $vars, $value, $message)
40724108
{
40734109
return false;
40744110
}
40754111

4112+
/**
4113+
* Return info about field type.
4114+
*/
4115+
public function about()
4116+
{
4117+
return [
4118+
'name' => Horde_Form_Translation::t("Invalid"),
4119+
'params' => [
4120+
'message' => [
4121+
'label' => Horde_Form_Translation::t("Text"),
4122+
'type' => 'text'
4123+
]
4124+
]
4125+
];
4126+
}
4127+
40764128
}

0 commit comments

Comments
 (0)