mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 09:48:40 +00:00
added hashing of strings to ints
This commit is contained in:
parent
e736214417
commit
568f3fab4b
|
|
@ -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,
|
||||
]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user