1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Swis\JsonApi\Client; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @internal |
9
|
|
|
*/ |
10
|
|
|
class Util |
11
|
|
|
{ |
12
|
|
|
private static array $camelCache = []; |
13
|
|
|
|
14
|
|
|
private static array $snakeCache = []; |
15
|
|
|
|
16
|
|
|
private static array $studlyCache = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Convert the given string to lower-case. |
20
|
|
|
* |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
48 |
|
public static function stringLower(string $value) |
25
|
|
|
{ |
26
|
48 |
|
return mb_strtolower($value, 'UTF-8'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Convert a string to snake case. |
31
|
|
|
* |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
204 |
|
public static function stringSnake(string $value, string $delimiter = '_') |
36
|
|
|
{ |
37
|
204 |
|
$key = $value; |
38
|
|
|
|
39
|
204 |
|
if (isset(self::$snakeCache[$key][$delimiter])) { |
40
|
180 |
|
return self::$snakeCache[$key][$delimiter]; |
41
|
|
|
} |
42
|
|
|
|
43
|
56 |
|
if (! ctype_lower($value)) { |
44
|
44 |
|
$value = preg_replace('/\s+/u', '', ucwords($value)); |
45
|
|
|
|
46
|
44 |
|
$value = static::stringLower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value)); |
47
|
|
|
} |
48
|
|
|
|
49
|
56 |
|
return self::$snakeCache[$key][$delimiter] = $value; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Convert a value to studly caps case. |
54
|
|
|
* |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
312 |
|
public static function stringStudly(string $value) |
59
|
|
|
{ |
60
|
312 |
|
$key = $value; |
61
|
|
|
|
62
|
312 |
|
if (isset(self::$studlyCache[$key])) { |
63
|
260 |
|
return self::$studlyCache[$key]; |
64
|
|
|
} |
65
|
|
|
|
66
|
100 |
|
$value = ucwords(str_replace(['-', '_'], ' ', $value)); |
67
|
|
|
|
68
|
100 |
|
return self::$studlyCache[$key] = str_replace(' ', '', $value); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Convert a value to camel case. |
73
|
|
|
* |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
248 |
|
public static function stringCamel(string $value) |
78
|
|
|
{ |
79
|
248 |
|
if (isset(self::$camelCache[$value])) { |
80
|
216 |
|
return self::$camelCache[$value]; |
81
|
|
|
} |
82
|
|
|
|
83
|
44 |
|
return self::$camelCache[$value] = lcfirst(static::stringStudly($value)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Remove array element from array. |
88
|
|
|
* |
89
|
|
|
* @param mixed $keys |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
108 |
|
public static function arrayExcept(array $array, $keys) |
93
|
|
|
{ |
94
|
108 |
|
if (is_string($keys)) { |
95
|
28 |
|
$keys = [$keys]; |
96
|
|
|
} |
97
|
|
|
|
98
|
108 |
|
if (count($keys) === 0) { |
99
|
48 |
|
return $array; |
100
|
|
|
} |
101
|
|
|
|
102
|
80 |
|
foreach ($keys as $key) { |
103
|
80 |
|
if (array_key_exists($key, $array)) { |
104
|
80 |
|
unset($array[$key]); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
80 |
|
return $array; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|