1010namespace Toolkit \Stdlib \Str \Traits ;
1111
1212use Toolkit \Stdlib \Helper \DataHelper ;
13+ use Toolkit \Stdlib \Str ;
1314use function array_map ;
1415use function array_values ;
1516use function count ;
1617use function explode ;
18+ use function is_numeric ;
1719use function mb_convert_encoding ;
1820use function mb_convert_variables ;
1921use function mb_detect_encoding ;
2022use function mb_strwidth ;
2123use function preg_split ;
24+ use function str_contains ;
2225use function str_pad ;
2326use function str_split ;
27+ use function strlen ;
2428use function strpos ;
29+ use function substr ;
2530use function trim ;
2631use const PREG_SPLIT_NO_EMPTY ;
2732
@@ -46,25 +51,6 @@ public static function toBool(string $str): bool
4651 /// split to array
4752 ////////////////////////////////////////////////////////////////////////
4853
49- /**
50- * var_dump(str2array('34,56,678, 678, 89, '));
51- *
52- * @param string $str
53- * @param string $sep
54- *
55- * @return array
56- */
57- public static function str2array (string $ str , string $ sep = ', ' ): array
58- {
59- $ str = trim ($ str , "$ sep " );
60-
61- if (!$ str ) {
62- return [];
63- }
64-
65- return preg_split ("/\s* $ sep\s*/ " , $ str , -1 , PREG_SPLIT_NO_EMPTY );
66- }
67-
6854 /**
6955 * @param string $string
7056 * @param string $delimiter
@@ -78,15 +64,15 @@ public static function toInts(string $string, string $delimiter = ',', int $limi
7864 }
7965
8066 /**
81- * @param string $string
67+ * @param string $str
8268 * @param string $delimiter
8369 * @param int $limit
8470 *
8571 * @return array
8672 */
87- public static function str2ints (string $ string , string $ delimiter = ', ' , int $ limit = 0 ): array
73+ public static function str2ints (string $ str , string $ delimiter = ', ' , int $ limit = 0 ): array
8874 {
89- $ values = self ::toArray ( $ string , $ delimiter , $ limit );
75+ $ values = self ::splitTrimFiltered ( $ str , $ delimiter , $ limit );
9076
9177 return array_map ('intval ' , $ values );
9278 }
@@ -100,21 +86,7 @@ public static function str2ints(string $string, string $delimiter = ',', int $li
10086 */
10187 public static function toArray (string $ str , string $ delimiter = ', ' , int $ limit = 0 ): array
10288 {
103- $ str = trim ($ str );
104- if ($ str === '' ) {
105- return [];
106- }
107-
108- $ values = [];
109- $ rawList = $ limit < 1 ? explode ($ delimiter , $ str ) : explode ($ delimiter , $ str , $ limit );
110-
111- foreach ($ rawList as $ val ) {
112- if (($ val = trim ($ val )) !== '' ) {
113- $ values [] = $ val ;
114- }
115- }
116-
117- return $ values ;
89+ return self ::splitTrimFiltered ($ str , $ delimiter , $ limit );;
11890 }
11991
12092 /**
@@ -128,7 +100,26 @@ public static function toArray(string $str, string $delimiter = ',', int $limit
128100 */
129101 public static function explode (string $ str , string $ separator = '. ' , int $ limit = 0 ): array
130102 {
131- return static ::split2Array ($ str , $ separator , $ limit );
103+ return self ::splitTrimFiltered ($ str , $ separator , $ limit );
104+ }
105+
106+ /**
107+ * split to array.
108+ *
109+ * @param string $str
110+ * @param string $sep
111+ * @param int $limit
112+ *
113+ * @return array
114+ */
115+ public static function str2array (string $ str , string $ sep = ', ' , int $ limit = 0 ): array
116+ {
117+ // $str = trim($str, "$sep ");
118+ // if (!$str) {
119+ // return [];
120+ // }
121+ // return preg_split("/\s*$sep\s*/", $str, -1, PREG_SPLIT_NO_EMPTY);
122+ return self ::splitTrimFiltered ($ str , $ sep , $ limit );
132123 }
133124
134125 /**
@@ -141,6 +132,20 @@ public static function explode(string $str, string $separator = '.', int $limit
141132 * @return array
142133 */
143134 public static function split2Array (string $ str , string $ delimiter = ', ' , int $ limit = 0 ): array
135+ {
136+ return self ::splitTrimFiltered ($ str , $ delimiter , $ limit );
137+ }
138+
139+ /**
140+ * Like explode, but will trim each item and filter empty item.
141+ *
142+ * @param string $str
143+ * @param string $delimiter
144+ * @param int $limit
145+ *
146+ * @return array
147+ */
148+ public static function splitTrimFiltered (string $ str , string $ delimiter = ', ' , int $ limit = 0 ): array
144149 {
145150 if (!$ str = trim ($ str )) {
146151 return [];
@@ -150,16 +155,14 @@ public static function split2Array(string $str, string $delimiter = ',', int $li
150155 return [$ str ];
151156 }
152157
153- if ($ limit < 1 ) {
154- $ list = explode ($ delimiter , $ str );
155- } else {
156- $ list = explode ($ delimiter , $ str , $ limit );
157- }
158+ $ list = $ limit < 1 ? explode ($ delimiter , $ str ) : explode ($ delimiter , $ str , $ limit );
158159
159160 return array_values (array_filter (array_map ('trim ' , $ list ), 'strlen ' ));
160161 }
161162
162163 /**
164+ * Like explode, but will trim each item.
165+ *
163166 * @param string $str
164167 * @param string $delimiter
165168 * @param int $limit
@@ -178,6 +181,27 @@ public static function splitTrimmed(string $str, string $delimiter = ',', int $l
178181 return array_map ('trim ' , $ list );
179182 }
180183
184+ /**
185+ * @param string $str
186+ * @param string $delimiter
187+ *
188+ * @return array
189+ */
190+ public static function splitTypedList (string $ str , string $ delimiter = ', ' ): array
191+ {
192+ if (!$ str ) {
193+ return [];
194+ }
195+
196+ $ arr = self ::splitTrimFiltered ($ str , $ delimiter );
197+ foreach ($ arr as &$ val ) {
198+ if (is_numeric ($ val ) && strlen ($ val ) < 11 ) {
199+ $ val = str_contains ($ val , '. ' ) ? (float )$ val : (int )$ val ;
200+ }
201+ }
202+
203+ return $ arr ;
204+ }
181205 /**
182206 * @param string $string
183207 * @param int $width
0 commit comments