Skip to content

Commit

Permalink
added random string function
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Dec 29, 2023
1 parent 75d28a4 commit 9be157f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/PhPease/BufferedVar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class BufferedVar
/**
* @var array
*/
protected static $_buffer = [];
protected static array $_buffer = [];

/**
* @param $token string unique token
* @param $callable mixed function or variable to be buffered
* @param string $ns buffer namespace
* @return mixed
*/
public static function get($token, $callable, $ns = 'default')
public static function get(string $token, $callable, string $ns = 'default')
{
if (!self::exists($token, $ns)) {
if (is_callable($callable)) {
Expand All @@ -43,13 +43,12 @@ public static function get($token, $callable, $ns = 'default')
* @param string $ns
* @return bool
*/
public static function exists($token, $ns = 'default')
public static function exists($token, string $ns = 'default'): bool
{
if (!isset(self::$_buffer[$ns])) {
self::$_buffer[$ns] = [];
}
$result = isset(self::$_buffer[$ns]) && array_key_exists($token, self::$_buffer[$ns]);
return $result;
return isset(self::$_buffer[$ns]) && array_key_exists($token, self::$_buffer[$ns]);
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/PhPease/String/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace PhPease\String;


use PhPease\BufferedVar;

function filter_string($string, $mode='alphanum') {
switch ($mode) {
case 'filename':
Expand Down Expand Up @@ -157,3 +159,29 @@ function str_replace_all_except(string $str, $what, string $with = '_', bool $co
function str_replace_all_except_numbers(string $str, string $with = '_', bool $combineRepeating = true) {
return str_replace_all_except($str, '/[^Z0-9]/', $with, $combineRepeating);
}

/**
* A simple, handy function to create a random string without any claim to uniqueness.
* Due to the intention of use for everyday non-critical tasks, with an upper character length limit of 255 characters.
*
* @param int $length
* @param string|null $token If specified, the identical string is returned within the same PHP process using PhPease\BufferedVar
* @return string
*/
function random_string(int $length = 10, string $token = null): string
{
if (!empty($token)) {
return BufferedVar::get($token, random_string($length));
}

if ($length > 255) {
$length = 255;
}

$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $chars[mt_rand(0, strlen($chars) - 1)];
}
return $string;
}
23 changes: 23 additions & 0 deletions tests/StringTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use function PhPease\String\random_string;
use function PhPease\String\str_contains;
use function PhPease\String\str_ends_with;
use function PhPease\String\str_replace_all_except_numbers;
Expand Down Expand Up @@ -59,4 +60,26 @@ public function testStrReplaceAllExceptNumbers()
$this->assertEquals('20-08-2012-20-13-33', str_replace_all_except_numbers('20.08.2012 - 20:13:33', '-'));
$this->assertEquals('20-08-2012---20-13-33', str_replace_all_except_numbers('20.08.2012 - 20:13:33', '-', false));
}

public function testRandomString()
{
$this->assertEquals(10, strlen(random_string()));
$this->assertEquals(32, strlen(random_string(32)));
$this->assertEquals(16, strlen(random_string(16)));
$this->assertEquals(8, strlen(random_string(8)));
$this->assertEquals(4, strlen(random_string(4)));
$this->assertEquals(2, strlen(random_string(2)));
$this->assertEquals(1, strlen(random_string(1)));
$this->assertEquals(0, strlen(random_string(0)));
$this->assertEquals('', random_string(0));
$this->assertEquals(0, strlen(random_string(-1)));
$this->assertEquals(255, strlen(random_string(PHP_INT_MAX)));
$this->assertEquals(0, strlen(random_string(PHP_INT_MIN)));

$this->expectException(\TypeError::class);
$this->assertEquals(255, strlen(random_string(PHP_INT_MAX + 1)));

$this->expectException(\TypeError::class);
$this->assertEquals(0, strlen(random_string(PHP_INT_MIN - 1)));
}
}

0 comments on commit 9be157f

Please sign in to comment.