sanitize date and date time, parse ATOM format

This commit is contained in:
Yuri Kuznetsov
2024-03-21 11:35:47 +02:00
parent 00b4569f8e
commit ed3a7d64c6
7 changed files with 305 additions and 0 deletions
@@ -0,0 +1,70 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2024 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Classes\FieldSanitizers;
use DateTimeImmutable;
use DateTimeInterface;
use Espo\Core\Field\Date as DateValue;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Exception;
/**
* @noinspection PhpUnused
*/
class Date implements Sanitizer
{
public function sanitize(Data $data, string $field): void
{
$value = $data->get($field);
if ($value === null) {
return;
}
try {
DateValue::fromString($value);
return;
}
catch (Exception) {}
$dateTime = DateTimeImmutable::createFromFormat(DateTimeInterface::ATOM, $value);
if ($dateTime === false) {
return;
}
$value = $dateTime->format(DateTimeUtil::SYSTEM_DATE_FORMAT);
$data->set($field, $value);
}
}
@@ -0,0 +1,73 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2024 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Classes\FieldSanitizers;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use Espo\Core\Field\DateTime as DateTimeValue;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Exception;
/**
* @noinspection PhpUnused
*/
class Datetime implements Sanitizer
{
public function sanitize(Data $data, string $field): void
{
$value = $data->get($field);
if ($value === null) {
return;
}
try {
DateTimeValue::fromString($value);
return;
}
catch (Exception) {}
$dateTime = DateTimeImmutable::createFromFormat(DateTimeInterface::ATOM, $value);
if ($dateTime === false) {
return;
}
$value = $dateTime
->setTimezone(new DateTimeZone('UTC'))
->format(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT);
$data->set($field, $value);
}
}
@@ -0,0 +1,72 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2024 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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 Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Classes\FieldSanitizers;
use DateTimeImmutable;
use DateTimeInterface;
use Espo\Core\Field\Date;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Exception;
/**
* @noinspection PhpUnused
*/
class DatetimeOptionalDate implements Sanitizer
{
public function sanitize(Data $data, string $field): void
{
$attribute = $field . 'Date';
$value = $data->get($attribute);
if ($value === null) {
return;
}
try {
Date::fromString($value);
return;
}
catch (Exception) {}
$dateTime = DateTimeImmutable::createFromFormat(DateTimeInterface::ATOM, $value);
if ($dateTime === false) {
return;
}
$value = $dateTime->format(DateTimeUtil::SYSTEM_DATE_FORMAT);
$data->set($attribute, $value);
}
}
@@ -80,5 +80,8 @@
"personalData": true,
"valueFactoryClassName": "Espo\\Core\\Field\\Date\\DateFactory",
"attributeExtractorClassName": "Espo\\Core\\Field\\Date\\DateAttributeExtractor",
"sanitizerClassNameList": [
"Espo\\Classes\\FieldSanitizers\\Date"
],
"default": null
}
@@ -94,5 +94,8 @@
"personalData": true,
"valueFactoryClassName": "Espo\\Core\\Field\\DateTime\\DateTimeFactory",
"attributeExtractorClassName": "Espo\\Core\\Field\\DateTime\\DateTimeAttributeExtractor",
"sanitizerClassNameList": [
"Espo\\Classes\\FieldSanitizers\\Datetime"
],
"default": null
}
@@ -102,5 +102,9 @@
"personalData": true,
"valueFactoryClassName": "Espo\\Core\\Field\\DateTimeOptional\\DateTimeOptionalFactory",
"attributeExtractorClassName": "Espo\\Core\\Field\\DateTimeOptional\\DateTimeOptionalAttributeExtractor",
"sanitizerClassNameList": [
"Espo\\Classes\\FieldSanitizers\\Datetime",
"Espo\\Classes\\FieldSanitizers\\DatetimeOptionalDate"
],
"default": null
}
@@ -31,7 +31,11 @@ namespace tests\integration\Espo\Record;
use Espo\Core\Record\CreateParams;
use Espo\Core\Record\ServiceContainer;
use Espo\Entities\User;
use Espo\Modules\Crm\Entities\Account;
use Espo\Modules\Crm\Entities\Meeting;
use Espo\Modules\Crm\Entities\Opportunity;
use Espo\Modules\Crm\Entities\Task;
use Espo\Tools\FieldManager\FieldManager;
use tests\integration\Core\BaseTestCase;
@@ -39,6 +43,8 @@ class SanitizeTest extends BaseTestCase
{
public function testSanitize(): void
{
// phone
/** @noinspection PhpUnhandledExceptionInspection */
$this->getInjectableFactory()
->create(FieldManager::class)
@@ -101,5 +107,79 @@ class SanitizeTest extends BaseTestCase
$this->assertEquals('+380904443322', $numbers[0]);
$this->assertEquals('+380904443333', $numbers[1]);
// datetime
/** @noinspection PhpUnhandledExceptionInspection */
$meeting = $this->getContainer()
->getByClass(ServiceContainer::class)
->getByClass(Meeting::class)
->create((object) [
'name' => 'Test',
'dateStart' => '2030-12-10 10:11:12',
'dateEnd' => '2030-12-10T10:11:12-01:00',
'assignedUserId' => $this->getContainer()->getByClass(User::class)->getId(),
], CreateParams::create());
$this->assertEquals('2030-12-10 10:11:12', $meeting->get('dateStart'));
$this->assertEquals('2030-12-10 11:11:12', $meeting->get('dateEnd'));
// datetimeOptional
/** @noinspection PhpUnhandledExceptionInspection */
$task = $this->getContainer()
->getByClass(ServiceContainer::class)
->getByClass(Task::class)
->create((object) [
'name' => 'Test',
'dateStartDate' => '2030-12-10T10:11:12-01:00',
'dateEnd' => '2030-12-10T10:11:12-01:00',
'assignedUserId' => $this->getContainer()->getByClass(User::class)->getId(),
], CreateParams::create());
$this->assertEquals('2030-12-10', $task->get('dateStartDate'));
$this->assertEquals('2030-12-10 11:11:12', $task->get('dateEnd'));
/** @noinspection PhpUnhandledExceptionInspection */
$task = $this->getContainer()
->getByClass(ServiceContainer::class)
->getByClass(Task::class)
->create((object) [
'name' => 'Test',
'dateStartDate' => '2030-12-10',
'assignedUserId' => $this->getContainer()->getByClass(User::class)->getId(),
], CreateParams::create());
$this->assertEquals('2030-12-10', $task->get('dateStartDate'));
// date
/** @noinspection PhpUnhandledExceptionInspection */
$meeting = $this->getContainer()
->getByClass(ServiceContainer::class)
->getByClass(Opportunity::class)
->create((object) [
'name' => 'Test',
'closeDate' => '2030-12-10T10:11:12-01:00',
'assignedUserId' => $this->getContainer()->getByClass(User::class)->getId(),
'probability' => 10,
'amount' => 1.0,
], CreateParams::create());
$this->assertEquals('2030-12-10', $meeting->get('closeDate'));
/** @noinspection PhpUnhandledExceptionInspection */
$meeting = $this->getContainer()
->getByClass(ServiceContainer::class)
->getByClass(Opportunity::class)
->create((object) [
'name' => 'Test',
'closeDate' => '2030-12-10',
'assignedUserId' => $this->getContainer()->getByClass(User::class)->getId(),
'probability' => 10,
'amount' => 1.0,
], CreateParams::create());
$this->assertEquals('2030-12-10', $meeting->get('closeDate'));
}
}