Adding initial version of OVERRIDE(..) macro

This commit is contained in:
Pavel Shevaev 2023-06-26 15:09:45 +03:00
parent d079c79d50
commit 24b2a0ee45
1 changed files with 36 additions and 0 deletions

View File

@ -171,3 +171,39 @@ function macro_UNWRAP($jsm, $content)
$content = rtrim($content, '}');
return $content;
}
function _array_merge_recursive_distinct(array $array1, array $array2)
{
$merged = $array1;
foreach($array2 as $key => $value)
{
if(is_array($value) && isset($merged[$key]) && is_array($merged[$key]))
$merged[$key] = _array_merge_recursive_distinct($merged[$key], $value);
else
$merged[$key] = $value;
}
return $merged;
}
/**
* @global @raw_args
*/
function macro_OVERRIDE($jsm, $content)
{
$items = \jzon_parse($content);
if(!is_array($items))
throw new Exception("OVERRIDE contents not an array");
if(sizeof($items) != 2)
throw new Exception("OVERRIDE array size is not 2, it's " . sizeof($items));
$merged = _array_merge_recursive_distinct($items[0], $items[1]);
$json = json_encode($merged, JSON_UNESCAPED_LINE_TERMINATORS | JSON_UNESCAPED_SLASHES);
$json = ltrim($json, '{');
$json = rtrim($json, '}');
return $json;
}