1212
1313namespace PhpStringHelpers \utility ;
1414
15+ use InvalidArgumentException ;
1516use PhpStringHelpers \exceptions \UrlIsNotValidException ;
1617use PhpStringHelpers \exceptions \FileDoesNotExistsException ;
1718use PhpStringHelpers \exceptions \LanguageFileIsNotArrayException ;
@@ -148,8 +149,8 @@ public function entitiesWrapper($data): string
148149 */
149150 public function toSlug (string $ string ): string
150151 {
151- $ string = preg_replace ('/[^\pL\d]+/i ' , '- ' , $ string );
152- return trim ( strtolower (preg_replace ( ' /-+/ ' , ' - ' , $ string)) , '- ' );
152+ $ string = preg_replace ('/[^\pL\d]+/u ' , '- ' , $ string );
153+ return strtolower (trim ( $ string , '- ' ) );
153154 }
154155
155156 /**
@@ -160,7 +161,7 @@ public function toSlug(string $string): string
160161 */
161162 public function rmAllBlanks (string $ string ): string
162163 {
163- return preg_replace ('/[\s] / ' , '' , $ string );
164+ return preg_replace ('/\s+ / ' , '' , $ string );
164165 }
165166
166167 /**
@@ -279,10 +280,10 @@ public function generatePin(int $length = 4): int
279280 {
280281 if ($ length < 4 || $ length > 12 ) return 0 ;
281282
282- $ min = str_pad ('1 ' , $ length , '0 ' );
283- $ max = str_pad ('9 ' , $ length , '9 ' );
283+ $ min = ( int ) str_pad ('1 ' , $ length , '0 ' );
284+ $ max = ( int ) str_pad ('9 ' , $ length , '9 ' );
284285
285- return mt_rand (( int ) $ min , ( int ) $ max );
286+ return mt_rand ($ min , $ max );
286287 }
287288
288289 /**
@@ -293,11 +294,12 @@ public function generatePin(int $length = 4): int
293294 */
294295 public function clearString (string $ data ): string
295296 {
296- $ data = stripslashes (trim ($ data ));
297+ $ data = trim ($ data );
298+ $ data = stripslashes ($ data );
297299 $ data = strip_tags ($ data );
298- $ data = $ this -> rmExtraBlank ( $ data );
299- $ data = html_entity_decode ( htmlentities ( $ data) );
300- $ data = $ this -> entitiesWrapper ( $ data );
300+ $ data = preg_replace ( ' /\s+/im ' , ' ' , $ data );
301+ $ data = htmlspecialchars ( $ data, ENT_QUOTES , ' UTF-8 ' );
302+
301303 return $ data ;
302304 }
303305
@@ -877,9 +879,132 @@ public function before(string $string, string $search): string
877879 * @param string $string
878880 * @return bool
879881 */
880- public function hasSpace (string $ string )
882+ public function hasSpace (string $ string ): bool
881883 {
882- return preg_match_all ('/\s/ ' , $ string ) ? true : false ;
884+ return (bool ) preg_match ('/\s/ ' , $ string );
885+ }
886+
887+ /**
888+ * a wrapper for FILTER_VALIDATE_EMAIL filter
889+ *
890+ * @param string $email
891+ * @return bool
892+ */
893+ public function isEmail (string $ email )
894+ {
895+ return (bool ) filter_var ($ email , FILTER_VALIDATE_EMAIL );
896+ }
897+
898+ /**
899+ * Detect Given Word Case
900+ *
901+ * @param mixed $word
902+ * @return string
903+ */
904+ public function detectCase (string $ word ): string
905+ {
906+ if ($ this ->isLowerCase ($ word )) return 'lowerCase ' ;
907+ if ($ this ->isUpperCase ($ word )) return 'upperCase ' ;
908+ if ($ this ->isTitleCase ($ word )) return 'titleCase ' ;
909+ if ($ this ->isSnakeCase ($ word )) return 'snakeCase ' ;
910+
911+ return 'mixedCase ' ;
912+ }
913+
914+ /**
915+ * Find Whether The Type Of A Given Word Is Lower Case
916+ *
917+ * @param string $word
918+ * @return boolean
919+ */
920+ public function isLowerCase (string $ word ): bool
921+ {
922+ return preg_match ('/^[a-z]+$/ ' , trim ($ word )) === 1 ;
923+ }
924+
925+ /**
926+ * Find Whether The Type Of A Given Word Is Upper Case
927+ *
928+ * @param string $word
929+ * @return boolean
930+ */
931+ public function isUpperCase (string $ word ): bool
932+ {
933+ return trim ($ word ) === strtoupper ($ word );
934+ }
935+
936+ /**
937+ * Whether The Type Of A Given Word Is Title Case
938+ *
939+ * @param string $word
940+ * @return boolean
941+ */
942+ public function isTitleCase (string $ word ): bool
943+ {
944+ $ pattern = '/^(?:\b\p{Lu}\p{Ll}*\b\s*)+$/ ' ;
945+ return preg_match ($ pattern , trim ($ word )) === 1 ;
946+ }
947+
948+ /**
949+ * whether the type of a given Word is SnakeCase
950+ *
951+ * @param string $word
952+ * @return boolean
953+ */
954+ public function isSnakeCase (string $ word ): bool
955+ {
956+ return preg_match ('/^[a-z]+(_[a-z]+)*$/ ' , trim ($ word )) === 1 ;
957+ }
958+
959+ /**
960+ * validateUserName
961+ *
962+ * @param string $userName
963+ * @return boolean
964+ */
965+ public function validateUserName (string $ userName , int $ min = 3 , int $ max = 20 ): bool
966+ {
967+ return (bool ) preg_match ('/^[A-Za-z0-9._-]{ ' . $ min . ', ' . $ max . '}$/i ' , $ userName );
968+ }
969+
970+ /**
971+ * Convert File Size To Human Readable Format.
972+ *
973+ * This function converts a given file size to a human-readable format,
974+ * based on the specified type (e.g., KB, MB, GB).
975+ *
976+ * @param int $size The file size to convert, in bytes.
977+ * @param string $type The type of file size to convert to (e.g., KB, MB, GB).
978+ * @return string The file size in human-readable format (e.g., "1.25 MB").
979+ * @throws InvalidArgumentException If the provided type is invalid.
980+ * @throws InvalidArgumentException If the provided size is negative.
981+ */
982+ public function humanFileSize (int $ size , string $ type = 'KB ' ): string
983+ {
984+ if ($ size < 0 ) {
985+ throw new InvalidArgumentException ('File size cannot be negative ' );
986+ }
987+
988+ if ($ size === 0 ) {
989+ return '0 B ' ;
990+ }
991+
992+ $ conversionFactors = [
993+ 'B ' => 1 ,
994+ 'KB ' => 1024 ,
995+ 'MB ' => 1024 * 1024 ,
996+ 'GB ' => 1024 * 1024 * 1024 ,
997+ 'TB ' => 1024 * 1024 * 1024 * 1024 ,
998+ 'PB ' => 1024 * 1024 * 1024 * 1024 * 1024 ,
999+ ];
1000+
1001+ if (!array_key_exists ($ type , $ conversionFactors )) {
1002+ throw new InvalidArgumentException ("Invalid type: $ type " );
1003+ }
1004+
1005+ $ humanReadableSize = $ size / $ conversionFactors [$ type ];
1006+
1007+ return number_format ($ humanReadableSize , 2 ) . ' ' . $ type ;
8831008 }
8841009
8851010 private function checkStringForRemoveOperation (string $ string , string $ word ): bool
0 commit comments