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