Skip to content

Commit

Permalink
Merge pull request #5715 from getkirby/v4/feature/str-random-base32
Browse files Browse the repository at this point in the history
`Str::pool()`: New `base32`/`base32hex` pools
  • Loading branch information
lukasbestle authored Sep 30, 2023
2 parents 6782b9d + e40aff9 commit b3be965
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Toolkit/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,8 @@ public static function pool(
'alpha' => static::pool(['alphaLower', 'alphaUpper']),
'num' => range(0, 9),
'alphanum' => static::pool(['alpha', 'num']),
'base32' => array_merge(static::pool('alphaUpper'), range(2, 7)),
'base32hex' => array_merge(range(0, 9), range('A', 'V')),
default => $pool
};
}
Expand Down
32 changes: 26 additions & 6 deletions tests/Toolkit/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,33 +570,53 @@ public function testPool()
{
// alpha
$string = Str::pool('alpha', false);
$this->assertTrue(V::alpha($string));
$this->assertSame(52, strlen($string));
$this->assertSame(
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
$string
);

// alphaLower
$string = Str::pool('alphaLower', false);
$this->assertTrue(V::alpha($string));
$this->assertSame(26, strlen($string));
$this->assertSame('abcdefghijklmnopqrstuvwxyz', $string);

// alphaUpper
$string = Str::pool('alphaUpper', false);
$this->assertTrue(V::alpha($string));
$this->assertSame(26, strlen($string));
$this->assertSame('ABCDEFGHIJKLMNOPQRSTUVWXYZ', $string);

// num
$string = Str::pool('num', false);
$this->assertTrue(V::num($string));
$this->assertSame(10, strlen($string));
$this->assertSame('0123456789', $string);

// alphaNum
$string = Str::pool('alphaNum', false);
$this->assertTrue(V::alphanum($string));
$this->assertSame(62, strlen($string));
$this->assertSame(
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
$string
);

// base32
$string = Str::pool('base32', false);
$this->assertSame(32, strlen($string));
$this->assertSame('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', $string);

// base32hex
$string = Str::pool('base32hex', false);
$this->assertSame(32, strlen($string));
$this->assertSame('0123456789ABCDEFGHIJKLMNOPQRSTUV', $string);

// default fallback: empty pool
$pool = Str::pool('invalid', true);
$this->assertSame([], $pool);

// [alphaLower, num]
$string = Str::pool(['alphaLower', 'num'], false);
$this->assertTrue(V::alphanum($string));
$this->assertSame(36, strlen($string));
$this->assertSame('abcdefghijklmnopqrstuvwxyz0123456789', $string);

// string vs. array
$this->assertIsString(Str::pool('alpha', false));
Expand Down

0 comments on commit b3be965

Please sign in to comment.