Skip to content

Commit a501fc2

Browse files
committed
be functional
1 parent 95c24f7 commit a501fc2

File tree

1 file changed

+19
-39
lines changed

1 file changed

+19
-39
lines changed

src/Illuminate/Support/Str.php

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -733,45 +733,25 @@ public static function pluralStudly($value, $count = 2)
733733
*/
734734
public static function password($length = 32, $letters = true, $numbers = true, $symbols = true, $spaces = false)
735735
{
736-
$characters = [];
737-
738-
if ($letters) {
739-
$characters = array_merge($characters, [
740-
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
741-
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
742-
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
743-
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
744-
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
745-
]);
746-
}
747-
748-
if ($numbers) {
749-
$characters = array_merge($characters, [
750-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
751-
]);
752-
}
753-
754-
if ($symbols) {
755-
$characters = array_merge($characters, [
756-
'~', '!', '#', '$', '%', '^', '&', '*', '(', ')', '-',
757-
'_', '.', ',', '<', '>', '?', '/', '\\', '{', '}', '[',
758-
']', '|', ':', ';',
759-
]);
760-
}
761-
762-
if ($spaces) {
763-
$characters = array_merge($characters, [' ']);
764-
}
765-
766-
$password = '';
767-
768-
$possible = count($characters) - 1;
769-
770-
for ($i = 0; $i < $length; $i++) {
771-
$password = $password.$characters[random_int(0, $possible)];
772-
}
773-
774-
return $password;
736+
return (new Collection)
737+
->when($letters, fn ($c) => $c->merge([
738+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
739+
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
740+
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
741+
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
742+
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
743+
]))
744+
->when($numbers, fn ($c) => $c->merge([
745+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
746+
]))
747+
->when($symbols, fn ($c) => $c->merge([
748+
'~', '!', '#', '$', '%', '^', '&', '*', '(', ')', '-',
749+
'_', '.', ',', '<', '>', '?', '/', '\\', '{', '}', '[',
750+
']', '|', ':', ';',
751+
]))
752+
->when($spaces, fn ($c) => $c->merge([' ']))
753+
->pipe(fn ($c) => Collection::times($length, fn () => $c[random_int(0, $c->count() - 1)]))
754+
->implode('');
775755
}
776756

777757
/**

0 commit comments

Comments
 (0)