From 8de3ec16b6859b259285698253ac31f5830ccd47 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 3 Mar 2023 01:15:27 -0800 Subject: [PATCH] (REF) TokenProcessor - Extract class StandardFilters --- Civi/Token/StandardFilters.php | 62 ++++++++++++++++++++++++++++++++++ Civi/Token/TokenProcessor.php | 51 ++++++++-------------------- 2 files changed, 76 insertions(+), 37 deletions(-) create mode 100644 Civi/Token/StandardFilters.php diff --git a/Civi/Token/StandardFilters.php b/Civi/Token/StandardFilters.php new file mode 100644 index 0000000000..69f1b86783 --- /dev/null +++ b/Civi/Token/StandardFilters.php @@ -0,0 +1,62 @@ +format('Y-m-d H:i:s'), $filter[1] ?? NULL); + } + if ($value === '') { + return $value; + } + + // I don't think this makes sense, but it matches the pre-refactor + // behavior (where the old `switch()` block would fall-through to `case "default"`. + return static::default($value, $filter, $format); + } + + public static function default($value, array $filter, string $format) { + if (!$value) { + return $filter[1]; + } + else { + return $value; + } + } + +} diff --git a/Civi/Token/TokenProcessor.php b/Civi/Token/TokenProcessor.php index 57761c7fdf..2472f03b4e 100644 --- a/Civi/Token/TokenProcessor.php +++ b/Civi/Token/TokenProcessor.php @@ -377,10 +377,10 @@ class TokenProcessor { $useSmarty = !empty($row->context['smarty']); $tokens = $this->rowValues[$row->tokenRow][$message['format']]; - $getToken = function(?string $fullToken, ?string $entity, ?string $field, ?array $modifier) use ($tokens, $useSmarty, $row) { + $getToken = function(?string $fullToken, ?string $entity, ?string $field, ?array $modifier) use ($tokens, $useSmarty, $row, $message) { if (isset($tokens[$entity][$field])) { $v = $tokens[$entity][$field]; - $v = $this->filterTokenValue($v, $modifier, $row); + $v = $this->filterTokenValue($v, $modifier, $row, $message['format']); if ($useSmarty) { $v = \CRM_Utils_Token::tokenEscapeSmarty($v); } @@ -456,10 +456,12 @@ class TokenProcessor { * @param array|null $filter * @param TokenRow $row * The current target/row. + * @param string $messageFormat + * Ex: 'text/plain' or 'text/html' * @return string * @throws \CRM_Core_Exception */ - private function filterTokenValue($value, ?array $filter, TokenRow $row) { + private function filterTokenValue($value, ?array $filter, TokenRow $row, string $messageFormat) { // KISS demonstration. This should change... e.g. provide a filter-registry or reuse Smarty's registry... if ($value instanceof \DateTime && $filter === NULL) { @@ -470,6 +472,7 @@ class TokenProcessor { } } + // TODO: Move this to StandardFilters if ($value instanceof Money) { switch ($filter[0] ?? NULL) { case NULL: @@ -484,40 +487,14 @@ class TokenProcessor { } } - switch ($filter[0] ?? NULL) { - case NULL: - return $value; - - case 'upper': - return mb_strtoupper($value); - - case 'lower': - return mb_strtolower($value); - - case 'boolean': - // Cast to 0 or 1 for use in text. - return (int) ((bool) $value); - - case 'crmDate': - if ($value instanceof \DateTime) { - // @todo cludgey. - require_once 'CRM/Core/Smarty/plugins/modifier.crmDate.php'; - return \smarty_modifier_crmDate($value->format('Y-m-d H:i:s'), $filter[1] ?? NULL); - } - if ($value === '') { - return $value; - } - - case 'default': - if (!$value) { - return $filter[1]; - } - else { - return $value; - } - - default: - throw new \CRM_Core_Exception('Invalid token filter: ' . json_encode($filter, JSON_UNESCAPED_SLASHES)); + if (!isset($filter[0])) { + return $value; + } + elseif (is_callable([StandardFilters::class, $filter[0]])) { + return call_user_func([StandardFilters::class, $filter[0]], $value, $filter, $messageFormat); + } + else { + throw new \CRM_Core_Exception('Invalid token filter: ' . json_encode($filter, JSON_UNESCAPED_SLASHES)); } } -- 2.25.1