diff --git a/macro.inc.php b/macro.inc.php index 3e718f4..413137b 100644 --- a/macro.inc.php +++ b/macro.inc.php @@ -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; +} +