mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
json retrive support empty path
This commit is contained in:
@@ -29,26 +29,31 @@
|
||||
|
||||
namespace Espo\Core\Formula\Functions\JsonGroup;
|
||||
|
||||
use Espo\Core\Formula\{
|
||||
Functions\BaseFunction,
|
||||
ArgumentList,
|
||||
};
|
||||
use Espo\Core\Formula\ArgumentList;
|
||||
use Espo\Core\Formula\Exceptions\Error;
|
||||
use Espo\Core\Formula\Exceptions\ExecutionException;
|
||||
use Espo\Core\Formula\Exceptions\TooFewArguments;
|
||||
use Espo\Core\Formula\Functions\BaseFunction;
|
||||
|
||||
class RetrieveType extends BaseFunction
|
||||
{
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws \Espo\Core\Formula\Exceptions\TooFewArguments
|
||||
* @throws \Espo\Core\Formula\Exceptions\Error
|
||||
* @throws TooFewArguments
|
||||
* @throws Error
|
||||
* @throws ExecutionException
|
||||
*/
|
||||
public function process(ArgumentList $args)
|
||||
{
|
||||
if (count($args) < 2) {
|
||||
if (count($args) < 1) {
|
||||
$this->throwTooFewArguments();
|
||||
}
|
||||
|
||||
$jsonString = $this->evaluate($args[0]);
|
||||
$path = $this->evaluate($args[1]);
|
||||
|
||||
$path = count($args) > 1 ?
|
||||
$this->evaluate($args[1]) :
|
||||
'';
|
||||
|
||||
if (!is_string($jsonString)) {
|
||||
$this->throwBadArgumentType(1, 'string');
|
||||
@@ -58,10 +63,6 @@ class RetrieveType extends BaseFunction
|
||||
$this->throwBadArgumentType(2, 'string');
|
||||
}
|
||||
|
||||
if ($path === '') {
|
||||
$this->throwBadArgumentValue(2);
|
||||
}
|
||||
|
||||
$item = json_decode($jsonString);
|
||||
|
||||
$pathArray = $this->splitPath($path);
|
||||
@@ -75,6 +76,10 @@ class RetrieveType extends BaseFunction
|
||||
*/
|
||||
private function splitPath(string $path): array
|
||||
{
|
||||
if ($path === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
/** @var string[] $pathArray */
|
||||
$pathArray = preg_split('/(?<!\\\)\./', $path);
|
||||
|
||||
|
||||
@@ -585,6 +585,21 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertEquals('test', $result);
|
||||
}
|
||||
|
||||
public function testJsonRetrieve8()
|
||||
{
|
||||
$value = (object) [
|
||||
0 => 'test',
|
||||
];
|
||||
|
||||
$expression = "json\\retrieve(\$value)";
|
||||
|
||||
$result = $this->evaluator->process($expression, null, (object) [
|
||||
'value' => json_encode($value),
|
||||
]);
|
||||
|
||||
$this->assertEquals($value, $result);
|
||||
}
|
||||
|
||||
public function testNegate1()
|
||||
{
|
||||
$expression = "!string\contains('hello', 'test')";
|
||||
|
||||
Reference in New Issue
Block a user