formula functions change

This commit is contained in:
Yuri Kuznetsov
2020-07-15 12:39:59 +03:00
parent 42d894fd5a
commit f4b2d9b1df
15 changed files with 167 additions and 80 deletions

View File

@@ -41,6 +41,8 @@ use Espo\Core\Formula\{
Exceptions\NotPassedEntity,
};
use Espo\Core\Utils\Log;
use StdClass;
abstract class BaseFunction
@@ -53,6 +55,8 @@ abstract class BaseFunction
protected $name;
protected $log;
protected function getVariables() : StdClass
{
return $this->variables;
@@ -66,12 +70,14 @@ abstract class BaseFunction
return $this->entity;
}
public function __construct(string $name, Processor $processor, ?Entity $entity = null, ?StdClass $variables = null)
{
public function __construct(
string $name, Processor $processor, ?Entity $entity = null, ?StdClass $variables = null, ?Log $log = null
) {
$this->name = $name;
$this->processor = $processor;
$this->entity = $entity;
$this->variables = $variables;
$this->log = $log;
}
/**
@@ -97,12 +103,22 @@ abstract class BaseFunction
throw new TooFewArguments('function: ' . $this->name);
}
protected function throwBadArgumentType(?int $index = null)
protected function throwBadArgumentType(?int $index = null, ?string $type = null)
{
$msg = 'function: ' . $this->name;
if ($index !== null) {
$msg .= ', index: ' . $index;
if ($type) {
$msg .= ', should be: ' . $type;
}
}
throw new BadArgumentType($msg);
}
protected function logBadArgumentType(int $index, string $type)
{
if (!$this->log) return;
$this->log->warning("Formula function: {$this->name}, argument {$index} should be '{$type}'.");
}
}

View File

@@ -29,13 +29,18 @@
namespace Espo\Core\Formula\Functions\StringGroup;
class ConcatenationType extends \Espo\Core\Formula\Functions\Base
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
class ConcatenationType extends BaseFunction
{
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
$result = '';
foreach ($item->value as $subItem) {
foreach ($args as $subItem) {
$part = $this->evaluate($subItem);
if (!is_string($part)) {

View File

@@ -29,17 +29,20 @@
namespace Espo\Core\Formula\Functions\StringGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
class LengthType extends \Espo\Core\Formula\Functions\Base
class LengthType extends BaseFunction
{
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
if (count($item->value) < 1) {
throw new Error();
if (count($args) < 1) {
$this->throwTooFewArguments();
}
$value = $this->evaluate($item->value[0]);
$value = $this->evaluate($args[0]);
if (!is_string($value)) {
$value = strval($value);

View File

@@ -29,17 +29,20 @@
namespace Espo\Core\Formula\Functions\StringGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
class LowerCaseType extends \Espo\Core\Formula\Functions\Base
class LowerCaseType extends BaseFunction
{
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
if (count($item->value) < 1) {
throw new Error();
if (count($args) < 1) {
$this->throwTooFewArguments();
}
$value = $this->evaluate($item->value[0]);
$value = $this->evaluate($args[0]);
if (!is_string($value)) {
$value = strval($value);

View File

@@ -29,15 +29,20 @@
namespace Espo\Core\Formula\Functions\StringGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
class MatchAllType extends \Espo\Core\Formula\Functions\Base
class MatchAllType extends BaseFunction
{
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
$args = $this->fetchArguments($item);
$args = $this->evaluate($args);
if (count($args) < 2) throw new Error("string\\matchAll: Too few arguments.");
if (count($args) < 2) {
$this->throwTooFewArguments();
}
$string = $args[0];
$regexp = $args[1];

View File

@@ -29,15 +29,20 @@
namespace Espo\Core\Formula\Functions\StringGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
class MatchType extends \Espo\Core\Formula\Functions\Base
class MatchType extends BaseFunction
{
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
$args = $this->fetchArguments($item);
$args = $this->evaluate($args);
if (count($args) < 2) throw new Error("string\\match: Too few arguments.");
if (count($args) < 2) {
$this->throwTooFewArguments();
}
$string = $args[0];
$regexp = $args[1];

View File

@@ -29,23 +29,33 @@
namespace Espo\Core\Formula\Functions\StringGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
class PadType extends \Espo\Core\Formula\Functions\Base
class PadType extends BaseFunction
{
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
$args = $this->fetchArguments($item);
$args = $this->evaluate($args);
if (count($args) < 2) throw new Error("string\\pad: should have at least 2 arguments.");
if (count($args) < 2) {
$this->throwTooFewArguments();
}
$input = $args[0];
$length = $args[1];
$string = $args[2] ?? ' ';
$type = $args[3] ?? 'right';
if (!is_string($input)) $input = strval($input);
if (!is_int($length)) throw new Error("string\\pad: second argument should be integer.");
if (!is_string($input)) {
$input = strval($input);
}
if (!is_int($length)) {
$this->throwBadArgumentType(2);
}
$map = [
'right' => \STR_PAD_RIGHT,

View File

@@ -29,18 +29,23 @@
namespace Espo\Core\Formula\Functions\StringGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
class PosType extends \Espo\Core\Formula\Functions\Base
class PosType extends BaseFunction
{
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
$args = $item->value ?? [];
$args = $this->evaluate($args);
if (count($args) < 2) throw new Error("Bad arguments passed to function string\\pos.");
if (count($args) < 2) {
$this->throwTooFewArguments();
}
$string = $this->evaluate($args[0]);
$needle = $this->evaluate($args[1]);
$string = $args[0];
$needle = $args[1];
if (!is_string($string)) {
return false;

View File

@@ -29,36 +29,40 @@
namespace Espo\Core\Formula\Functions\StringGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
class ReplaceType extends \Espo\Core\Formula\Functions\Base
class ReplaceType extends BaseFunction
{
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
$args = $this->fetchArguments($item);
$args = $this->evaluate($args);
if (count($args) < 3) throw new Error("string\\replace: Too few arguments.");
if (count($args) < 3) {
$this->throwTooFewArguments();
};
$string = $args[0];
$search = $args[1];
$replace = $args[2];
if (!is_string($string)) {
$GLOBALS['log']->warning("string\\replace: 1st argument should be string.");
$this->logBadArgumentType(1, 'string');
return '';
}
if (!is_string($search)) {
$GLOBALS['log']->warning("string\\replace: 2nd argument should be string.");
$this->logBadArgumentType(2, 'string');
return $string;
}
if (!is_string($replace)) {
$GLOBALS['log']->warning("string\\replace: 3rd argument should be string.");
$this->logBadArgumentType(3, 'string');
return $string;
}
return str_replace($search, $replace, $string);
}
}

View File

@@ -29,21 +29,26 @@
namespace Espo\Core\Formula\Functions\StringGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
class SubstringType extends \Espo\Core\Formula\Functions\Base
class SubstringType extends BaseFunction
{
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
if (count($item->value) < 2) {
throw new Error("string\\substring: too few arguments.");
$args = $this->evaluate($args);
if (count($args) < 2) {
$this->throwTooFewArguments();
}
$string = $this->evaluate($item->value[0]);
$start = $this->evaluate($item->value[1]);
$string = $args[0];
$start = $args[1];
if (count($item->value) > 2) {
$length = $this->evaluate($item->value[2]);
if (count($args) > 2) {
$length = $args[2];
return mb_substr($string, $start, $length);
} else {
return mb_substr($string, $start);

View File

@@ -29,18 +29,23 @@
namespace Espo\Core\Formula\Functions\StringGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
class TestType extends \Espo\Core\Formula\Functions\Base
class TestType extends BaseFunction
{
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
if (count($item->value) < 2) {
throw new Error('string\\test: too few arguments.');
$args = $this->evaluate($args);
if (count($args) < 2) {
$this->throwTooFewArguments();
}
$string = $this->evaluate($item->value[0]);
$regexp = $this->evaluate($item->value[1]);
$string = $args[0];
$regexp = $args[1];
if (!is_string($string)) {
return false;

View File

@@ -29,17 +29,22 @@
namespace Espo\Core\Formula\Functions\StringGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
class TrimType extends \Espo\Core\Formula\Functions\Base
class TrimType extends BaseFunction
{
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
if (count($item->value) < 1) {
throw new Error();
$args = $this->evaluate($args);
if (count($args) < 1) {
$this->throwTooFewArguments();
}
$value = $this->evaluate($item->value[0]);
$value = $args[0];
if (!is_string($value)) {
$value = strval($value);

View File

@@ -29,18 +29,22 @@
namespace Espo\Core\Formula\Functions\StringGroup;
use Espo\Core\Exceptions\Error;
use Espo\Core\Formula\{
Functions\BaseFunction,
ArgumentList,
};
class UpperCaseType extends \Espo\Core\Formula\Functions\Base
class UpperCaseType extends BaseFunction
{
public function process(\StdClass $item)
public function process(ArgumentList $args)
{
$args = $this->evaluate($args);
if (count($item->value) < 1) {
throw new Error();
if (count($args) < 1) {
$this->throwTooFewArguments();
}
$value = $this->evaluate($item->value[0]);
$value = $args[0];
if (!is_string($value)) {
$value = strval($value);

View File

@@ -38,6 +38,15 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase
$container = $this->container =
$this->getMockBuilder('\\Espo\\Core\\Container')->disableOriginalConstructor()->getMock();
$this->log = $this->getMockBuilder('Espo\\Core\\Utils\\Log')->disableOriginalConstructor()->getMock();
$container
->expects($this->any())
->method('get')
->will($this->returnValueMap([
['log', $this->log],
]));
$injectableFactory = $injectableFactory = new \Espo\Core\InjectableFactory($container);
$this->evaluator = new \Espo\Core\Formula\Evaluator($injectableFactory);

View File

@@ -63,6 +63,8 @@ class FormulaTest extends \PHPUnit\Framework\TestCase
$this->user = $this->getMockBuilder('Espo\\Entities\\User')->disableOriginalConstructor()->getMock();
$this->log = $this->getMockBuilder('Espo\\Core\\Utils\\Log')->disableOriginalConstructor()->getMock();
$this->user->id = '1';
$this->user
@@ -80,7 +82,8 @@ class FormulaTest extends \PHPUnit\Framework\TestCase
['dateTime', $this->dateTime],
['number', $this->number],
['config', $this->config],
['user', $this->user]
['user', $this->user],
['log', $this->log],
]));
}