@@ -733,45 +733,25 @@ public static function pluralStudly($value, $count = 2)
733
733
*/
734
734
public static function password ($ length = 32 , $ letters = true , $ numbers = true , $ symbols = true , $ spaces = false )
735
735
{
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 ('' );
775
755
}
776
756
777
757
/**
0 commit comments