4
4
5
5
namespace Php \Support \Candidates \Helpers ;
6
6
7
- use function mb_strtolower ;
8
- use function preg_replace ;
9
-
10
7
/**
11
8
* Class Str
12
9
*
13
10
* @package Php\Support\Helpers
14
11
*/
15
12
class Str
16
13
{
17
- /**
18
- * The cache of studly-cased words.
19
- *
20
- * @var array
21
- */
22
- protected static $ studlyCache = [];
23
- /**
24
- * The cache of snake-cased words.
25
- *
26
- * @var array
27
- */
28
- protected static $ snakeCache = [];
29
14
30
15
/**
31
16
* Replace templates into string
@@ -41,50 +26,4 @@ public static function stringReplaceByTemplate(string $str, array $replace)
41
26
{
42
27
return str_replace (array_keys ($ replace ), array_values ($ replace ), $ str );
43
28
}
44
-
45
- /**
46
- * Convert a value to studly caps case.
47
- *
48
- * @param string $value
49
- *
50
- * @return string
51
- */
52
- public static function studly ($ value ): string
53
- {
54
- $ key = $ value ;
55
-
56
- if (isset (static ::$ studlyCache [$ key ])) {
57
- return static ::$ studlyCache [$ key ];
58
- }
59
-
60
- $ value = ucwords (str_replace (['- ' , '_ ' ], ' ' , $ value ));
61
-
62
- return static ::$ studlyCache [$ key ] = str_replace (' ' , '' , $ value );
63
- }
64
-
65
- /**
66
- * Convert a string to snake case.
67
- *
68
- * @param string $value
69
- * @param string $delimiter
70
- *
71
- * @return string
72
- */
73
- public static function snake ($ value , $ delimiter = '_ ' ): string
74
- {
75
- $ key = $ value ;
76
-
77
- if (isset (static ::$ snakeCache [$ key ][$ delimiter ])) {
78
- return static ::$ snakeCache [$ key ][$ delimiter ];
79
- }
80
-
81
- if (!ctype_lower ($ value )) {
82
- $ value = preg_replace ('/\s+/u ' , '' , ucwords ($ value ));
83
-
84
- $ value = preg_replace ('/(.)(?=[A-Z])/u ' , '$1 ' . $ delimiter , (string )$ value );
85
- $ value = mb_strtolower ((string )$ value , 'UTF-8 ' );
86
- }
87
-
88
- return static ::$ snakeCache [$ key ][$ delimiter ] = $ value ;
89
- }
90
29
}
0 commit comments