1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AcquiroPay\Helpers; |
6
|
|
|
|
7
|
|
|
class Str |
8
|
|
|
{ |
9
|
|
|
private static $snakeCache = []; |
10
|
|
|
private static $camelCache = []; |
11
|
|
|
private static $studlyCache = []; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Get the class "basename" of the given object / class. |
15
|
|
|
* |
16
|
|
|
* @param string|object $class |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
public static function classBaseName($class): string |
20
|
|
|
{ |
21
|
|
|
$class = is_object($class) ? get_class($class) : $class; |
22
|
|
|
|
23
|
|
|
return basename(str_replace('\\', '/', $class)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Convert the given string to lower-case. |
28
|
|
|
* |
29
|
|
|
* @param string $value |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public static function lower($value): string |
33
|
|
|
{ |
34
|
|
|
return mb_strtolower($value, 'UTF-8'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Convert a value to studly caps case. |
39
|
|
|
* |
40
|
|
|
* @param string $value |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public static function studly($value) |
44
|
|
|
{ |
45
|
|
|
$key = $value; |
46
|
|
|
if (isset(static::$studlyCache[$key])) { |
|
|
|
|
47
|
|
|
return static::$studlyCache[$key]; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
$value = ucwords(str_replace(['-', '_'], ' ', $value)); |
50
|
|
|
|
51
|
|
|
return static::$studlyCache[$key] = str_replace(' ', '', $value); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Convert a value to camel case. |
56
|
|
|
* |
57
|
|
|
* @param string $value |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public static function camel($value): string |
61
|
|
|
{ |
62
|
|
|
if (isset(static::$camelCache[$value])) { |
|
|
|
|
63
|
|
|
return static::$camelCache[$value]; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return static::$camelCache[$value] = lcfirst(static::studly($value)); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Convert a string to snake case. |
71
|
|
|
* |
72
|
|
|
* @param string $value |
73
|
|
|
* @param string $delimiter |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public static function snake($value, $delimiter = '_'): string |
77
|
|
|
{ |
78
|
|
|
$key = $value; |
79
|
|
|
if (isset(static::$snakeCache[$key][$delimiter])) { |
|
|
|
|
80
|
|
|
return static::$snakeCache[$key][$delimiter]; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
if (!ctype_lower($value)) { |
83
|
|
|
$value = preg_replace('/\s+/u', '', ucwords($value)); |
84
|
|
|
$value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return static::$snakeCache[$key][$delimiter] = $value; |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: