formula: minute, hour

This commit is contained in:
yuri
2017-05-05 13:45:37 +03:00
parent 299c3c13fc
commit 8ae0696f07
3 changed files with 196 additions and 0 deletions
@@ -0,0 +1,67 @@
<?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\DateTimeGroup;
use \Espo\Core\Exceptions\Error;
class HourType extends \Espo\Core\Formula\Functions\Base
{
protected function init()
{
$this->addDependency('dateTime');
}
public function process(\StdClass $item)
{
if (!property_exists($item, 'value')) {
return true;
}
if (!is_array($item->value)) {
throw new Error();
}
if (count($item->value) < 1) {
throw new Error();
}
$value = $this->evaluate($item->value[0]);
if (empty($value)) return -1;
if (strlen($value) > 11) {
$resultString = $this->getInjection('dateTime')->convertSystemDateTime($value, null, 'H');
} else {
return 0;
}
return intval($resultString);
}
}
@@ -0,0 +1,67 @@
<?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\DateTimeGroup;
use \Espo\Core\Exceptions\Error;
class MinuteType extends \Espo\Core\Formula\Functions\Base
{
protected function init()
{
$this->addDependency('dateTime');
}
public function process(\StdClass $item)
{
if (!property_exists($item, 'value')) {
return true;
}
if (!is_array($item->value)) {
throw new Error();
}
if (count($item->value) < 1) {
throw new Error();
}
$value = $this->evaluate($item->value[0]);
if (empty($value)) return -1;
if (strlen($value) > 11) {
$resultString = $this->getInjection('dateTime')->convertSystemDateTime($value, null, 'm');
} else {
return 0;
}
return intval($resultString);
}
}
@@ -1251,6 +1251,68 @@ class FormulaTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(20, $actual);
}
function testDatetimeHour()
{
$item = json_decode('
{
"type": "datetime\\\\hour",
"value": [
{
"type": "value",
"value": "2017-10-02 14:15"
}
]
}
');
$actual = $this->formula->process($item);
$this->assertEquals(14, $actual);
$item = json_decode('
{
"type": "datetime\\\\hour",
"value": [
{
"type": "value",
"value": "2017-10-20"
}
]
}
');
$actual = $this->formula->process($item);
$this->assertEquals(0, $actual);
}
function testDatetimeMinute()
{
$item = json_decode('
{
"type": "datetime\\\\minute",
"value": [
{
"type": "value",
"value": "2017-10-02 14:05"
}
]
}
');
$actual = $this->formula->process($item);
$this->assertEquals(5, $actual);
$item = json_decode('
{
"type": "datetime\\\\hour",
"value": [
{
"type": "value",
"value": "2017-10-20"
}
]
}
');
$actual = $this->formula->process($item);
$this->assertEquals(0, $actual);
}
function testDatetimeDayOfWeek()
{
$item = json_decode('