55namespace Tamedevelopers \Support ;
66
77use Tamedevelopers \Support \Server ;
8+ use Tamedevelopers \Support \Slugify ;
89
910class Str{
1011
@@ -56,19 +57,25 @@ static public function last($array = null)
5657 * @param string $fromKey
5758 * @param string $toKey
5859 * @return array
60+ * - Returns an associative array
5961 */
60- static public function changeKeysFromArray ($ data = [] , $ fromKey , $ toKey )
62+ static public function changeKeysFromArray ($ data , $ fromKey , $ toKey )
6163 {
6264 // always convert to an array
6365 $ data = Server::toArray ($ data );
66+
67+ // If $data is an associative array, convert it to a numeric array
68+ if (self ::isAssoc ($ data )) {
69+ $ data = [$ data ];
70+ }
6471
6572 // If you don't want to modify the original array and create a new one without 'id' columns:
66- return array_map (function ($ data ) use ($ fromKey , $ toKey ) {
67- if (isset ($ data [$ fromKey ])) {
68- $ data [$ toKey ] = $ data [$ fromKey ];
69- unset($ data [$ fromKey ]);
73+ return array_map (function ($ item ) use ($ fromKey , $ toKey ) {
74+ if (isset ($ item [$ fromKey ])) {
75+ $ item [$ toKey ] = $ item [$ fromKey ];
76+ unset($ item [$ fromKey ]);
7077 }
71- return $ data ;
78+ return $ item ;
7279 }, $ data );
7380 }
7481
@@ -78,21 +85,27 @@ static public function changeKeysFromArray($data = [], $fromKey, $toKey)
7885 * @param array $data
7986 * @param mixed $keys
8087 * @return array
88+ * - Returns an associative array
8189 */
82- static public function removeKeysFromArray ($ data = [] , ...$ keys )
90+ static public function removeKeysFromArray ($ data , ...$ keys )
8391 {
8492 // always convert to an array
8593 $ data = Server::toArray ($ data );
94+
95+ // If $data is an associative array, convert it to a numeric array
96+ if (self ::isAssoc ($ data )) {
97+ $ data = [$ data ];
98+ }
8699
87100 // If you don't want to modify the original array and create a new one without 'id' columns:
88- return array_map (function ($ data ) use ($ keys ) {
101+ return array_map (function ($ items ) use ($ keys ) {
89102 $ keys = self ::flattenValue ($ keys );
90103 foreach ($ keys as $ key ){
91- if (isset ($ data [$ key ])){
92- unset($ data [$ key ]);
104+ if (isset ($ items [$ key ])){
105+ unset($ items [$ key ]);
93106 }
94107 }
95- return $ data ;
108+ return $ items ;
96109 }, $ data );
97110 }
98111
@@ -323,6 +336,33 @@ static public function endsWith(string $haystack, string $needle)
323336 return substr ($ haystack , -strlen ($ needle )) === $ needle ;
324337 }
325338
339+ /**
340+ * Generate a string with a specified number of random words.
341+ *
342+ * @param int $wordCount
343+ * @param int $minLength
344+ * @param int $maxLength
345+ * @return string
346+ */
347+ static public function randomWords (int $ wordCount , int $ minLength = 3 , int $ maxLength = 10 )
348+ {
349+ $ words = [];
350+ $ characters = 'abcdefghijklmnopqrstuvwxyz ' ;
351+
352+ for ($ i = 0 ; $ i < $ wordCount ; $ i ++) {
353+ $ length = rand ($ minLength , $ maxLength );
354+ $ word = '' ;
355+
356+ for ($ j = 0 ; $ j < $ length ; $ j ++) {
357+ $ word .= $ characters [rand (0 , strlen ($ characters ) - 1 )];
358+ }
359+
360+ $ words [] = $ word ;
361+ }
362+
363+ return implode (' ' , $ words );
364+ }
365+
326366 /**
327367 * Generate a random string of a given length.
328368 *
@@ -343,22 +383,39 @@ static public function random(int $length = 16)
343383 return $ randomString ;
344384 }
345385
386+ /**
387+ * Generate a UUID (Universally Unique Identifier).
388+ *
389+ * @return string
390+ */
391+ public static function uuid ()
392+ {
393+ return sprintf (
394+ '%04x%04x-%04x-%04x-%04x-%04x%04x%04x ' ,
395+ mt_rand (0 , 0xffff ),
396+ mt_rand (0 , 0xffff ),
397+ mt_rand (0 , 0xffff ),
398+ mt_rand (0 , 0x0fff ) | 0x4000 ,
399+ mt_rand (0 , 0x3fff ) | 0x8000 ,
400+ mt_rand (0 , 0xffff ),
401+ mt_rand (0 , 0xffff ),
402+ mt_rand (0 , 0xffff )
403+ );
404+ }
405+
346406 /**
347407 * Convert a string to snake_case.
348408 *
349409 * @param string $value
350410 * @param string $delimiter
351411 * @return string
352412 */
353- static public function snakeCase (string $ value , string $ delimiter = '_ ' )
413+ static public function snake (string $ value , string $ delimiter = '_ ' )
354414 {
355415 // Replace spaces with delimiter and capitalize each word
356416 $ value = preg_replace ('/\s+/u ' , $ delimiter , ucwords ($ value ));
357417
358- // Convert to lowercase and insert delimiter before uppercase letters
359- $ value = self ::lower (preg_replace ('/(.)(?=[A-Z])/u ' , '$1 ' . $ delimiter , $ value ));
360-
361- return $ value ;
418+ return self ::lower ($ value );
362419 }
363420
364421 /**
@@ -367,7 +424,7 @@ static public function snakeCase(string $value, string $delimiter = '_')
367424 * @param string $value
368425 * @return string
369426 */
370- static public function camelCase (string $ value )
427+ static public function camel (string $ value )
371428 {
372429 // Remove special characters and spaces
373430 $ value = preg_replace ('/[^a-z0-9]+/i ' , ' ' , $ value );
@@ -403,7 +460,7 @@ static public function slug(string $value, string $separator = '-')
403460 * @param string $value
404461 * @return string
405462 */
406- static public function studlyCase (string $ value )
463+ static public function studly (string $ value )
407464 {
408465 $ value = ucwords (preg_replace ('/[\s_]+/ ' , ' ' , $ value ));
409466 $ value = str_replace (' ' , '' , $ value );
@@ -417,12 +474,11 @@ static public function studlyCase(string $value)
417474 * @param string $value
418475 * @return string
419476 */
420- static public function kebabCase (string $ value )
477+ static public function kebab (string $ value )
421478 {
422- $ value = preg_replace ('/\s+/u ' , '- ' , $ value );
423- $ value = self ::lower (preg_replace ('/(.)(?=[A-Z])/u ' , '$1- ' , $ value ));
424-
425- return $ value ;
479+ return self ::lower (
480+ preg_replace ('/\s+/u ' , '- ' , $ value )
481+ );
426482 }
427483
428484 /**
@@ -431,7 +487,7 @@ static public function kebabCase(string $value)
431487 * @param string $value
432488 * @return string
433489 */
434- static public function titleCase (string $ value )
490+ static public function title (string $ value )
435491 {
436492 return ucwords (self ::lower ($ value ));
437493 }
@@ -440,21 +496,118 @@ static public function titleCase(string $value)
440496 * Convert a string to a URL-friendly slug.
441497 *
442498 * @param string $value
499+ * @param string $language
443500 * @param string $separator
501+ * @param bool $case
444502 * @return string
445503 */
446- static public function slugify (string $ value , string $ separator = '- ' )
504+ static public function slugify (string $ value , $ language = null , string $ separator = '- ' , $ case = true )
447505 {
448- // Transliterate special characters to ASCII
449- $ value = transliterator_transliterate ('Any-Latin; Latin-ASCII; Lower() ' , $ value );
506+ return Slugify::slug (
507+ $ value , $ language , $ separator , $ case
508+ );
509+ }
450510
451- // Replace non-alphanumeric characters with the separator
452- $ value = preg_replace ('/[^a-z0-9-]+/ ' , $ separator , $ value );
511+ /**
512+ * Masks characters in a string.
513+ *
514+ * @param string|null $str
515+ * - The string to be masked.
516+ *
517+ * @param int $length
518+ * - The desired length of the masked string. Default is 4.
519+ *
520+ * @param string|null $position
521+ * - The position to apply the mask: 'left', 'middle' or 'center', 'right'. Default is 'right'.
522+ *
523+ * @param string $mask
524+ * - The character used for masking. Default is '*'.
525+ *
526+ * @return string
527+ * - The masked string.
528+ */
529+ static public function mask ($ str = null , ?int $ length = 4 , ?string $ position = null , ?string $ mask = '* ' )
530+ {
531+ return Tame::mask (
532+ $ str , $ length , $ position , $ mask
533+ );
534+ }
453535
454- // Remove leading and trailing separators
455- $ value = trim ($ value , $ separator );
536+ /**
537+ * Shorten String to Given Limit
538+ *
539+ * @param mixed $string
540+ *
541+ * @param mixed $limit
542+ * [optional] Default 50
543+ *
544+ * @param mixed $replacer
545+ * [optional] Default ...
546+ *
547+ * @return string
548+ */
549+ static public function shorten ($ string = null , $ limit = 50 , $ replacer = '... ' )
550+ {
551+ return Tame::shortenString (
552+ $ string , $ limit , $ replacer
553+ );
554+ }
456555
457- return $ value ;
556+ /**
557+ * Decode entity html strings
558+ *
559+ * @param string|null $string
560+ * @return string
561+ */
562+ static public function html ($ string = null )
563+ {
564+ return Tame::html ($ string );
565+ }
566+
567+ /**
568+ * Convert string to clean text without html tags
569+ *
570+ * @param string|null $string
571+ *
572+ * @return string
573+ * - strip all tags from string content
574+ */
575+ static public function text ($ string = null )
576+ {
577+ return Tame::text ($ string );
578+ }
579+
580+ /**
581+ * Hash String
582+ *
583+ * @param string|null $string
584+ * @param int $length
585+ * @param string $type
586+ * @param int $interation
587+ * @return void
588+ */
589+ static public function hash ($ string = null , $ length = 100 , $ type = 'sha256 ' , $ interation = 100 )
590+ {
591+ return Tame::stringHash (
592+ $ string , $ length , $ type , $ interation
593+ );
594+ }
595+
596+ /**
597+ * Clean phone string
598+ *
599+ * @param string|null $phone
600+ *
601+ * @param bool $allow --- Default is true
602+ * - [optional] to allow int format `+` (before number)
603+ *
604+ * @return string
605+ */
606+ static public function phone ($ phone = null , ?bool $ allow = true )
607+ {
608+ return Tame::cleanPhoneNumber (
609+ $ phone , $ allow
610+ );
458611 }
459612
460613 /**
@@ -558,40 +711,13 @@ static public function removeWhitespace(string $value)
558711 return preg_replace ('/\s+/ ' , '' , $ value );
559712 }
560713
561- /**
562- * Generate a string with a specified number of random words.
563- *
564- * @param int $wordCount
565- * @param int $minLength
566- * @param int $maxLength
567- * @return string
568- */
569- static public function generateRandomWords (int $ wordCount , int $ minLength = 3 , int $ maxLength = 10 )
570- {
571- $ words = [];
572- $ characters = 'abcdefghijklmnopqrstuvwxyz ' ;
573-
574- for ($ i = 0 ; $ i < $ wordCount ; $ i ++) {
575- $ length = rand ($ minLength , $ maxLength );
576- $ word = '' ;
577-
578- for ($ j = 0 ; $ j < $ length ; $ j ++) {
579- $ word .= $ characters [rand (0 , strlen ($ characters ) - 1 )];
580- }
581-
582- $ words [] = $ word ;
583- }
584-
585- return implode (' ' , $ words );
586- }
587-
588714 /**
589715 * Get the file extension from a filename or path.
590716 *
591717 * @param string $filename
592718 * @return string|null
593719 */
594- static public function getFileExtension (string $ filename )
720+ static public function fileExtension (string $ filename )
595721 {
596722 $ extension = pathinfo ($ filename , PATHINFO_EXTENSION );
597723
@@ -684,4 +810,15 @@ static public function padString(string $value, int $length, string $padChar = '
684810 return str_pad ($ value , $ length , $ padChar , $ padType );
685811 }
686812
813+ /**
814+ * Check if an array is associative.
815+ *
816+ * @param array $array
817+ * @return bool
818+ */
819+ static private function isAssoc (array $ array )
820+ {
821+ return array_keys ($ array ) !== range (0 , count ($ array ) - 1 );
822+ }
823+
687824}
0 commit comments