diff --git a/application/Espo/Core/Formula/Functions/NumberGroup/PowerType.php b/application/Espo/Core/Formula/Functions/NumberGroup/PowerType.php new file mode 100644 index 0000000000..0f0f8b730e --- /dev/null +++ b/application/Espo/Core/Formula/Functions/NumberGroup/PowerType.php @@ -0,0 +1,61 @@ +. + * + * 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); + } +} diff --git a/application/Espo/Resources/metadata/app/formula.json b/application/Espo/Resources/metadata/app/formula.json index c09d2542ff..bd76f94b74 100644 --- a/application/Espo/Resources/metadata/app/formula.json +++ b/application/Espo/Resources/metadata/app/formula.json @@ -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)", diff --git a/tests/unit/Espo/Core/Formula/EvaluatorTest.php b/tests/unit/Espo/Core/Formula/EvaluatorTest.php index 0458a9ad74..66bce1e46d 100644 --- a/tests/unit/Espo/Core/Formula/EvaluatorTest.php +++ b/tests/unit/Espo/Core/Formula/EvaluatorTest.php @@ -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";