diff --git a/Module/ModuleAbstract.php b/Module/ModuleAbstract.php index 293ab028b..b313a8765 100644 --- a/Module/ModuleAbstract.php +++ b/Module/ModuleAbstract.php @@ -18,10 +18,13 @@ use phpOMS\Application\ApplicationAbstract; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; use phpOMS\System\MimeType; +use phpOMS\Utils\StringUtils; /** * Module abstraction class. * + * @method __call(string $name, array $arguments) + * * @package phpOMS\Module * @license OMS License 1.0 * @link https://orange-management.org @@ -264,8 +267,10 @@ abstract class ModuleAbstract $this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', [ $account, null, $obj, - 0, 0, + StringUtils::intHash(\is_string($mapper) ? $mapper : \get_class($mapper)), 0, static::MODULE_NAME, + (string) $obj->getId(), + '', $ip, ]); } @@ -291,8 +296,10 @@ abstract class ModuleAbstract $this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', [ $account, null, $obj, - 0, 0, + StringUtils::intHash(\is_string($mapper) ? $mapper : \get_class($mapper)), 0, static::MODULE_NAME, + (string) $obj->getId(), + '', $ip, ]); } @@ -323,8 +330,10 @@ abstract class ModuleAbstract $this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-update', '', [ $account, $old, $new, - 0, 0, + StringUtils::intHash(\is_string($mapper) ? $mapper : \get_class($mapper)), 0, static::MODULE_NAME, + (string) $old->getId(), + '', $ip, ]); } @@ -349,8 +358,10 @@ abstract class ModuleAbstract $this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-delete', '', [ $account, $obj, null, - 0, 0, + StringUtils::intHash(\is_string($mapper) ? $mapper : \get_class($mapper)), 0, static::MODULE_NAME, + (string) $obj->getId(), + '', $ip, ]); } @@ -377,8 +388,10 @@ abstract class ModuleAbstract $this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-relation', '', [ $account, $rel1, $rel2, - 0, 0, + StringUtils::intHash(\is_string($mapper) ? $mapper : \get_class($mapper)), 0, static::MODULE_NAME, + '0', + '', $ip, ]); } diff --git a/Utils/StringUtils.php b/Utils/StringUtils.php index 6bdec30d2..0b26f11fc 100644 --- a/Utils/StringUtils.php +++ b/Utils/StringUtils.php @@ -377,4 +377,27 @@ final class StringUtils return ['values' => $diffValues, 'mask' => $diffMask]; } + + /** + * Create a int hash from a string + * + * @param string $str String to hash + * + * @return int + * + * @since 1.0.0 + */ + public static function intHash(string $str) : int + { + $res = 0; + $pow = 1; + $len = \strlen($str); + + for ($i = 0; $i < $len; ++$i) { + $res = ($res + (\ord($str[$i]) - \ord('a') + 1) * $pow) % (1e9 + 9); + $pow = ($pow * 31) % (1e9 + 9); + } + + return (int) $res; + } } diff --git a/tests/Utils/StringUtilsTest.php b/tests/Utils/StringUtilsTest.php index 0568ebe73..87f359774 100644 --- a/tests/Utils/StringUtilsTest.php +++ b/tests/Utils/StringUtilsTest.php @@ -84,6 +84,36 @@ class StringUtilsTest extends \PHPUnit\Framework\TestCase self::assertEquals(0, StringUtils::countCharacterFromStart(' Test string', 's')); } + /** + * @testdox A string creates a integer hash + * @covers phpOMS\Utils\StringUtils + * @group framework + */ + public function testIntHash() : void + { + self::assertGreaterThan(0, StringUtils::intHash('test')); + } + + /** + * @testdox The same string creates the same hash + * @covers phpOMS\Utils\StringUtils + * @group framework + */ + public function testSameHash() : void + { + self::assertEquals(StringUtils::intHash('test'), StringUtils::intHash('test')); + } + + /** + * @testdox Different strings create different hashes + * @covers phpOMS\Utils\StringUtils + * @group framework + */ + public function testDifferentHash() : void + { + self::assertEquals(StringUtils::intHash('test1'), StringUtils::intHash('test2')); + } + /** * @testdox Various data types can be stringified * @covers phpOMS\Utils\StringUtils