Skip to content

Commit

Permalink
formula: string match
Browse files Browse the repository at this point in the history
  • Loading branch information
yurikuzn committed Feb 14, 2020
1 parent 283a251 commit 9e622bb
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
@@ -0,0 +1,61 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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\StringGroup;

use \Espo\Core\Exceptions\Error;

class MatchAllType extends \Espo\Core\Formula\Functions\Base
{
public function process(\StdClass $item)
{
$args = $this->fetchArguments($item);

if (count($args) < 2) throw new Error("Formula: string\\matchAll: Too few arguments.");

$string = $args[0];
$regexp = $args[1];

if (!is_string($string)) return null;
if (!is_string($regexp)) return null;

$offset = $args[2] ?? 0;

$matches = null;
$result = preg_match_all($regexp, $string, $matches, \PREG_PATTERN_ORDER, $offset);

if (!$result) return null;

$matchesResult = [];

if (!count($matches)) return null;

return $matches[0];
}
}
61 changes: 61 additions & 0 deletions application/Espo/Core/Formula/Functions/StringGroup/MatchType.php
@@ -0,0 +1,61 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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\StringGroup;

use \Espo\Core\Exceptions\Error;

class MatchType extends \Espo\Core\Formula\Functions\Base
{
public function process(\StdClass $item)
{
$args = $this->fetchArguments($item);

if (count($args) < 2) throw new Error("Formula: string\\match: Too few arguments.");

$string = $args[0];
$regexp = $args[1];

if (!is_string($string)) return null;
if (!is_string($regexp)) return null;

$offset = $args[2] ?? 0;

$matches = null;
$result = preg_match($regexp, $string, $matches, 0, $offset);

if (!$result) return null;

$matchesResult = [];

if (!count($matches)) return null;

return $matches[0];
}
}
30 changes: 30 additions & 0 deletions tests/unit/Espo/Core/Formula/EvaluatorTest.php
Expand Up @@ -122,6 +122,36 @@ function testStringPad()
$this->assertEquals('0110', $actual);
}

function testStringMatchAll()
{
$expression = "string\\matchAll('{token1} foo {token2} bar', '/{[^}]*}/')";
$actual = $this->evaluator->process($expression);
$this->assertEquals(['{token1}', '{token2}'], $actual);

$expression = "string\\matchAll('foo bar', '/{[^}]*}/')";
$actual = $this->evaluator->process($expression);
$this->assertEquals(null, $actual);

$expression = "string\\matchAll('{token1} foo {token2} bar', '/{[^}]*}/', 5)";
$actual = $this->evaluator->process($expression);
$this->assertEquals(['{token2}'], $actual);
}

function testStringMatch()
{
$expression = "string\\match('{token1} foo {token2} bar', '/{[^}]*}/')";
$actual = $this->evaluator->process($expression);
$this->assertEquals('{token1}', $actual);

$expression = "string\\match('foo bar', '/{[^}]*}/')";
$actual = $this->evaluator->process($expression);
$this->assertEquals(null, $actual);

$expression = "string\\match('{token1} foo {token2} bar', '/{[^}]*}/', 5)";
$actual = $this->evaluator->process($expression);
$this->assertEquals('{token2}', $actual);
}

function testArrayAt()
{
$expression = "array\\at(list(1, 2, 4, 8, 16), 2)";
Expand Down

0 comments on commit 9e622bb

Please sign in to comment.