From 24b2a0ee457c34b6d5b903053d4cbc165d5b7281 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Mon, 26 Jun 2023 15:09:45 +0300 Subject: [PATCH] Adding initial version of OVERRIDE(..) macro --- macro.inc.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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; +} +