number power function

This commit is contained in:
Yuri Kuznetsov
2024-03-27 10:11:03 +02:00
parent c7d1fc7c35
commit 07b68d9b7d
3 changed files with 86 additions and 0 deletions
@@ -0,0 +1,61 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2024 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\NumberGroup;
use Espo\Core\Formula\EvaluatedArgumentList;
use Espo\Core\Formula\Exceptions\BadArgumentType;
use Espo\Core\Formula\Exceptions\TooFewArguments;
use Espo\Core\Formula\Func;
/**
* @noinspection PhpUnused
*/
class PowerType implements Func
{
public function process(EvaluatedArgumentList $arguments): float|int
{
if (count($arguments) < 2) {
throw TooFewArguments::create(2);
}
$value = $arguments[0];
$exp = $arguments[1];
if (!is_int($value) && !is_float($value)) {
throw BadArgumentType::create(1, 'int|float');
}
if (!is_int($exp) && !is_float($exp)) {
throw BadArgumentType::create(2, 'int|float');
}
return pow($value, $exp);
}
}
@@ -182,6 +182,11 @@
"name": "number\\abs",
"insertText": "number\\abs(VALUE)"
},
{
"name": "number\\power",
"insertText": "number\\power(VALUE, EXP)",
"returnType": "int|float"
},
{
"name": "number\\round",
"insertText": "number\\round(VALUE, PRECISION)",
@@ -867,6 +867,26 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase
);
}
public function testNumberPower1(): void
{
$expression = "number\\power(3, 2)";
$this->assertSame(
9,
$this->evaluator->process($expression)
);
}
public function testNumberPower2(): void
{
$expression = "number\\power(3.0, 2.0)";
$this->assertSame(
9.0,
$this->evaluator->process($expression)
);
}
public function testNullCoalescing1(): void
{
$expression = "null ?? 1";