formula: array append

This commit is contained in:
Yuri Kuznetsov
2025-03-27 11:13:07 +02:00
parent a56ccbd1f8
commit b2156f36b9
3 changed files with 122 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* 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;
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Formula\Exceptions\Error;
/**
* @noinspection PhpUnused
*/
class ArrayAppendType extends BaseFunction
{
public function process(ArgumentList $args)
{
if (count($args) < 2) {
$this->throwTooFewArguments();
}
$name = $this->evaluate($args[0]);
if (!is_string($name)) {
$this->throwBadArgumentValue(1, 'string');
}
$value = $this->evaluate($args[1]);
if (!property_exists($this->getVariables(), $name)) {
throw new Error("Cannot array-append to not existing variable.");
}
$array =& $this->getVariables()->$name;
if (!is_array($array)) {
throw new Error("Cannot array-append to non-array variable.");
}
$array[] = $value;
}
}

View File

@@ -108,10 +108,25 @@ class Parser
if ($firstPart[0] == '$') {
$variable = substr($firstPart, 1);
$isArrayAppend = false;
if (str_ends_with($firstPart, '[]')) {
$variable = substr($firstPart, 1, -2);
$isArrayAppend = true;
}
if ($variable === '' || !preg_match($this->variableNameRegExp, $variable)) {
throw new SyntaxError("Bad variable name `$variable`.");
}
if ($isArrayAppend) {
return new Node('arrayAppend', [
new Value($variable),
$this->split($secondPart)
]);
}
return new Node('assign', [
new Value($variable),
$this->split($secondPart)

View File

@@ -1690,4 +1690,45 @@ class EvaluatorTest extends TestCase
$this->assertEquals(null, $result);
}
public function testArrayAppend1(): void
{
$expression = "
\$test = list();
\$test[] = 'a';
array\\at(\$test, 0);
";
/** @noinspection PhpUnhandledExceptionInspection */
$result = $this->evaluator->process($expression);
$this->assertEquals('a', $result);
}
public function testArrayAppendError1(): void
{
$expression = "
\$test[] = 'a';
array\\at(\$test, 0);
";
$this->expectException(Error::class);
/** @noinspection PhpUnhandledExceptionInspection */
$this->evaluator->process($expression);
}
public function testArrayAppendError2(): void
{
$expression = "
\$test = '';
\$test[] = 'a';
array\\at(\$test, 0);
";
$this->expectException(Error::class);
/** @noinspection PhpUnhandledExceptionInspection */
$this->evaluator->process($expression);
}
}