From 110e2fbc3734116a5f7124a8bb941645f8fbdb46 Mon Sep 17 00:00:00 2001 From: yuri Date: Mon, 14 Oct 2019 12:32:30 +0300 Subject: [PATCH] string pos formula function --- .../Formula/Functions/StringGroup/PosType.php | 51 +++++++++++++++++++ tests/unit/Espo/Core/Formula/FormulaTest.php | 41 +++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 application/Espo/Core/Formula/Functions/StringGroup/PosType.php diff --git a/application/Espo/Core/Formula/Functions/StringGroup/PosType.php b/application/Espo/Core/Formula/Functions/StringGroup/PosType.php new file mode 100644 index 0000000000..9784596ec3 --- /dev/null +++ b/application/Espo/Core/Formula/Functions/StringGroup/PosType.php @@ -0,0 +1,51 @@ +value ?? []; + + if (count($args) < 2) throw new Error("Bad arguments passed to function string\\pos."); + + $string = $this->evaluate($args[0]); + $needle = $this->evaluate($args[1]); + + if (!is_string($string)) { + return false; + } + + return mb_strpos($string, $needle); + } +} \ No newline at end of file diff --git a/tests/unit/Espo/Core/Formula/FormulaTest.php b/tests/unit/Espo/Core/Formula/FormulaTest.php index be454fca6f..ff1fa4cf6c 100644 --- a/tests/unit/Espo/Core/Formula/FormulaTest.php +++ b/tests/unit/Espo/Core/Formula/FormulaTest.php @@ -2816,6 +2816,47 @@ class FormulaTest extends \PHPUnit\Framework\TestCase $this->assertEquals('12', $actual); } + function testPps() + { + $item = json_decode(' + { + "type": "string\\\\pos", + "value": [ + { + "type": "value", + "value": "1234" + }, + { + "type": "value", + "value": 23 + } + ] + } + '); + + $actual = $this->formula->process($item, $this->entity); + $this->assertEquals(1, $actual); + + $item = json_decode(' + { + "type": "string\\\\pos", + "value": [ + { + "type": "value", + "value": "1234" + }, + { + "type": "value", + "value": 54 + } + ] + } + '); + + $actual = $this->formula->process($item, $this->entity); + $this->assertFalse($actual); + } + function testBundle() { $item = json_decode('