Skip to content

Commit

Permalink
Merge pull request #5 from mpstenson/random-phrase-and-word
Browse files Browse the repository at this point in the history
RandomWord and RandomPhrase functions
  • Loading branch information
mpstenson authored Aug 25, 2024
2 parents a6e8fe8 + 83db4dc commit 2b335ac
Show file tree
Hide file tree
Showing 6 changed files with 7,974 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ AdvStr::redactSsn('My social security number is 222-22-2222'); // My social secu
- [advPassword](#advpassword)
- [charWrap](#charwrap)
- [emailDomain](#emaildomain)
- [randomPhrase](#randomphrase)
- [randomWord](#randomword)
- [readTime](#readtime)
- [redactCreditCard](#redactcreditcard)
- [redactSsn](#redactssn)
Expand Down Expand Up @@ -142,6 +144,38 @@ public static function emailDomain(
#### Returns:
- string: The email domain from the string

### [randomPhrase](#randomphrase)

Returns a random phrase with a configurable delimiter.

```php
public static function randomPhrase(
$wordCount,
$separator = '-'
)
```

#### Parameters:
- `$wordCount` (int): The number of words in the phrase.
- `$separator` (string): The separator between words (default: '-').

#### Returns:
- string: The generated random phrase.
### [randomWord](#randomword)

Returns a random word.

```php
public static function randomWord(
)
```

#### Parameters:
- none

#### Returns:
- string: A random word

### [readTime](#readtime)

Calculates the read time of a string.
Expand Down
43 changes: 43 additions & 0 deletions src/AdvStr.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class AdvStr
{
private static ?array $words = null;

/**
* Generate a random, secure password.
*
Expand Down Expand Up @@ -323,4 +325,45 @@ public static function redactCreditCard($string, $redacted = '********', $exclud

return $string;
}

/**
* Generate a random word from the loaded word list.
*
* @return string A randomly selected word.
*/
public static function randomWord(): string
{
self::loadWords();

return self::$words[array_rand(self::$words)];
}

/**
* Generate a random phrase with a specified number of words.
*
* @param int $wordCount The number of words in the phrase.
* @param string $separator The separator between words (default: '-').
* @return string The generated random phrase.
*/
public static function randomPhrase(int $wordCount, string $separator = '-'): string
{
self::loadWords();
$phrase = [];
$poolCount = count(self::$words);
for ($i = 0; $i < $wordCount; $i++) {
$phrase[] = self::$words[random_int(0, $poolCount - 1)];
}

return implode($separator, $phrase);
}

/**
* Load the words from the JSON file if not already loaded.
*/
private static function loadWords(): void
{
if (self::$words === null) {
self::$words = json_decode(file_get_contents(__DIR__.'/words.json'), true);
}
}
}
Loading

0 comments on commit 2b335ac

Please sign in to comment.