Move uniqid creation methods from Hash to Uids class

uniq id short and long without parametersare pure uid creations so they
have moved over and have been deprecated in the Hash class.

Update Logging class for this.
Update Form\Generate for cursor ext access
This commit is contained in:
Clemens Schwaighofer
2022-03-02 09:18:49 +09:00
parent 714311cf85
commit fe13c24a13
7 changed files with 128 additions and 64 deletions

View File

@@ -110,15 +110,6 @@ final class CoreLibsCreateHashTest extends TestCase
];
}
public function uniqIdLongProvider(): array
{
return [
'uniq id long: ' . \CoreLibs\Create\Hash::STANDARD_HASH_LONG => [
strlen(hash(\CoreLibs\Create\Hash::STANDARD_HASH_LONG, 'A'))
],
];
}
/**
* Undocumented function
*
@@ -194,24 +185,6 @@ final class CoreLibsCreateHashTest extends TestCase
}
}
/**
* Undocumented function
*
* @covers ::__uniqueId
* @testWith [8]
* @testdox __uniqId will be length $expected [$_dataName]
*
* @param integer $expected
* @return void
*/
public function testUniqId(int $expected): void
{
$this->assertEquals(
$expected,
strlen(\CoreLibs\Create\Hash::__uniqId())
);
}
/**
* Undocumented function
*
@@ -230,24 +203,6 @@ final class CoreLibsCreateHashTest extends TestCase
\CoreLibs\Create\Hash::__hashLong($input)
);
}
/**
* Undocumented function
*
* @covers ::__uniqueIdLong
* @dataProvider uniqIdLongProvider
* @testdox __uniqIdLong will be length $expected [$_dataName]
*
* @param integer $expected
* @return void
*/
public function testUniqIdLong(int $expected): void
{
$this->assertEquals(
$expected,
strlen(\CoreLibs\Create\Hash::__uniqIdLong())
);
}
}
// __END__

View File

@@ -13,6 +13,11 @@ use PHPUnit\Framework\TestCase;
*/
final class CoreLibsCreateUidsTest extends TestCase
{
/**
* Undocumented function
*
* @return array
*/
public function uniqIdProvider(): array
{
return [
@@ -32,6 +37,10 @@ final class CoreLibsCreateUidsTest extends TestCase
0 => 'adler32',
1 => 8
],
'not in list hash but valid' => [
0 => 'sha3-512',
1 => strlen(hash('sha3-512', 'A'))
],
'default hash not set' => [
0 => null,
1 => 64,
@@ -39,10 +48,37 @@ final class CoreLibsCreateUidsTest extends TestCase
'invalid name' => [
0 => 'iamnotavalidhash',
1 => 64,
]
],
'auto: ' . \CoreLibs\Create\Uids::DEFAULT_HASH => [
0 => \CoreLibs\Create\Uids::DEFAULT_HASH,
1 => strlen(hash(\CoreLibs\Create\Uids::DEFAULT_HASH, 'A'))
],
'auto: ' . \CoreLibs\Create\Uids::STANDARD_HASH_LONG => [
0 => \CoreLibs\Create\Uids::STANDARD_HASH_LONG,
1 => strlen(hash(\CoreLibs\Create\Uids::STANDARD_HASH_LONG, 'A'))
],
'auto: ' . \CoreLibs\Create\Uids::STANDARD_HASH_SHORT => [
0 => \CoreLibs\Create\Uids::STANDARD_HASH_SHORT,
1 => strlen(hash(\CoreLibs\Create\Uids::STANDARD_HASH_SHORT, 'A'))
],
];
}
/**
* Undocumented function
*
* @return array
*/
public function uniqIdLongProvider(): array
{
return [
'uniq id long: ' . \CoreLibs\Create\Uids::STANDARD_HASH_LONG => [
strlen(hash(\CoreLibs\Create\Uids::STANDARD_HASH_LONG, 'A'))
],
];
}
/**
* must match 7e78fe0d-59b8-4637-af7f-e88d221a7d1e
*
@@ -109,6 +145,42 @@ final class CoreLibsCreateUidsTest extends TestCase
strlen(\CoreLibs\Create\Uids::uniqId())
);
}
/**
* Short id, always 8 in length
*
* @covers ::uniqIdShort
* @testWith [8]
* @testdox uniqIdShort will be length $expected [$_dataName]
*
* @param integer $expected
* @return void
*/
public function testUniqIdShort(int $expected): void
{
$this->assertEquals(
$expected,
strlen(\CoreLibs\Create\Uids::uniqIdShort())
);
}
/**
* Long Id, length can change
*
* @covers ::uniqIdLong
* @dataProvider uniqIdLongProvider
* @testdox uniqIdLong will be length $expected [$_dataName]
*
* @param integer $expected
* @return void
*/
public function testUniqIdLong(int $expected): void
{
$this->assertEquals(
$expected,
strlen(\CoreLibs\Create\Uids::uniqIdLong())
);
}
}
// __END__

View File

@@ -68,8 +68,8 @@ print "<br>CURRENT STANDARD_HASH_LONG: " . Hash::STANDARD_HASH_LONG . "<br>";
print "HASH SHORT: " . $to_crc . ": " . Hash::__hash($to_crc) . "<br>";
print "HASH LONG: " . $to_crc . ": " . Hash::__hashLong($to_crc) . "<br>";
print "UNIQU ID SHORT : " . Hash::__uniqId() . "<br>";
print "UNIQU ID LONG : " . Hash::__uniqIdLong() . "<br>";
// print "UNIQU ID SHORT : " . Hash::__uniqId() . "<br>";
// print "UNIQU ID LONG : " . Hash::__uniqIdLong() . "<br>";
// DEPRECATED
/* print "D/__CRC32B: $to_crc: ".$basic->__crc32b($to_crc)."<br>";

View File

@@ -91,10 +91,13 @@ class Hash
* create a unique id with the standard hash type defined in __hash
*
* @return string Unique ID with fixed length of 8 characters
* @deprecated Use \CoreLibs\Create\Uids::uniqIdShort() instead
*/
public static function __uniqId(): string
{
return self::__hash(uniqid((string)rand(), true));
trigger_error('Method ' . __METHOD__ . ' is deprecated, '
. '\CoreLibs\Create\Uids::uniqIdShort() class', E_USER_DEPRECATED);
return \CoreLibs\Create\Uids::uniqIdShort();
}
/**
@@ -102,10 +105,13 @@ class Hash
* defined in __hashLong
*
* @return string Unique ID with length of current default long hash
* @deprecated Use \CoreLibs\Create\Uids::uniqIdLong() instead
*/
public static function __uniqIdLong(): string
{
return self::__hashLong(uniqid((string)rand(), true));
trigger_error('Method ' . __METHOD__ . ' is deprecated, '
. '\CoreLibs\Create\Uids::uniqIdLong() class', E_USER_DEPRECATED);
return \CoreLibs\Create\Uids::uniqIdLong();
}
}

View File

@@ -7,7 +7,9 @@ namespace CoreLibs\Create;
class Uids
{
// what to use as a default hash if non ise set and no DEFAULT_HASH is defined
public const FALLBACK_HASH = 'sha256';
public const DEFAULT_HASH = 'sha256';
public const STANDARD_HASH_LONG = 'ripemd160';
public const STANDARD_HASH_SHORT = 'adler32';
/**
* creates psuedo random uuid v4
@@ -53,23 +55,52 @@ class Uids
case 'md5':
$uniq_id = md5(uniqid((string)rand(), true));
break;
case 'sha256':
$uniq_id = hash('sha256', uniqid((string)rand(), true));
case self::DEFAULT_HASH:
$uniq_id = hash(self::DEFAULT_HASH, uniqid((string)rand(), true));
break;
case 'ripemd160':
$uniq_id = hash('ripemd160', uniqid((string)rand(), true));
case self::STANDARD_HASH_LONG:
$uniq_id = hash(self::STANDARD_HASH_LONG, uniqid((string)rand(), true));
break;
case 'adler32':
$uniq_id = hash('adler32', uniqid((string)rand(), true));
case self::STANDARD_HASH_SHORT:
$uniq_id = hash(self::STANDARD_HASH_SHORT, uniqid((string)rand(), true));
break;
default:
// fallback to this hash type
$hash = self::FALLBACK_HASH;
// if not empty, check if in valid list
if (
!empty($type) &&
in_array($type, hash_algos())
) {
$hash = $type;
} else {
// fallback to default hash type if none set or invalid
$hash = self::DEFAULT_HASH;
}
$uniq_id = hash($hash, uniqid((string)rand(), true));
break;
}
return $uniq_id;
}
/**
* create a unique id with the standard hash type defined in __hash
*
* @return string Unique ID with fixed length of 8 characters
*/
public static function uniqIdShort(): string
{
return self::uniqId(self::STANDARD_HASH_SHORT);
}
/**
* create a unique id with the standard long hash type
* defined in __hashLong
*
* @return string Unique ID with length of current default long hash
*/
public static function uniqIdLong(): string
{
return self::uniqId(self::STANDARD_HASH_LONG);
}
}
// __END__

View File

@@ -25,7 +25,7 @@ declare(strict_types=1);
namespace CoreLibs\Debug;
use CoreLibs\Debug\Support;
use CoreLibs\Create\Hash;
use CoreLibs\Create\Uids;
use CoreLibs\Get\System;
use CoreLibs\Convert\Html;
@@ -161,7 +161,7 @@ class Logging
// running time start for script
$this->script_starttime = microtime(true);
// set per run UID for logging
$this->running_uid = Hash::__uniqId();
$this->running_uid = Uids::uniqIdShort();
// set the page name
$this->page_name = System::getPageName();
// set host name

View File

@@ -2246,17 +2246,17 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
// if drop down db read data for element list from the given sub table as from the query
// only two elements are allowed: pos 0 is key, pso 1 is visible output name
if (isset($data_array['type']) && $data_array['type'] == 'drop_down_db') {
$md_q = md5($data_array['query']);
while (is_array($res = $this->dbReturn($data_array['query']))) {
/** @phan-suppress-next-line PhanTypeInvalidDimOffset */
$this->log->debug('edit', 'Q[' . $md_q . '] pos: ' . $this->cursor_ext[$md_q]['pos']
$this->log->debug('edit', 'Q[' . $this->dbGetQueryHash($data_array['query']) . '] pos: '
. $this->dbGetCursorExt($data_array['query'], 'pos')
. ' | want: ' . ($data_array['preset'] ?? '-')
. ' | set: ' . ($data['preset'][$el_name] ?? '-'));
// first is default for this element
if (
isset($data_array['preset']) &&
(!isset($data['preset'][$el_name]) || empty($data['preset'][$el_name])) &&
($this->cursor_ext[$md_q]['pos'] == $data_array['preset'])
($this->dbGetCursorExt($data_array['query'], 'pos') == $data_array['preset'])
) {
$data['preset'][$el_name] = $res[0];
}