From 0c44eec377bacccaf7644dde23d5b031e094113b Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 13 Feb 2020 15:39:32 +0200 Subject: [PATCH] formula array\\at --- .../Formula/Functions/ArrayGroup/AtType.php | 54 +++++++++++++++++++ .../unit/Espo/Core/Formula/EvaluatorTest.php | 7 +++ 2 files changed, 61 insertions(+) create mode 100644 application/Espo/Core/Formula/Functions/ArrayGroup/AtType.php diff --git a/application/Espo/Core/Formula/Functions/ArrayGroup/AtType.php b/application/Espo/Core/Formula/Functions/ArrayGroup/AtType.php new file mode 100644 index 0000000000..9f578303cd --- /dev/null +++ b/application/Espo/Core/Formula/Functions/ArrayGroup/AtType.php @@ -0,0 +1,54 @@ +fetchArguments($item); + if (count($args) < 2) throw new Error("Formula: array\\at: Not enough arguments."); + + $array = $args[0]; + $index = $args[1]; + + if (!is_array($array)) throw new Error("Formula: array\\at: First argument must be array."); + if (!is_int($index)) throw new Error("Formula: array\\at: Second argument must be integer."); + + if (!array_key_exists($index, $array)) { + $GLOBALS['log']->notics("Formula: array\\at: Index doesn't exist."); + return null; + } + + return $array[$index]; + } +} diff --git a/tests/unit/Espo/Core/Formula/EvaluatorTest.php b/tests/unit/Espo/Core/Formula/EvaluatorTest.php index bcc01fafcd..9f463f2dbf 100644 --- a/tests/unit/Espo/Core/Formula/EvaluatorTest.php +++ b/tests/unit/Espo/Core/Formula/EvaluatorTest.php @@ -121,4 +121,11 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase $actual = $this->evaluator->process($expression); $this->assertEquals('0110', $actual); } + + function testArrayAt() + { + $expression = "array\\at(list(1, 2, 4, 8, 16), 2)"; + $actual = $this->evaluator->process($expression); + $this->assertEquals(4, $actual); + } }