From: Tim Otten Date: Tue, 28 Sep 2021 21:32:39 +0000 (-0700) Subject: (REF) CRM_Utils_Array - Add pathSync() helper X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=6a7b185861f6d5aaf4a05f14c0afe9e1a28d9327;p=civicrm-core.git (REF) CRM_Utils_Array - Add pathSync() helper --- 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. *