From 6a7b185861f6d5aaf4a05f14c0afe9e1a28d9327 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 28 Sep 2021 14:32:39 -0700 Subject: [PATCH] (REF) CRM_Utils_Array - Add pathSync() helper --- CRM/Utils/Array.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/CRM/Utils/Array.php b/CRM/Utils/Array.php index bbc931c38f..f507f7978b 100644 --- a/CRM/Utils/Array.php +++ b/CRM/Utils/Array.php @@ -1167,6 +1167,38 @@ class CRM_Utils_Array { } } + /** + * Attempt to synchronize or fill aliased items. + * + * If $canonPath is set, copy to $altPath; or... + * If $altPath is set, copy to $canonPath. + * + * @param array $params + * Data-tree + * @param string[] $canonPath + * Preferred path + * @param string[] $altPath + * Old/alternate/deprecated path. + * @param callable|null $filter + * Optional function to filter the value as it passes through (canonPath=>altPath or altPath=>canonPath). + * function(mixed $v, bool $isCanon): mixed + */ + public static function pathSync(&$params, $canonPath, $altPath, ?callable $filter = NULL) { + $MISSING = new \stdClass(); + + $v = static::pathGet($params, $canonPath, $MISSING); + if ($v !== $MISSING) { + static::pathSet($params, $altPath, $filter ? $filter($v, TRUE) : $v); + return; + } + + $v = static::pathGet($params, $altPath, $MISSING); + if ($v !== $MISSING) { + static::pathSet($params, $canonPath, $filter ? $filter($v, FALSE) : $v); + return; + } + } + /** * Convert a simple dictionary into separate key+value records. * -- 2.25.1