Add apply_array_filters for apply filters for array #1

Merged
ps merged 2 commits from a.prusov/metagen_php:master into master 2023-07-12 11:16:20 +03:00
1 changed files with 13 additions and 2 deletions

View File

@ -103,12 +103,12 @@ function default_value($default)
return str_replace('$', '\$', $default);
}
function apply_value_filters($field_name, array $tokens, $val, $add_assoc_check = true)
function apply_filters($token_prefix, $field_name, array $tokens, $val, $add_assoc_check = true)
{
$str = $val;
foreach($tokens as $token => $args_json)
{
if(strpos($token, 'flt_') === false)
if(strpos($token, $token_prefix) === false)
continue;
$filter_func = '\metagen_php\\' . $token;
@ -136,6 +136,16 @@ function apply_value_filters($field_name, array $tokens, $val, $add_assoc_check
return $str;
}
function apply_array_filters($field_name, array $tokens, $val, $add_assoc_check = true)
{
return apply_filters("fltarr_", $field_name, $tokens, $val, $add_assoc_check);
}
function apply_value_filters($field_name, array $tokens, $val, $add_assoc_check = true)
{
return apply_filters("flt_", $field_name, $tokens, $val, $add_assoc_check);
}
//TODO: move it to template
function data2value($name, \mtgType $type, $buf, $prefix = '', $tokens = array(), $as_is = false, $postfix = '', $indent = '')
{
@ -201,6 +211,7 @@ function data2value($name, \mtgType $type, $buf, $prefix = '', $tokens = array()
else if($type instanceof \mtgArrType)
{
//TODO: maybe filters should be applied to the whole array as well?
$str .= $cond_indent."{$tmp_val} = " . apply_array_filters($name, $tokens, "{$tmp_val}"). ";\n";
$str .= $cond_indent."\$tmp_arr__ = \metagen_php\\val_arr({$tmp_val});\n";
$str .= $cond_indent."foreach(\$tmp_arr__ as \$tmp_arr_item__)\n";
$str .= $cond_indent."{\n";