@@ -37,4 +37,178 @@ public static function replace($search, $replace, $subject)
3737 {
3838 return str_replace ($ search , $ replace , $ subject );
3939 }
40+
41+ public static function startsWith ($ haystack , $ needles )
42+ {
43+ foreach ((array ) $ needles as $ needle ) {
44+ if ($ needle !== '' && strncmp ($ haystack , $ needle , strlen ($ needle )) === 0 ) {
45+ return true ;
46+ }
47+ }
48+
49+ return false ;
50+ }
51+
52+ public static function endsWith ($ haystack , $ needles )
53+ {
54+ foreach ((array ) $ needles as $ needle ) {
55+ if ((string ) $ needle === static ::substr ($ haystack , -static ::length ($ needle ))) {
56+ return true ;
57+ }
58+ }
59+
60+ return false ;
61+ }
62+
63+ public static function length ($ value )
64+ {
65+ return mb_strlen ($ value );
66+ }
67+
68+ public static function substr ($ string , $ start , $ length = null )
69+ {
70+ return mb_substr ($ string , $ start , $ length , 'UTF-8 ' );
71+ }
72+
73+ public static function contains ($ haystack , $ needles )
74+ {
75+ foreach ((array ) $ needles as $ needle ) {
76+ if ($ needle !== '' && mb_strpos ($ haystack , $ needle ) !== false ) {
77+ return true ;
78+ }
79+ }
80+
81+ return false ;
82+ }
83+
84+ public static function isSnakeCase ($ value )
85+ {
86+ return $ value === static ::snake ($ value );
87+ }
88+
89+ public static function isCamelCase ($ value )
90+ {
91+ return $ value === static ::camel ($ value );
92+ }
93+
94+ public static function isStudlyCase ($ value )
95+ {
96+ return $ value === static ::studly ($ value );
97+ }
98+
99+ /**
100+ * Generate a more truly "random" alpha-numeric string.
101+ *
102+ * @param int $length
103+ * @return string
104+ *
105+ * @throws \Exception
106+ */
107+ public static function random ($ length = 16 )
108+ {
109+ $ string = '' ;
110+
111+ while (($ len = static ::length ($ string )) < $ length ) {
112+ $ size = $ length - $ len ;
113+
114+ $ bytes = random_bytes ($ size );
115+
116+ $ string .= substr (str_replace (['/ ' , '+ ' , '= ' ], '' , base64_encode ($ bytes )), 0 , $ size );
117+ }
118+
119+ return $ string ;
120+ }
121+
122+ /**
123+ * Generate a more truly "random" alpha-numeric string of ASCII characters.
124+ *
125+ * @param int $length
126+ * @return string
127+ *
128+ * @throws \Exception
129+ */
130+ public static function randomAscii ($ length = 16 )
131+ {
132+ return static ::substr (str_replace (['/ ' , '+ ' , '= ' ], '' , base64_encode (random_bytes ($ length ))), 0 , $ length );
133+ }
134+
135+ /**
136+ * Generate a more truly "random" numeric string.
137+ *
138+ * @param int $length
139+ * @return string
140+ *
141+ * @throws \Exception
142+ */
143+ public static function randomNumeric ($ length = 16 )
144+ {
145+ $ string = '' ;
146+
147+ while (($ len = static ::length ($ string )) < $ length ) {
148+ $ size = $ length - $ len ;
149+
150+ $ bytes = random_bytes ($ size );
151+
152+ $ string .= preg_replace ('/[^0-9]/ ' , '' , base64_encode ($ bytes ));
153+ }
154+
155+ return static ::substr ($ string , 0 , $ length );
156+ }
157+
158+ /**
159+ * Generate a more truly "random" alpha-numeric string.
160+ *
161+ * @param int $length
162+ * @return string
163+ *
164+ * @throws \Exception
165+ */
166+ public static function randomAlphanumeric ($ length = 16 )
167+ {
168+ $ string = '' ;
169+
170+ while (($ len = static ::length ($ string )) < $ length ) {
171+ $ size = $ length - $ len ;
172+
173+ $ bytes = random_bytes ($ size );
174+
175+ $ string .= preg_replace ('/[^A-Za-z0-9]/ ' , '' , base64_encode ($ bytes ));
176+ }
177+
178+ return static ::substr ($ string , 0 , $ length );
179+ }
180+
181+ /**
182+ * Generate a more truly "random" string.
183+ *
184+ * @param int $length
185+ * @param string|null $characters
186+ * @return string
187+ *
188+ * @throws \Exception
189+ */
190+ public static function randomString ($ length = 16 , $ characters = null )
191+ {
192+ $ string = '' ;
193+
194+ $ characters = $ characters ?: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ' ;
195+
196+ $ max = static ::length ($ characters ) - 1 ;
197+
198+ while (($ len = static ::length ($ string )) < $ length ) {
199+ $ string .= $ characters [random_int (0 , $ max )];
200+ }
201+
202+ return $ string ;
203+ }
204+
205+ public static function randomStringWithNumeric ($ length = 16 )
206+ {
207+ return static ::randomString ($ length , 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ' );
208+ }
209+
210+ public static function randomStringWithSpecialCharacter ($ length = 16 )
211+ {
212+ return static ::randomString ($ length , 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=[]{};:,.<>/? ' );
213+ }
40214}
0 commit comments