Change Random key base to be string not array

Because we do not have double byte characters in there we use a string
so we don't have to do any array work. Return is also a string and not
an array that is then converted to string.

Add info to Colors Class for oklab code that we should use as basic for
all conversions
This commit is contained in:
Clemens Schwaighofer
2022-02-21 06:50:45 +09:00
parent c584af8393
commit 0109a67b20
4 changed files with 19 additions and 9 deletions

View File

@@ -4,6 +4,7 @@ includes:
parameters:
tmpDir: /tmp/phpstan-corelibs
level: 8 # max is now 9
checkMissingCallableSignature: true
paths:
- %currentWorkingDirectory%/www
bootstrapFiles:

View File

@@ -50,11 +50,13 @@ print '<div><a href="class_test.php">Class Test Master</a></div>';
$key_length = 10;
$key_length_b = 5;
$key_lenght_long = 64;
print "S::RANDOMKEYGEN(auto): " . RandomKey::randomKeyGen() . "<br>";
print "S::SETRANDOMKEYLENGTH($key_length): " . RandomKey::setRandomKeyLength($key_length) . "<br>";
print "S::RANDOMKEYGEN($key_length): " . RandomKey::randomKeyGen() . "<br>";
print "S::RANDOMKEYGEN($key_length_b): " . RandomKey::randomKeyGen($key_length_b) . "<br>";
print "S::RANDOMKEYGEN($key_length): " . RandomKey::randomKeyGen() . "<br>";
print "S::RANDOMKEYGEN($key_lenght_long): " . RandomKey::randomKeyGen($key_lenght_long) . "<br>";
$_array = new CoreLibs\Create\RandomKey();
print "C->RANDOMKEYGEN(auto): " . $_array->randomKeyGen() . "<br>";
// DEPRECATED

View File

@@ -10,6 +10,9 @@
* hsl to rgb
*/
// TODO: use oklab as base for converting colors
// https://bottosson.github.io/posts/oklab/
declare(strict_types=1);
namespace CoreLibs\Convert;

View File

@@ -11,8 +11,8 @@ namespace CoreLibs\Create;
class RandomKey
{
// key generation
/** @var array<mixed> */
private static $key_range = [];
/** @var string */
private static $key_range = '';
/** @var int */
private static $one_key_length;
/** @var int */
@@ -33,9 +33,13 @@ class RandomKey
*/
private static function initRandomKeyData()
{
// random key generation
self::$key_range = array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9'));
self::$one_key_length = count(self::$key_range);
// random key generation base string
self::$key_range = join('', array_merge(
range('A', 'Z'),
range('a', 'z'),
range('0', '9')
));
self::$one_key_length = strlen(self::$key_range);
}
/**
@@ -103,11 +107,11 @@ class RandomKey
} else {
$use_key_length = self::$key_length;
}
$pieces = [];
// create random string
$random_string = '';
for ($i = 1; $i <= $use_key_length; $i++) {
$pieces[] = self::$key_range[random_int(0, self::$one_key_length - 1)];
$random_string .= self::$key_range[random_int(0, self::$one_key_length - 1)];
}
return join('', $pieces);
return $random_string;
}
}