object functions

This commit is contained in:
Yuri Kuznetsov
2022-02-11 13:44:56 +02:00
parent 3fed1a5ae8
commit 0e5ff9f64a
8 changed files with 489 additions and 0 deletions
@@ -0,0 +1,49 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://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 General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Formula\Functions\JsonGroup;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Utils\Json;
class EncodeType extends BaseFunction
{
public function process(ArgumentList $args)
{
if (count($args) < 1) {
$this->throwTooFewArguments();
}
$value = $this->evaluate($args[0]);
return Json::encode($value);
}
}
@@ -0,0 +1,55 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://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 General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Formula\Functions\ObjectGroup;
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Core\Utils\ObjectUtil;
use stdClass;
class cloneDeepType extends BaseFunction
{
public function process(ArgumentList $args)
{
if (count($args) < 1) {
$this->throwTooFewArguments(1);
}
$obj = $this->evaluate($args[0]);
if (!$obj instanceof stdClass) {
$this->throwBadArgumentType(1, 'object');
}
return ObjectUtil::clone($obj);
}
}
@@ -0,0 +1,41 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://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 General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Formula\Functions\ObjectGroup;
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Formula\Functions\BaseFunction;
class CreateType extends BaseFunction
{
public function process(ArgumentList $args)
{
return (object) [];
}
}
@@ -0,0 +1,58 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://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 General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Formula\Functions\ObjectGroup;
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Formula\Functions\BaseFunction;
use stdClass;
class GetType extends BaseFunction
{
public function process(ArgumentList $args)
{
if (count($args) < 2) {
$this->throwTooFewArguments(2);
}
$obj = $this->evaluate($args[0]);
$key = $this->evaluate($args[1]);
if (!$obj instanceof stdClass) {
$this->throwBadArgumentType(1, 'object');
}
if ($key === null) {
$this->throwBadArgumentType(2);
}
return $obj->$key ?? null;
}
}
@@ -0,0 +1,58 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://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 General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Formula\Functions\ObjectGroup;
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Formula\Functions\BaseFunction;
use stdClass;
class HasType extends BaseFunction
{
public function process(ArgumentList $args)
{
if (count($args) < 2) {
$this->throwTooFewArguments(2);
}
$obj = $this->evaluate($args[0]);
$key = $this->evaluate($args[1]);
if (!$obj instanceof stdClass) {
$this->throwBadArgumentType(1, 'object');
}
if ($key === null) {
$this->throwBadArgumentType(2);
}
return property_exists($obj, $key);
}
}
@@ -0,0 +1,65 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://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 General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Formula\Functions\ObjectGroup;
use Espo\Core\Formula\ArgumentList;
use Espo\Core\Formula\Functions\BaseFunction;
use Espo\Core\Utils\ObjectUtil;
use stdClass;
class SetType extends BaseFunction
{
public function process(ArgumentList $args)
{
if (count($args) < 3) {
$this->throwTooFewArguments(3);
}
$obj = $this->evaluate($args[0]);
$key = $this->evaluate($args[1]);
$value = $this->evaluate($args[2]);
if (!$obj instanceof stdClass) {
$this->throwBadArgumentType(1, 'object');
}
if ($key === null) {
$this->throwBadArgumentType(2);
}
$clonedObj = ObjectUtil::clone($obj);
$clonedObj->$key = $value;
return $clonedObj;
}
}
@@ -339,6 +339,31 @@
"insertText": "util\\generateId()",
"returnType": "string"
},
{
"name": "object\\create",
"insertText": "object\\create()",
"returnType": "object"
},
{
"name": "object\\get",
"insertText": "object\\get(OBJECT, KEY)",
"returnType": "mixed"
},
{
"name": "object\\has",
"insertText": "object\\has(OBJECT, KEY)",
"returnType": "bool"
},
{
"name": "object\\set",
"insertText": "object\\set(OBJECT, KEY, VALUE)",
"returnType": "object"
},
{
"name": "object\\cloneDeep",
"insertText": "object\\cloneDeep(OBJECT)",
"returnType": "object"
},
{
"name": "password\\generate",
"insertText": "password\\generate()",
@@ -388,6 +413,11 @@
"insertText": "json\\retrieve(JSON, PATH)",
"returnType": "mixed"
},
{
"name": "json\\encode",
"insertText": "json\\encode(VALUE)",
"returnType": "string"
},
{
"name": "ext\\email\\send",
"insertText": "ext\\email\\send(EMAIL_ID)",
@@ -764,4 +764,137 @@ class EvaluatorTest extends \PHPUnit\Framework\TestCase
$this->evaluator->process($expression, null)
);
}
public function testObjectCreate(): void
{
$expression = "object\\create()";
$this->assertEquals(
(object) [],
$this->evaluator->process($expression, null)
);
}
public function testObjectSet(): void
{
$expression = "
\$o1 = object\\create();
\$o2 = object\\set(\$o1, 'key', 'value');
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertNotEquals(
$vars->o1,
$vars->o2
);
$this->assertEquals(
'value',
$vars->o2->key
);
}
public function testObjectGet1(): void
{
$expression = "
\$o = object\\create();
\$o = object\\set(\$o, 'key', 'value');
\$v = object\\get(\$o, 'key');
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals(
'value',
$vars->o->key
);
}
public function testObjectGet2(): void
{
$expression = "
\$o = object\\create();
\$v = object\\get(\$o, 'key');
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals(
null,
$vars->v
);
}
public function testObjectHas1(): void
{
$expression = "
object\\has(
object\\set(object\\create(), 'key', 'value'),
'key'
)
";
$this->assertEquals(
true,
$this->evaluator->process($expression, null)
);
}
public function testObjectHas2(): void
{
$expression = "
object\\has(
object\\create(),
'key'
)
";
$this->assertEquals(
false,
$this->evaluator->process($expression, null)
);
}
public function testObjectClone(): void
{
$expression = "
\$o1 = object\\create();
\$o2 = object\\cloneDeep(\$o1);
";
$vars = (object) [];
$this->evaluator->process($expression, null, $vars);
$this->assertEquals(
$vars->o1,
$vars->o2
);
$this->assertNotSame(
$vars->o1,
$vars->o2
);
}
public function testJsonEncode(): void
{
$expression = "
json\\encode(
object\\set(object\\create(), 'key', 'value')
)
";
$this->assertEquals(
"{\"key\":\"value\"}",
$this->evaluator->process($expression, null)
);
}
}