From 9e622bbfbd41c7d1f35f0e4e89ba18777c8cad09 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 14 Feb 2020 11:11:31 +0200 Subject: [PATCH] formula: string match --- .../Functions/StringGroup/MatchAllType.php | 61 +++++++++++++++++++ .../Functions/StringGroup/MatchType.php | 61 +++++++++++++++++++ .../unit/Espo/Core/Formula/EvaluatorTest.php | 30 +++++++++ 3 files changed, 152 insertions(+) create mode 100644 application/Espo/Core/Formula/Functions/StringGroup/MatchAllType.php create mode 100644 application/Espo/Core/Formula/Functions/StringGroup/MatchType.php diff --git a/application/Espo/Core/Formula/Functions/StringGroup/MatchAllType.php b/application/Espo/Core/Formula/Functions/StringGroup/MatchAllType.php new file mode 100644 index 0000000000..95df8a0500 --- /dev/null +++ b/application/Espo/Core/Formula/Functions/StringGroup/MatchAllType.php @@ -0,0 +1,61 @@ +fetchArguments($item); + + if (count($args) < 2) throw new Error("Formula: string\\matchAll: Too few arguments."); + + $string = $args[0]; + $regexp = $args[1]; + + if (!is_string($string)) return null; + if (!is_string($regexp)) return null; + + $offset = $args[2] ?? 0; + + $matches = null; + $result = preg_match_all($regexp, $string, $matches, \PREG_PATTERN_ORDER, $offset); + + if (!$result) return null; + + $matchesResult = []; + + if (!count($matches)) return null; + + return $matches[0]; + } +} diff --git a/application/Espo/Core/Formula/Functions/StringGroup/MatchType.php b/application/Espo/Core/Formula/Functions/StringGroup/MatchType.php new file mode 100644 index 0000000000..ba9c59d3e6 --- /dev/null +++ b/application/Espo/Core/Formula/Functions/StringGroup/MatchType.php @@ -0,0 +1,61 @@ +fetchArguments($item); + + if (count($args) < 2) throw new Error("Formula: string\\match: Too few arguments."); + + $string = $args[0]; + $regexp = $args[1]; + + if (!is_string($string)) return null; + if (!is_string($regexp)) return null; + + $offset = $args[2] ?? 0; + + $matches = null; + $result = preg_match($regexp, $string, $matches, 0, $offset); + + if (!$result) return null; + + $matchesResult = []; + + if (!count($matches)) return null; + + return $matches[0]; + } +} diff --git a/tests/unit/Espo/Core/Formula/EvaluatorTest.php b/tests/unit/Espo/Core/Formula/EvaluatorTest.php index 9f463f2dbf..b308757c4f 100644 --- a/tests/unit/Espo/Core/Formula/EvaluatorTest.php +++ b/tests/unit/Espo/Core/Formula/EvaluatorTest.php @@ -122,6 +122,36 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase $this->assertEquals('0110', $actual); } + function testStringMatchAll() + { + $expression = "string\\matchAll('{token1} foo {token2} bar', '/{[^}]*}/')"; + $actual = $this->evaluator->process($expression); + $this->assertEquals(['{token1}', '{token2}'], $actual); + + $expression = "string\\matchAll('foo bar', '/{[^}]*}/')"; + $actual = $this->evaluator->process($expression); + $this->assertEquals(null, $actual); + + $expression = "string\\matchAll('{token1} foo {token2} bar', '/{[^}]*}/', 5)"; + $actual = $this->evaluator->process($expression); + $this->assertEquals(['{token2}'], $actual); + } + + function testStringMatch() + { + $expression = "string\\match('{token1} foo {token2} bar', '/{[^}]*}/')"; + $actual = $this->evaluator->process($expression); + $this->assertEquals('{token1}', $actual); + + $expression = "string\\match('foo bar', '/{[^}]*}/')"; + $actual = $this->evaluator->process($expression); + $this->assertEquals(null, $actual); + + $expression = "string\\match('{token1} foo {token2} bar', '/{[^}]*}/', 5)"; + $actual = $this->evaluator->process($expression); + $this->assertEquals('{token2}', $actual); + } + function testArrayAt() { $expression = "array\\at(list(1, 2, 4, 8, 16), 2)";