Skip to content

Commit

Permalink
Add dashed() random string generator
Browse files Browse the repository at this point in the history
  • Loading branch information
valorin committed Dec 31, 2023
1 parent 099bf07 commit 46ddf05
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ The following are wrappers for common use cases:

```php
// Random letters only
$string = Random::letters(int $length = 32): string;
$letters = Random::letters(int $length = 32): string;

// Random letters and numbers (i.e. a random token)
$string = Random::token(int $length = 32): string;
// Random alphanumeric (letters and numbers) token string
$token = Random::token(int $length = 32): string;

// Random letters, numbers, and symbols (i.e. a random password).
$string = Random::password(int $length = 16, bool $requireAll = false): string;
$password = Random::password(int $length = 16, bool $requireAll = false): string;

// Random alphanumeric token string with chunks separated by dashes, making it easy to read and type.
$password = Random::dashed(int $length = 25, string $delimiter = '-', int $chunkLength = 5): string;
```

To limit the characters available in any of the types (i.e. lower, upper, numbers, or symbols),
Expand Down
16 changes: 16 additions & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ public function password(int $length = 16, bool $requireAll = false): string
return $this->string($length, $lower = true, $upper = true, $numbers = true, $symbols = true, $requireAll);
}

/**
* Generate a random string of $length with lowercase and uppercase letters, and numbers, divided by $divider.
* This is suitable for use as a long random password that is easy to read and type.
*
* @param int $length
* @param string $delimiter
* @param int $chunkLength = 5
* @return string
*/
public function dashed(int $length = 25, string $delimiter = '-', int $chunkLength = 5): string
{
$string = $this->string($length, true, true, true, false, true);

return implode($delimiter, str_split($string, $chunkLength));
}

/**
* Shuffle the characters in an array, string, or Laravel Collection,
* optionally preserving the keys.
Expand Down
1 change: 1 addition & 0 deletions src/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @method static string letters(int $length = 32) Generate a random string of $length lowercase and uppercase letters.
* @method static string token(int $length = 32) Generate a random string of $length which includes lowercase and uppercase letters, and numbers.
* @method static string password(int $length = 32, bool $requireAll = false) Generate a random password string of $length which includes lowercase and uppercase letters, numbers, and symbols.
* @method static string dashed(int $length = 25, string $delimiter = '-', int $chunkLength = 5) Generate a random string of $length with lowercase and uppercase letters, and numbers, divided by $divider.
* @method static array|string|\Illuminate\Support\Collection shuffle(array|string|\Illuminate\Support\Collection $values, bool $preserveKeys = false) Shuffle the characters in an array, string, or Laravel Collection, optionally preserving the keys.
* @method static array|string|\Illuminate\Support\Collection pick(array|string|\Illuminate\Support\Collection $values, int $count) Pick $count random items (or characters) from an array, string, or Laravel Collection.
* @method static array|string|\Illuminate\Support\Collection pickOne(array|string|\Illuminate\Support\Collection $values) Picks a single item (or character) from an array, string, or Laravel Collection.
Expand Down
10 changes: 10 additions & 0 deletions tests/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ public function testPassword()
$this->assertTrue($valid, 'Not all character types were seen in the result. (Low chance of a false positive.)');
}

public function testDashed()
{
for ($i = 0; $i < 100; $i++) {
$string = Random::dashed($length = 25, $delimiter = '-');

$this->assertIsString($string);
$this->assertRegExpCustom('/^[a-zA-Z0-9]{5}\-[a-zA-Z0-9]{5}\-[a-zA-Z0-9]{5}\-[a-zA-Z0-9]{5}\-[a-zA-Z0-9]{5}$/', $string);
}
}

public function testCustomCharacterSets()
{
$generator = Random::useLower(range('a', 'f'))
Expand Down

0 comments on commit 46ddf05

Please sign in to comment.