EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Formula function string replaceMany
Yuriic2f5701c226c
diff --git a/application/Espo/Core/Formula/Functions/StringGroup/ReplaceManyType.php b/application/Espo/Core/Formula/Functions/StringGroup/ReplaceManyType.phpnew file mode 100644index 0000000000..252d053cc2--- /dev/null+++ b/application/Espo/Core/Formula/Functions/StringGroup/ReplaceManyType.php@@ -0,0 +1,72 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Core\Formula\Functions\StringGroup;++use Espo\Core\Formula\EvaluatedArgumentList;+use Espo\Core\Formula\Exceptions\BadArgumentType;+use Espo\Core\Formula\Exceptions\BadArgumentValue;+use Espo\Core\Formula\Exceptions\TooFewArguments;+use Espo\Core\Formula\Func;++/**+ * @noinspection PhpUnused+ */+class ReplaceManyType implements Func+{+ public function process(EvaluatedArgumentList $arguments): string+ {+ if (count($arguments) < 2) {+ throw TooFewArguments::create(2);+ }++ $input = $arguments[0];+ $map = $arguments[1];++ if (!is_string($input)) {+ throw BadArgumentType::create(1, 'string');+ }++ if (!is_object($map)) {+ throw BadArgumentType::create(2, 'object');+ }++ $pairs = [];++ foreach (get_object_vars($map) as $k => $v) {+ if (!is_scalar($v) && $v !== null) {+ throw BadArgumentValue::create(2, "Replacement strings must be scalar.");+ }++ $pairs[$k] = (string) $v;+ }++ return strtr($input, $pairs);+ }+}diff --git a/application/Espo/Resources/metadata/app/formula.json b/application/Espo/Resources/metadata/app/formula.jsonindex 1e9c3b8ce0..14aaf82de3 100644--- a/application/Espo/Resources/metadata/app/formula.json+++ b/application/Espo/Resources/metadata/app/formula.json@@ -83,6 +83,11 @@"insertText": "string\\replace(STRING, SEARCH, REPLACE)","returnType": "string"},+ {+ "name": "string\\replaceMany",+ "insertText": "string\\replace(STRING, MAP)",+ "returnType": "string"+ },{"name": "string\\split","insertText": "string\\split(STRING, SEPARATOR)",diff --git a/tests/unit/Espo/Core/Formula/EvaluatorTest.php b/tests/unit/Espo/Core/Formula/EvaluatorTest.phpindex b834ff1e20..a235be30b5 100644--- a/tests/unit/Espo/Core/Formula/EvaluatorTest.php+++ b/tests/unit/Espo/Core/Formula/EvaluatorTest.php@@ -34,6 +34,7 @@ use Espo\Core\Binding\BindingContainerBuilder;use Espo\Core\FieldProcessing\SpecificFieldLoader;use Espo\Core\Formula\Evaluator;use Espo\Core\Formula\Exceptions\Error;+use Espo\Core\Formula\Exceptions\GeneralError;use Espo\Core\Formula\Exceptions\UndefinedKey;use Espo\Core\Formula\Exceptions\UnsafeFunction;use Espo\Core\Formula\Utils\EntityUtil;@@ -362,6 +363,53 @@ class EvaluatorTest extends TestCase$this->assertEquals('hello hello hello', $actual);}+ public function testStringReplaceMany(): void+ {+ $expression = <<<EOF+ \$map = object\\create();+ \$map['{one}'] = 'one';+ \$map['{two}'] = 'two';+ \$map['3'] = 'three';+ \$map['0'] = 'zero';+ \$map['true'] = 'TRUE';+ \$map['false'] = 'FALSE';++ \$input = "Test {one} {two} 3 0 true false.";++ string\\replaceMany(\$input, \$map);+ EOF;++ $actual = $this->evaluator->process($expression);++ $this->assertEquals('Test one two three zero TRUE FALSE.', $actual);+ }++ public function testStringReplaceManyError1(): void+ {+ $expression = <<<EOF+ \$input = "Test {one} {two}.";++ string\\replaceMany("Test");+ EOF;++ $this->expectException(GeneralError::class);++ $this->evaluator->process($expression);+ }++ public function testStringReplaceManyError2(): void+ {+ $expression = <<<EOF+ \$input = "Test {one} {two}.";++ string\\replaceMany("Test", list());+ EOF;++ $this->expectException(GeneralError::class);++ $this->evaluator->process($expression);+ }+public function testArrayAt(){$expression = "array\\at(list(1, 2, 4, 8, 16), 2)";