55namespace Tamedevelopers \Support ;
66
77use Tamedevelopers \Support \Server ;
8- use Tamedevelopers \Support \Slugify ;
98
109class Str{
1110
@@ -57,25 +56,19 @@ static public function last($array = null)
5756 * @param string $fromKey
5857 * @param string $toKey
5958 * @return array
60- * - Returns an associative array
6159 */
6260 static public function changeKeysFromArray ($ data , $ fromKey , $ toKey )
6361 {
6462 // always convert to an array
6563 $ 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- }
7164
7265 // If you don't want to modify the original array and create a new one without 'id' columns:
73- return array_map (function ($ item ) use ($ fromKey , $ toKey ) {
74- if (isset ($ item [$ fromKey ])) {
75- $ item [$ toKey ] = $ item [$ fromKey ];
76- unset($ item [$ fromKey ]);
66+ return array_map (function ($ data ) use ($ fromKey , $ toKey ) {
67+ if (isset ($ data [$ fromKey ])) {
68+ $ data [$ toKey ] = $ data [$ fromKey ];
69+ unset($ data [$ fromKey ]);
7770 }
78- return $ item ;
71+ return $ data ;
7972 }, $ data );
8073 }
8174
@@ -85,27 +78,21 @@ static public function changeKeysFromArray($data, $fromKey, $toKey)
8578 * @param array $data
8679 * @param mixed $keys
8780 * @return array
88- * - Returns an associative array
8981 */
9082 static public function removeKeysFromArray ($ data , ...$ keys )
9183 {
9284 // always convert to an array
9385 $ 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- }
9986
10087 // If you don't want to modify the original array and create a new one without 'id' columns:
101- return array_map (function ($ items ) use ($ keys ) {
88+ return array_map (function ($ data ) use ($ keys ) {
10289 $ keys = self ::flattenValue ($ keys );
10390 foreach ($ keys as $ key ){
104- if (isset ($ items [$ key ])){
105- unset($ items [$ key ]);
91+ if (isset ($ data [$ key ])){
92+ unset($ data [$ key ]);
10693 }
10794 }
108- return $ items ;
95+ return $ data ;
10996 }, $ data );
11097 }
11198
@@ -336,33 +323,6 @@ static public function endsWith(string $haystack, string $needle)
336323 return substr ($ haystack , -strlen ($ needle )) === $ needle ;
337324 }
338325
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-
366326 /**
367327 * Generate a random string of a given length.
368328 *
@@ -496,118 +456,26 @@ static public function title(string $value)
496456 * Convert a string to a URL-friendly slug.
497457 *
498458 * @param string $value
499- * @param string $language
500459 * @param string $separator
501- * @param bool $case
502- * @return string
503- */
504- static public function slugify (string $ value , $ language = null , string $ separator = '- ' , $ case = true )
505- {
506- return Slugify::slug (
507- $ value , $ language , $ separator , $ case
508- );
509- }
510-
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- }
535-
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- }
555-
556- /**
557- * Decode entity html strings
558- *
559- * @param string|null $string
560460 * @return string
561461 */
562- static public function html ( $ string = null )
462+ static public function slugify ( string $ value , string $ separator = ' - ' )
563463 {
564- return Tame::html ($ string );
565- }
464+ // Try to transliterate using intl extension
465+ if (function_exists ('transliterator_transliterate ' )) {
466+ $ value = transliterator_transliterate ('Any-Latin; Latin-ASCII; Lower() ' , $ value );
467+ } else {
468+ // Fallback to iconv for transliteration if intl is not available
469+ $ value = iconv ('UTF-8 ' , 'ASCII//TRANSLIT//IGNORE ' , $ value );
470+ }
566471
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- }
472+ // Replace non-alphanumeric characters with the separator
473+ $ value = preg_replace ('/[^a-z0-9-]+/ ' , $ separator , $ value );
579474
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- }
475+ // Remove leading and trailing separators
476+ $ value = trim ($ value , $ separator );
595477
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- );
478+ return $ value ;
611479 }
612480
613481 /**
@@ -711,13 +579,40 @@ static public function removeWhitespace(string $value)
711579 return preg_replace ('/\s+/ ' , '' , $ value );
712580 }
713581
582+ /**
583+ * Generate a string with a specified number of random words.
584+ *
585+ * @param int $wordCount
586+ * @param int $minLength
587+ * @param int $maxLength
588+ * @return string
589+ */
590+ static public function generateRandomWords (int $ wordCount , int $ minLength = 3 , int $ maxLength = 10 )
591+ {
592+ $ words = [];
593+ $ characters = 'abcdefghijklmnopqrstuvwxyz ' ;
594+
595+ for ($ i = 0 ; $ i < $ wordCount ; $ i ++) {
596+ $ length = rand ($ minLength , $ maxLength );
597+ $ word = '' ;
598+
599+ for ($ j = 0 ; $ j < $ length ; $ j ++) {
600+ $ word .= $ characters [rand (0 , strlen ($ characters ) - 1 )];
601+ }
602+
603+ $ words [] = $ word ;
604+ }
605+
606+ return implode (' ' , $ words );
607+ }
608+
714609 /**
715610 * Get the file extension from a filename or path.
716611 *
717612 * @param string $filename
718613 * @return string|null
719614 */
720- static public function fileExtension (string $ filename )
615+ static public function getFileExtension (string $ filename )
721616 {
722617 $ extension = pathinfo ($ filename , PATHINFO_EXTENSION );
723618
@@ -810,15 +705,4 @@ static public function padString(string $value, int $length, string $padChar = '
810705 return str_pad ($ value , $ length , $ padChar , $ padType );
811706 }
812707
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-
824708}
0 commit comments