Commit fcfcea49 authored by Côme Chilliet's avatar Côme Chilliet
Browse files

:ambulance: fix(template) Fix modifier result handling

This avoids calling array_merge on what could be a generator.
It also changes a bit the logic behind modifier system

issue #5882
Showing with 14 additions and 21 deletions
+14 -21
...@@ -128,11 +128,11 @@ class templateHandling ...@@ -128,11 +128,11 @@ class templateHandling
$modifiers = $m[1]; $modifiers = $m[1];
$mask = substr($mask, strlen($m[0])); $mask = substr($mask, strlen($m[0]));
} }
$result = array(''); $result = array();
if (isset($attrs[$mask])) { if (isset($attrs[$mask])) {
$result = array($attrs[$mask]); $result = $attrs[$mask];
if (is_array($result[0])) { if (is_array($result)) {
unset($result[0]['count']); unset($result['count']);
} }
} elseif (($mask != '') && !preg_match('/c/', $modifiers)) { } elseif (($mask != '') && !preg_match('/c/', $modifiers)) {
trigger_error("'$mask' was not found in attributes"); trigger_error("'$mask' was not found in attributes");
...@@ -146,19 +146,8 @@ class templateHandling ...@@ -146,19 +146,8 @@ class templateHandling
$args = explode(',', $m[1]); $args = explode(',', $m[1]);
$i += strlen($m[1]) + 2; $i += strlen($m[1]) + 2;
} }
$result_tmp = array(); $result = static::applyModifier($modifier, $args, $result);
foreach ($result as $r) {
$result_tmp = array_merge($result_tmp, static::applyModifier($modifier, $args, $r));
}
$result = $result_tmp;
}
foreach ($result as &$r) {
/* Array that were not converted by a modifier into a string are now converted to strings */
if (is_array($r)) {
$r = reset($r);
}
} }
unset($r);
return $result; return $result;
} }
...@@ -469,7 +458,11 @@ class templateHandling ...@@ -469,7 +458,11 @@ class templateHandling
mb_regex_encoding('UTF-8'); mb_regex_encoding('UTF-8');
if (is_array($str) && (!is_numeric($m)) && (strtolower($m) == $m)) { if (is_array($str) && (!is_numeric($m)) && (strtolower($m) == $m)) {
/* $str is an array and $m is lowercase, so it's a string modifier */ /* $str is an array and $m is lowercase, so it's a string modifier */
$str = reset($str); if (count($str) == 0) {
$str = '';
} else {
$str = reset($str);
}
} }
switch ($m) { switch ($m) {
case 'F': case 'F':
...@@ -498,21 +491,21 @@ class templateHandling ...@@ -498,21 +491,21 @@ class templateHandling
trigger_error('Missing "M" match modifier parameter'); trigger_error('Missing "M" match modifier parameter');
$args[] = '/.*/'; $args[] = '/.*/';
} }
$result = array(array_filter( $result = array_filter(
$str, $str,
function ($s) use ($args) function ($s) use ($args)
{ {
return preg_match($args[0], $s); return preg_match($args[0], $s);
} }
)); );
break; break;
case '4': case '4':
// IPv4 // IPv4
$result = array(array_filter($str, 'tests::is_ipv4')); $result = array_filter($str, 'tests::is_ipv4');
break; break;
case '6': case '6':
// IPv6 // IPv6
$result = array(array_filter($str, 'tests::is_ipv6')); $result = array_filter($str, 'tests::is_ipv6');
break; break;
case 'c': case 'c':
// comment // comment
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment