Skip to content

EspoCRM – Open Source CRM Application

dynamic logic tool

Yuri Kuznetsov66ae50b1298a
diff --git a/application/Espo/Core/Utils/DateTime/SystemClock.php b/application/Espo/Core/Utils/DateTime/SystemClock.php
new file mode 100644
index 0000000000..b8c38a6af5
--- /dev/null
+++ b/application/Espo/Core/Utils/DateTime/SystemClock.php
@@ -0,0 +1,45 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 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\Core\Utils\DateTime;
+
+use Espo\Core\Field\DateTime;
+use Psr\Clock\ClockInterface;
+use DateTimeImmutable;
+
+/**
+ * @since 9.1.0
+ */
+class SystemClock implements ClockInterface
+{
+ public function now(): DateTimeImmutable
+ {
+ return DateTime::createNow()->toDateTime();
+ }
+}
diff --git a/application/Espo/Tools/DynamicLogic/ConditionChecker.php b/application/Espo/Tools/DynamicLogic/ConditionChecker.php
new file mode 100644
index 0000000000..e4c4a7d282
--- /dev/null
+++ b/application/Espo/Tools/DynamicLogic/ConditionChecker.php
@@ -0,0 +1,326 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 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\Tools\DynamicLogic;
+
+use Espo\Core\Field\Date;
+use Espo\Core\Field\DateTime;
+use Espo\Core\Utils\DateTime\SystemClock;
+use Espo\Entities\User;
+use Espo\ORM\Entity;
+use Espo\Tools\DynamicLogic\ConditionChecker\Options;
+use Espo\Tools\DynamicLogic\Exceptions\BadCondition;
+
+use Psr\Clock\ClockInterface;
+
+use Exception;
+use RuntimeException;
+use DateTimeImmutable;
+use DateTimeZone;
+
+class ConditionChecker
+{
+ public function __construct(
+ private Entity $entity,
+ private ?User $user = null,
+ private Options $options = new Options(),
+ private ClockInterface $clock = new SystemClock(),
+ ) {}
+
+ /**
+ * @throws BadCondition
+ */
+ public function check(Item $item): bool
+ {
+ $type = $item->type;
+ $value = $item->value;
+
+ if ($type === Type::And) {
+ if (!is_array($value)) {
+ throw new BadCondition();
+ }
+
+ foreach ($value as $subItem) {
+ if (!$subItem instanceof Item) {
+ throw new BadCondition();
+ }
+
+ if (!$this->check($subItem)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ if ($type === Type::Or) {
+ if (!is_array($value)) {
+ throw new BadCondition();
+ }
+
+ foreach ($value as $subItem) {
+ if (!$subItem instanceof Item) {
+ throw new BadCondition();
+ }
+
+ if ($this->check($subItem)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ if ($type === Type::Not) {
+ if (!$value instanceof Item) {
+ throw new BadCondition();
+ }
+
+ return !$this->check($value);
+ }
+
+ if (!$item->attribute) {
+ throw new BadCondition("No attribute.");
+ }
+
+ $setValue = $this->getAttributeValue($item->attribute);
+
+ if ($type === Type::Equals) {
+ return $setValue === $value;
+ }
+
+ if ($type === Type::NotEquals) {
+ return $setValue !== $value;
+ }
+
+ if ($type === Type::IsEmpty) {
+ return $setValue === [] ||
+ $setValue === null ||
+ $setValue === false ||
+ $setValue === '';
+ }
+
+ if ($type === Type::IsNotEmpty) {
+ return !(
+ $setValue === [] ||
+ $setValue === null ||
+ $setValue === false ||
+ $setValue === ''
+ );
+ }
+
+ if ($type === Type::IsTrue) {
+ return (bool) $setValue;
+ }
+
+ if ($type === Type::IsFalse) {
+ return !$setValue;
+ }
+
+ if ($type === Type::Contains) {
+ if (is_string($setValue) && is_string($value)) {
+ return str_contains($setValue, $value);
+ }
+
+ if (is_array($setValue)) {
+ return in_array($value, $setValue);
+ }
+
+ return false;
+ }
+
+ if ($type === Type::NotContains) {
+ if (is_string($setValue) && is_string($value)) {
+ return !str_contains($setValue, $value);
+ }
+
+ if (is_array($setValue)) {
+ return !in_array($value, $setValue);
+ }
+
+ return true;
+ }
+
+ if ($type === Type::StartsWith) {
+ if (is_string($setValue) && is_string($value)) {
+ return str_starts_with($setValue, $value);
+ }
+
+ return false;
+ }
+
+ if ($type === Type::EndsWith) {
+ if (is_string($setValue) && is_string($value)) {
+ return str_ends_with($setValue, $value);
+ }
+
+ return false;
+ }
+
+ if ($type === Type::Matches) {
+ if (is_string($setValue) && is_string($value)) {
+ return (bool) preg_match($value, $setValue);
+ }
+
+ return false;
+ }
+
+ if ($type === Type::GreaterThan) {
+ return $setValue > $value;
+ }
+
+ if ($type === Type::LessThan) {
+ return $setValue < $value;
+ }
+
+ if ($type === Type::GreaterThanOrEquals) {
+ return $setValue >= $value;
+ }
+
+ if ($type === Type::LessThanOrEquals) {
+ return $setValue <= $value;
+ }
+
+ if ($type === Type::In) {
+ if (is_array($setValue)) {
+ return in_array($value, $setValue);
+ }
+
+ return false;
+ }
+
+ if ($type === Type::NotIn) {
+ if (is_array($setValue)) {
+ return !in_array($value, $setValue);
+ }
+
+ return true;
+ }
+
+ if (!$setValue || !is_string($setValue)) {
+ return false;
+ }
+
+ if ($type === Type::IsToday) {
+ if (strlen($setValue) > 10) {
+ $setDateTime = DateTime::fromDateTime($this->createDateTime($setValue));
+
+ $todayStart = $this->createNow()
+ ->withTimezone($this->getTimeZone())
+ ->withTime(0, 0);
+
+ $todayEnd = $todayStart->addDays(1);
+
+ return $todayStart->isLessThanOrEqualTo($setDateTime) && $setDateTime->isLessThan($todayEnd);
+ }
+
+ $setDate = Date::fromDateTime($this->createDateTime($setValue));
+ $today = $this->createToday();
+
+ return $setDate->isEqualTo($today);
+ }
+
+ if ($type === Type::InFuture) {
+ if (strlen($setValue) > 10) {
+ $setDateTime = DateTime::fromDateTime($this->createDateTime($setValue));
+
+ return $setDateTime->isGreaterThan($this->createNow());
+ }
+
+ $setDate = Date::fromDateTime($this->createDateTime($setValue));
+ $today = $this->createToday();
+
+ return $setDate->isGreaterThan($today);
+ }
+
+ if ($type === Type::InPast) {
+ if (strlen($setValue) > 10) {
+ $setDateTime = DateTime::fromDateTime($this->createDateTime($setValue));
+
+ return $setDateTime->isLessThan($this->createNow());
+ }
+
+ $setDate = Date::fromDateTime($this->createDateTime($setValue));
+ $today = $this->createToday();
+
+ return $setDate->isLessThan($today);
+ }
+
+ throw new BadCondition("Unimplemented type '$type->value'.");
+ }
+
+ private function getAttributeValue(string $attribute): mixed
+ {
+ if (str_starts_with($attribute, '$')) {
+ if (!$this->user) {
+ return null;
+ }
+
+ if ($attribute === '$user.id') {
+ return $this->user->getId();
+ }
+
+ if ($attribute === '$user.teamsIds') {
+ return $this->user->getTeamIdList();
+ }
+ }
+
+ return $this->entity->get($attribute);
+ }
+
+ private function getTimeZone(): DateTimeZone
+ {
+ return $this->options->timeZone;
+ }
+
+ private function createDateTime(string $value): DateTimeImmutable
+ {
+ try {
+ $setDateTime = new DateTimeImmutable($value, $this->getTimeZone());
+ } catch (Exception $e) {
+ throw new RuntimeException($e->getMessage(), 0, $e);
+ }
+
+ return $setDateTime;
+ }
+
+ private function createNow(): DateTime
+ {
+ return DateTime::fromDateTime($this->clock->now());
+ }
+
+ private function createToday(): Date
+ {
+ $dateTime = $this->clock
+ ->now()
+ ->setTimezone($this->getTimeZone());
+
+ return Date::fromDateTime($dateTime);
+ }
+}
diff --git a/application/Espo/Tools/DynamicLogic/ConditionChecker/Options.php b/application/Espo/Tools/DynamicLogic/ConditionChecker/Options.php
new file mode 100644
index 0000000000..f5cf992481
--- /dev/null
+++ b/application/Espo/Tools/DynamicLogic/ConditionChecker/Options.php
@@ -0,0 +1,39 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 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\Tools\DynamicLogic\ConditionChecker;
+
+use DateTimeZone;
+
+readonly class Options
+{
+ public function __construct(
+ public DateTimeZone $timeZone = new DateTimeZone('UTC'),
+ ) {}
+}
diff --git a/application/Espo/Tools/DynamicLogic/Exceptions/BadCondition.php b/application/Espo/Tools/DynamicLogic/Exceptions/BadCondition.php
new file mode 100644
index 0000000000..709845381b
--- /dev/null
+++ b/application/Espo/Tools/DynamicLogic/Exceptions/BadCondition.php
@@ -0,0 +1,35 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 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\Tools\DynamicLogic\Exceptions;
+
+use Exception;
+
+class BadCondition extends Exception
+{}
diff --git a/application/Espo/Tools/DynamicLogic/Item.php b/application/Espo/Tools/DynamicLogic/Item.php
new file mode 100644
index 0000000000..71e49f197d
--- /dev/null
+++ b/application/Espo/Tools/DynamicLogic/Item.php
@@ -0,0 +1,106 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 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\Tools\DynamicLogic;
+
+use Espo\Tools\DynamicLogic\Exceptions\BadCondition;
+use stdClass;
+
+readonly class Item
+{
+ public function __construct(
+ public Type $type,
+ public mixed $value,
+ public ?string $attribute = null,
+ ) {}
+
+ /**
+ * @param stdClass[] $rawItems
+ * @throws BadCondition
+ */
+ public static function fromGroupDefinition(array $rawItems): Item
+ {
+ return new Item(
+ type: Type::And,
+ value: array_map(fn ($it) => self::fromItemDefinition($it), $rawItems),
+ );
+ }
+
+ /**
+ * @throws BadCondition
+ */
+ public static function fromItemDefinition(stdClass $rawItem): Item
+ {
+ $type = $rawItem->type ?? null;
+ $attribute = $rawItem->attribute ?? null;
+ $value = $rawItem->value ?? null;
+
+ if (!$type || !is_string($type)) {
+ throw new BadCondition("No type.");
+ }
+
+ if ($type === Type::And->value || $type === Type::Or->value) {
+ if (!is_array($value)) {
+ throw new BadCondition("Non-array value.");
+ }
+
+ foreach ($value as $it) {
+ if (!$it instanceof stdClass) {
+ throw new BadCondition("Bad group item value.");
+ }
+ }
+
+ return new Item(
+ type: Type::from($type),
+ value: array_map(fn ($it) => self::fromItemDefinition($it), $value),
+ );
+ }
+
+ if ($type === Type::Not->value) {
+ if (!$value instanceof stdClass) {
+ throw new BadCondition("Bad not item value.");
+ }
+
+ return new Item(
+ type: Type::from($type),
+ value: self::fromItemDefinition($value),
+ );
+ }
+
+ if ($attribute !== null && !is_string($attribute)) {
+ throw new BadCondition("No attribute.");
+ }
+
+ return new Item(
+ type: Type::from($type),
+ value: $value,
+ attribute: $attribute,
+ );
+ }
+}
diff --git a/application/Espo/Tools/DynamicLogic/Type.php b/application/Espo/Tools/DynamicLogic/Type.php
new file mode 100644
index 0000000000..90e7cc0aac
--- /dev/null
+++ b/application/Espo/Tools/DynamicLogic/Type.php
@@ -0,0 +1,59 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 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\Tools\DynamicLogic;
+
+enum Type: string
+{
+ case And = 'and';
+ case Or = 'or';
+ case Not = 'not';
+ case Equals = 'equals';
+ case NotEquals = 'notEquals';
+ case IsEmpty = 'isEmpty';
+ case IsNotEmpty = 'isNotEmpty';
+ case IsTrue = 'isTrue';
+ case IsFalse = 'isFalse';
+ case Contains = 'contains';
+ case Has = 'has';
+ case NotContains = 'notContains';
+ case NotHas = 'notHas';
+ case StartsWith = 'startsWith';
+ case EndsWith = 'endsWith';
+ case Matches = 'matches';
+ case GreaterThan = 'greaterThan';
+ case LessThan = 'lessThan';
+ case GreaterThanOrEquals = 'greaterThanOrEquals';
+ case LessThanOrEquals = 'lessThanOrEquals';
+ case In = 'in';
+ case NotIn = 'notIn';
+ case IsToday = 'isToday';
+ case InFuture = 'inFuture';
+ case InPast = 'inPast';
+}
diff --git a/composer.json b/composer.json
index ce609a1a0f..6296ace48a 100644
--- a/composer.json
+++ b/composer.json
@@ -54,7 +54,8 @@
"tholu/php-cidr-match": "^0.4",
"league/oauth2-client": "^2.8",
"symfony/mailer": "^6",
- "league/html-to-markdown": "^5.1"
+ "league/html-to-markdown": "^5.1",
+ "psr/clock": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
diff --git a/composer.lock b/composer.lock
index 1106ef61bb..327a2a2d61 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
- "content-hash": "0c7231d8b867aa042b9360424cf0702a",
+ "content-hash": "fdcc2fe81ecada0b857711ff3899dcef",
"packages": [
{
"name": "async-aws/core",
diff --git a/tests/unit/Espo/Tools/DynamicLogic/CheckerTest.php b/tests/unit/Espo/Tools/DynamicLogic/CheckerTest.php
new file mode 100644
index 0000000000..54cbac32e0
--- /dev/null
+++ b/tests/unit/Espo/Tools/DynamicLogic/CheckerTest.php
@@ -0,0 +1,1169 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 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 tests\unit\Espo\Tools\DynamicLogic;
+
+use DateTimeImmutable;
+use DateTimeZone;
+use Espo\ORM\Entity;
+use Espo\Tools\DynamicLogic\ConditionChecker;
+use Espo\Tools\DynamicLogic\Item;
+use PHPUnit\Framework\TestCase;
+use Psr\Clock\ClockInterface;
+
+class CheckerTest extends TestCase
+{
+ /**
+ * @param array<string, mixed> $map
+ */
+ private function initEntity(array $map): Entity
+ {
+ $entity = $this->createMock(Entity::class);
+
+ $entity
+ ->expects($this->any())
+ ->method('get')
+ ->willReturnCallback(fn ($attribute) => $map[$attribute] ?? null);
+
+ return $entity;
+ }
+
+ private function createClock(): ClockInterface
+ {
+ return new class() implements ClockInterface {
+
+ public function now(): DateTimeImmutable
+ {
+ return new DateTimeImmutable('2025-01-01 10:00:00', new DateTimeZone('UTC'));
+ }
+ };
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testEquals(): void
+ {
+ $map = [
+ 'k' => 'v',
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $item = Item::fromItemDefinition((object) [
+ 'type' => 'equals',
+ 'attribute' => 'k',
+ 'value' => 'v',
+ ]);
+
+ $this->assertTrue($checker->check($item));
+
+ $item = Item::fromItemDefinition((object) [
+ 'type' => 'equals',
+ 'attribute' => 'k',
+ 'value' => 'v1',
+ ]);
+
+ $this->assertFalse($checker->check($item));
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testNotEquals(): void
+ {
+ $map = [
+ 'k' => 'v',
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $item = Item::fromItemDefinition((object) [
+ 'type' => 'notEquals',
+ 'attribute' => 'k',
+ 'value' => 'v',
+ ]);
+
+ $this->assertFalse($checker->check($item));
+
+ $item = Item::fromItemDefinition((object) [
+ 'type' => 'notEquals',
+ 'attribute' => 'k',
+ 'value' => 'v1',
+ ]);
+
+ $this->assertTrue($checker->check($item));
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testAnd(): void
+ {
+ $map = [
+ 'k1' => 'v1',
+ 'k2' => 'v2',
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $item = Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'equals',
+ 'attribute' => 'k1',
+ 'value' => 'v1',
+ ],
+ (object) [
+ 'type' => 'equals',
+ 'attribute' => 'k2',
+ 'value' => 'v2',
+ ]
+ ]);
+
+ $this->assertTrue($checker->check($item));
+
+ $item = Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'equals',
+ 'attribute' => 'k1',
+ 'value' => 'v1',
+ ],
+ (object) [
+ 'type' => 'equals',
+ 'attribute' => 'k2',
+ 'value' => 'v2-modified',
+ ]
+ ]);
+
+ $this->assertFalse($checker->check($item));
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testOr(): void
+ {
+ $map = [
+ 'k1' => 'v1',
+ 'k2' => 'v2',
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $item = Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'or',
+ 'value' => [
+ (object) [
+ 'type' => 'equals',
+ 'attribute' => 'k1',
+ 'value' => 'v1-m',
+ ],
+ (object) [
+ 'type' => 'equals',
+ 'attribute' => 'k2',
+ 'value' => 'v2',
+ ],
+ ],
+ ]
+ ]);
+
+ $this->assertTrue($checker->check($item));
+
+ $item = Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'or',
+ 'value' => [
+ (object) [
+ 'type' => 'equals',
+ 'attribute' => 'k1',
+ 'value' => 'v1-m',
+ ],
+ (object) [
+ 'type' => 'equals',
+ 'attribute' => 'k2',
+ 'value' => 'v2-m',
+ ],
+ ],
+ ]
+ ]);
+
+ $this->assertFalse($checker->check($item));
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testNot(): void
+ {
+ $map = [
+ 'k1' => 'v1',
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $item = Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'not',
+ 'value' => (object) [
+ 'type' => 'equals',
+ 'attribute' => 'k1',
+ 'value' => 'v1',
+ ],
+ ]
+ ]);
+
+ $this->assertFalse($checker->check($item));
+
+ $item = Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'not',
+ 'value' => (object) [
+ 'type' => 'equals',
+ 'attribute' => 'k1',
+ 'value' => 'v1-m',
+ ],
+ ]
+ ]);
+
+ $this->assertTrue($checker->check($item));
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testIsEmpty(): void
+ {
+ $map = [
+ 'k1' => 'v1',
+ 'k' => '',
+ 'array' => [],
+ 'null' => null,
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isEmpty',
+ 'attribute' => 'k',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isEmpty',
+ 'attribute' => 'array',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isEmpty',
+ 'attribute' => 'null',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isEmpty',
+ 'attribute' => 'nonExistent',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isEmpty',
+ 'attribute' => 'k1',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testIsNotEmpty(): void
+ {
+ $map = [
+ 'k1' => 'v1',
+ 'k' => '',
+ 'array' => [],
+ 'null' => null,
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isNotEmpty',
+ 'attribute' => 'k',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isNotEmpty',
+ 'attribute' => 'array',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isNotEmpty',
+ 'attribute' => 'null',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isNotEmpty',
+ 'attribute' => 'nonExistent',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isNotEmpty',
+ 'attribute' => 'k1',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testIsTrue(): void
+ {
+ $map = [
+ 'k1' => true,
+ 'k2' => false,
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isTrue',
+ 'attribute' => 'k1',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isTrue',
+ 'attribute' => 'k2',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isTrue',
+ 'attribute' => 'nonExistent',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testIsFalse(): void
+ {
+ $map = [
+ 'k1' => true,
+ 'k2' => false,
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isFalse',
+ 'attribute' => 'k1',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isFalse',
+ 'attribute' => 'k2',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isFalse',
+ 'attribute' => 'nonExistent',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testContains(): void
+ {
+ $map = [
+ 'string' => '_test_',
+ 'array' => ['test'],
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'contains',
+ 'attribute' => 'string',
+ 'value' => 'test',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'contains',
+ 'attribute' => 'string',
+ 'value' => 'hello',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'contains',
+ 'attribute' => 'array',
+ 'value' => 'test',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'contains',
+ 'attribute' => 'array',
+ 'value' => 'hello',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testNotContains(): void
+ {
+ $map = [
+ 'string' => '_test_',
+ 'array' => ['test'],
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'notContains',
+ 'attribute' => 'string',
+ 'value' => 'test',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'notContains',
+ 'attribute' => 'string',
+ 'value' => 'hello',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'notContains',
+ 'attribute' => 'array',
+ 'value' => 'test',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'notContains',
+ 'attribute' => 'array',
+ 'value' => 'hello',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testIn(): void
+ {
+ $map = [
+ 'array' => ['test'],
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'in',
+ 'attribute' => 'array',
+ 'value' => 'test',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'in',
+ 'attribute' => 'array',
+ 'value' => 'hello',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testNotIn(): void
+ {
+ $map = [
+ 'array' => ['test'],
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'notIn',
+ 'attribute' => 'array',
+ 'value' => 'test',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'notIn',
+ 'attribute' => 'array',
+ 'value' => 'hello',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testStartsWith(): void
+ {
+ $map = [
+ 'string' => '_test+',
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'startsWith',
+ 'attribute' => 'string',
+ 'value' => '_',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'startsWith',
+ 'attribute' => 'string',
+ 'value' => '+',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testEndsWith(): void
+ {
+ $map = [
+ 'string' => '_test+',
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'endsWith',
+ 'attribute' => 'string',
+ 'value' => '+',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'endsWith',
+ 'attribute' => 'string',
+ 'value' => '_',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testMatches(): void
+ {
+ $map = [
+ 'string' => '11111-222222-33',
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'matches',
+ 'attribute' => 'string',
+ 'value' => '/^\d{5}-\d{6}-\d{2}$/',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'matches',
+ 'attribute' => 'string',
+ 'value' => '/^\d{5}-\d{6}-\d{3}$/',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testGreaterThan(): void
+ {
+ $map = [
+ 'value' => 1,
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'greaterThan',
+ 'attribute' => 'value',
+ 'value' => 0,
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'greaterThan',
+ 'attribute' => 'value',
+ 'value' => 1,
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'greaterThan',
+ 'attribute' => 'value',
+ 'value' => 2,
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testLessThan(): void
+ {
+ $map = [
+ 'value' => 1,
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'lessThan',
+ 'attribute' => 'value',
+ 'value' => 2,
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'lessThan',
+ 'attribute' => 'value',
+ 'value' => 1,
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'lessThan',
+ 'attribute' => 'value',
+ 'value' => 0,
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testGreaterThanOrEquals(): void
+ {
+ $map = [
+ 'value' => 1,
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'greaterThanOrEquals',
+ 'attribute' => 'value',
+ 'value' => 0,
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'greaterThanOrEquals',
+ 'attribute' => 'value',
+ 'value' => 1,
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'greaterThanOrEquals',
+ 'attribute' => 'value',
+ 'value' => 2,
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testLessThanOrEquals(): void
+ {
+ $map = [
+ 'value' => 1,
+ ];
+
+ $checker = new ConditionChecker($this->initEntity($map));
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'lessThanOrEquals',
+ 'attribute' => 'value',
+ 'value' => 2,
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'lessThanOrEquals',
+ 'attribute' => 'value',
+ 'value' => 1,
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'lessThanOrEquals',
+ 'attribute' => 'value',
+ 'value' => 0,
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testIsToday(): void
+ {
+ $map = [
+ 'dateTime' => '2025-01-01 15:00:00',
+ 'dateTimeNext' => '2025-01-02 15:00:00',
+ 'date' => '2025-01-01',
+ 'dateNext' => '2025-01-02',
+ ];
+
+ $checker = new ConditionChecker(
+ entity: $this->initEntity($map),
+ clock: $this->createClock(),
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isToday',
+ 'attribute' => 'dateTime',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isToday',
+ 'attribute' => 'date',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isToday',
+ 'attribute' => 'dateTimeNext',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'isToday',
+ 'attribute' => 'dateNext',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testInFuture(): void
+ {
+ $map = [
+ 'dateTime' => '2025-01-01 10:00:00',
+ 'dateTimeNext' => '2025-01-02 15:00:00',
+ 'date' => '2025-01-01',
+ 'dateNext' => '2025-01-02',
+ ];
+
+ $checker = new ConditionChecker(
+ entity: $this->initEntity($map),
+ clock: $this->createClock(),
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'inFuture',
+ 'attribute' => 'dateTime',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'inFuture',
+ 'attribute' => 'date',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'inFuture',
+ 'attribute' => 'dateTimeNext',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'inFuture',
+ 'attribute' => 'dateNext',
+ ]
+ ])
+ )
+ );
+ }
+
+ /**
+ * @noinspection PhpUnhandledExceptionInspection
+ */
+ public function testInPast(): void
+ {
+ $map = [
+ 'dateTime' => '2025-01-01 10:00:00',
+ 'dateTimePrevious' => '2025-01-01 01:00:00',
+ 'date' => '2025-01-01',
+ 'datePrevious' => '2024-12-31',
+ ];
+
+ $checker = new ConditionChecker(
+ entity: $this->initEntity($map),
+ clock: $this->createClock(),
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'inPast',
+ 'attribute' => 'dateTime',
+ ]
+ ])
+ )
+ );
+
+ $this->assertFalse(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'inPast',
+ 'attribute' => 'date',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'inPast',
+ 'attribute' => 'dateTimePrevious',
+ ]
+ ])
+ )
+ );
+
+ $this->assertTrue(
+ $checker->check(
+ Item::fromGroupDefinition([
+ (object) [
+ 'type' => 'inPast',
+ 'attribute' => 'datePrevious',
+ ]
+ ])
+ )
+ );
+ }
+}