mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-12 23:08:41 +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\RequestAbstract;
|
||||||
use phpOMS\Message\ResponseAbstract;
|
use phpOMS\Message\ResponseAbstract;
|
||||||
use phpOMS\System\MimeType;
|
use phpOMS\System\MimeType;
|
||||||
|
use phpOMS\Utils\StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Module abstraction class.
|
* Module abstraction class.
|
||||||
*
|
*
|
||||||
|
* @method __call(string $name, array $arguments)
|
||||||
|
*
|
||||||
* @package phpOMS\Module
|
* @package phpOMS\Module
|
||||||
* @license OMS License 1.0
|
* @license OMS License 1.0
|
||||||
* @link https://orange-management.org
|
* @link https://orange-management.org
|
||||||
|
|
@ -264,8 +267,10 @@ abstract class ModuleAbstract
|
||||||
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', [
|
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', [
|
||||||
$account,
|
$account,
|
||||||
null, $obj,
|
null, $obj,
|
||||||
0, 0,
|
StringUtils::intHash(\is_string($mapper) ? $mapper : \get_class($mapper)), 0,
|
||||||
static::MODULE_NAME,
|
static::MODULE_NAME,
|
||||||
|
(string) $obj->getId(),
|
||||||
|
'',
|
||||||
$ip,
|
$ip,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
@ -291,8 +296,10 @@ abstract class ModuleAbstract
|
||||||
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', [
|
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-create', '', [
|
||||||
$account,
|
$account,
|
||||||
null, $obj,
|
null, $obj,
|
||||||
0, 0,
|
StringUtils::intHash(\is_string($mapper) ? $mapper : \get_class($mapper)), 0,
|
||||||
static::MODULE_NAME,
|
static::MODULE_NAME,
|
||||||
|
(string) $obj->getId(),
|
||||||
|
'',
|
||||||
$ip,
|
$ip,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
@ -323,8 +330,10 @@ abstract class ModuleAbstract
|
||||||
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-update', '', [
|
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-update', '', [
|
||||||
$account,
|
$account,
|
||||||
$old, $new,
|
$old, $new,
|
||||||
0, 0,
|
StringUtils::intHash(\is_string($mapper) ? $mapper : \get_class($mapper)), 0,
|
||||||
static::MODULE_NAME,
|
static::MODULE_NAME,
|
||||||
|
(string) $old->getId(),
|
||||||
|
'',
|
||||||
$ip,
|
$ip,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
@ -349,8 +358,10 @@ abstract class ModuleAbstract
|
||||||
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-delete', '', [
|
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-delete', '', [
|
||||||
$account,
|
$account,
|
||||||
$obj, null,
|
$obj, null,
|
||||||
0, 0,
|
StringUtils::intHash(\is_string($mapper) ? $mapper : \get_class($mapper)), 0,
|
||||||
static::MODULE_NAME,
|
static::MODULE_NAME,
|
||||||
|
(string) $obj->getId(),
|
||||||
|
'',
|
||||||
$ip,
|
$ip,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
@ -377,8 +388,10 @@ abstract class ModuleAbstract
|
||||||
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-relation', '', [
|
$this->app->eventManager->trigger('POST:Module:' . static::MODULE_NAME . '-' . $trigger . '-relation', '', [
|
||||||
$account,
|
$account,
|
||||||
$rel1, $rel2,
|
$rel1, $rel2,
|
||||||
0, 0,
|
StringUtils::intHash(\is_string($mapper) ? $mapper : \get_class($mapper)), 0,
|
||||||
static::MODULE_NAME,
|
static::MODULE_NAME,
|
||||||
|
'0',
|
||||||
|
'',
|
||||||
$ip,
|
$ip,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -377,4 +377,27 @@ final class StringUtils
|
||||||
|
|
||||||
return ['values' => $diffValues, 'mask' => $diffMask];
|
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'));
|
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
|
* @testdox Various data types can be stringified
|
||||||
* @covers phpOMS\Utils\StringUtils
|
* @covers phpOMS\Utils\StringUtils
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user