formula array includes

This commit is contained in:
yuri
2017-04-28 13:36:11 +03:00
parent c3d44e2472
commit 65be8c385f
2 changed files with 110 additions and 0 deletions
@@ -0,0 +1,53 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2017 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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\ArrayGroup;
use \Espo\Core\Exceptions\Error;
class IncludesType extends \Espo\Core\Formula\Functions\Base
{
public function process(\StdClass $item)
{
if (!property_exists($item, 'value') || !is_array($item->value)) {
throw new Error('Value for \'Array\\Includses\' item is not array.');
}
if (count($item->value) < 2) {
throw new Error('Bad arguments passed to \'Array\\Includses\'.');
}
$list = $this->evaluate($item->value[0]);
$needle = $this->evaluate($item->value[1]);
if (!is_array($list)) {
return false;
}
return in_array($needle, $list);
}
}
@@ -1364,6 +1364,63 @@ class FormulaTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('2017-01-01 19:30:00', $actual);
}
function testArrayIncludes()
{
$item = json_decode('
{
"type": "array\\\\includes",
"value": [
{
"type": "value",
"value": ["Test", "Hello"]
},
{
"type": "value",
"value": "Test"
}
]
}
');
$actual = $this->formula->process($item, $this->entity);
$this->assertTrue($actual);
$item = json_decode('
{
"type": "array\\\\includes",
"value": [
{
"type": "value",
"value": false
},
{
"type": "value",
"value": ""
}
]
}
');
$actual = $this->formula->process($item, $this->entity);
$this->assertFalse($actual);
$item = json_decode('
{
"type": "array\\\\includes",
"value": [
{
"type": "value",
"value": ["Test", "Hello"]
},
{
"type": "value",
"value": "Yok"
}
]
}
');
$actual = $this->formula->process($item, $this->entity);
$this->assertFalse($actual);
}
function testEnvUserAttribute()
{
$item = json_decode('