Skip to content

Commit 2b335ac

Browse files
authored
Merge pull request #5 from mpstenson/random-phrase-and-word
RandomWord and RandomPhrase functions
2 parents a6e8fe8 + 83db4dc commit 2b335ac

File tree

6 files changed

+7974
-0
lines changed

6 files changed

+7974
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ AdvStr::redactSsn('My social security number is 222-22-2222'); // My social secu
3232
- [advPassword](#advpassword)
3333
- [charWrap](#charwrap)
3434
- [emailDomain](#emaildomain)
35+
- [randomPhrase](#randomphrase)
36+
- [randomWord](#randomword)
3537
- [readTime](#readtime)
3638
- [redactCreditCard](#redactcreditcard)
3739
- [redactSsn](#redactssn)
@@ -142,6 +144,38 @@ public static function emailDomain(
142144
#### Returns:
143145
- string: The email domain from the string
144146

147+
### [randomPhrase](#randomphrase)
148+
149+
Returns a random phrase with a configurable delimiter.
150+
151+
```php
152+
public static function randomPhrase(
153+
$wordCount,
154+
$separator = '-'
155+
)
156+
```
157+
158+
#### Parameters:
159+
- `$wordCount` (int): The number of words in the phrase.
160+
- `$separator` (string): The separator between words (default: '-').
161+
162+
#### Returns:
163+
- string: The generated random phrase.
164+
### [randomWord](#randomword)
165+
166+
Returns a random word.
167+
168+
```php
169+
public static function randomWord(
170+
)
171+
```
172+
173+
#### Parameters:
174+
- none
175+
176+
#### Returns:
177+
- string: A random word
178+
145179
### [readTime](#readtime)
146180

147181
Calculates the read time of a string.

src/AdvStr.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class AdvStr
88
{
9+
private static ?array $words = null;
10+
911
/**
1012
* Generate a random, secure password.
1113
*
@@ -323,4 +325,45 @@ public static function redactCreditCard($string, $redacted = '********', $exclud
323325

324326
return $string;
325327
}
328+
329+
/**
330+
* Generate a random word from the loaded word list.
331+
*
332+
* @return string A randomly selected word.
333+
*/
334+
public static function randomWord(): string
335+
{
336+
self::loadWords();
337+
338+
return self::$words[array_rand(self::$words)];
339+
}
340+
341+
/**
342+
* Generate a random phrase with a specified number of words.
343+
*
344+
* @param int $wordCount The number of words in the phrase.
345+
* @param string $separator The separator between words (default: '-').
346+
* @return string The generated random phrase.
347+
*/
348+
public static function randomPhrase(int $wordCount, string $separator = '-'): string
349+
{
350+
self::loadWords();
351+
$phrase = [];
352+
$poolCount = count(self::$words);
353+
for ($i = 0; $i < $wordCount; $i++) {
354+
$phrase[] = self::$words[random_int(0, $poolCount - 1)];
355+
}
356+
357+
return implode($separator, $phrase);
358+
}
359+
360+
/**
361+
* Load the words from the JSON file if not already loaded.
362+
*/
363+
private static function loadWords(): void
364+
{
365+
if (self::$words === null) {
366+
self::$words = json_decode(file_get_contents(__DIR__.'/words.json'), true);
367+
}
368+
}
326369
}

0 commit comments

Comments
 (0)