@@ -184,4 +184,86 @@ public static function hasWebProtocol(string $string) : bool
184184
185185 return !empty ($ d ['scheme ' ]);
186186 }
187+
188+ /**
189+ * helper to make a strung slug
190+ *
191+ * @param string $string
192+ * @param string $sep
193+ * @return string
194+ */
195+ public static function stringToSlug (string $ string , $ sep = '- ' ): string
196+ {
197+ return strtolower (str_replace (['- ' ],$ sep ,static ::cleanString ($ string )));
198+ }
199+
200+ /**
201+ * clean string, remove special characters
202+ *
203+ * @param string $string
204+ * @return string
205+ */
206+ public static function cleanString (string $ string ): string
207+ {
208+ $ string = str_replace (' ' , '- ' , $ string ); // Replaces all spaces with hyphens.
209+ $ string = preg_replace ('/[^A-Za-z0-9\-]/ ' , '' , $ string ); // Removes special chars.
210+ return preg_replace ('/-+/ ' , '- ' , $ string ); // Replaces multiple hyphens with single one.
211+ }
212+
213+ /**
214+ * normalize an input to array type. If its a string, then it might be comma
215+ * separated value so we will make it to array.
216+ *
217+ * @author darryldecode <darrylfernandez.com>
218+ * @since v1.0
219+ *
220+ * @param $input
221+ *
222+ * @return array
223+ */
224+ public static function normalizeToArray ($ input ): array
225+ {
226+ if (is_array ($ input )) return $ input ;
227+ if (is_string ($ input )) return explode (', ' ,$ input );
228+ if (is_object ($ input )) return (array )$ input ;
229+ return [];
230+ }
231+
232+ /**
233+ * helper to get the real client IP address
234+ *
235+ * @author darryldecode <darrylfernandez.com>
236+ * @since v1.0
237+ * @return array|false|string
238+ */
239+ public static function getClientIpAddress ()
240+ {
241+ return getenv ('HTTP_CLIENT_IP ' )?:
242+ getenv ('HTTP_X_FORWARDED_FOR ' )?:
243+ getenv ('HTTP_X_FORWARDED ' )?:
244+ getenv ('HTTP_FORWARDED_FOR ' )?:
245+ getenv ('HTTP_FORWARDED ' )?:
246+ getenv ('REMOTE_ADDR ' );
247+ }
248+
249+ /**
250+ * @author darryldecode <darrylfernandez.com>
251+ * @since v1.0
252+ *
253+ * @param $key
254+ * @param $value
255+ *
256+ * @return bool
257+ */
258+ public static function updateEnv (string $ key ,string $ value )
259+ {
260+ $ envFile = app ()->environmentFilePath ();
261+ $ str = file_get_contents ($ envFile );
262+ $ oldValue = env ($ key );
263+ $ str = str_replace ("{$ key }= {$ oldValue }" , "{$ key }= {$ value }" , $ str );
264+ $ fp = fopen ($ envFile , 'w ' );
265+ fwrite ($ fp , $ str );
266+ fclose ($ fp );
267+ return true ;
268+ }
187269}
0 commit comments