1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saltwater; |
4
|
|
|
|
5
|
|
|
use Saltwater\Server as S; |
6
|
|
|
|
7
|
|
|
class Utils |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Convert snake_case to CamelCase |
11
|
|
|
* |
12
|
|
|
* @param string $string |
13
|
|
|
* |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
|
|
public static function snakeToCamelCase($string) |
17
|
|
|
{ |
18
|
|
|
return self::camelCaseSpaced(str_replace('_', ' ', $string)); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Convert dashed-case to CamelCase |
23
|
|
|
* |
24
|
|
|
* @param string $string |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public static function dashedToCamelCase($string) |
29
|
|
|
{ |
30
|
|
|
return self::camelCaseSpaced(str_replace('-', ' ', $string)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Convert a camel cased Object into a CamelCasedObject |
35
|
|
|
* |
36
|
|
|
* @param string $string |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public static function camelCaseSpaced($string) |
41
|
|
|
{ |
42
|
|
|
return str_replace(' ', '', ucwords($string)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Convert a CamelCasedObject into a dashed-case-object |
47
|
|
|
* |
48
|
|
|
* @param string $string |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public static function camelTodashed($string) |
53
|
|
|
{ |
54
|
|
|
return strtolower( |
55
|
|
|
preg_replace('/([a-zA-Z])(?=[A-Z])/', '$1-', $string) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Convert a /Namespaced/Class to a dashed-class |
61
|
|
|
* |
62
|
|
|
* @param string $string |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public static function namespacedClassToDashed($string) |
67
|
|
|
{ |
68
|
|
|
$array = explode('\\', $string); |
69
|
|
|
|
70
|
|
|
return self::camelTodashed(array_pop($array)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Convert a /Namespaced/Class to a dashed-class |
75
|
|
|
* |
76
|
|
|
* @param string $string |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public static function namespaceFromClass($string) |
81
|
|
|
{ |
82
|
|
|
$array = explode('\\', $string); |
83
|
|
|
|
84
|
|
|
array_pop($array); |
85
|
|
|
|
86
|
|
|
return implode('\\', $array); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public static function className() |
93
|
|
|
{ |
94
|
|
|
$args = array(); |
95
|
|
|
foreach (func_get_args() as $arg) { |
96
|
|
|
if (strpos($arg, '_')) { |
97
|
|
|
$args[] = self::snakeToCamelCase($arg); |
98
|
|
|
} else { |
99
|
|
|
$args[] = self::dashedToCamelCase($arg); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return implode('\\', $args); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string|object $input |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public static function explodeClass($input) |
112
|
|
|
{ |
113
|
|
|
if (is_object($input)) { |
114
|
|
|
$input = get_class($input); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return explode('\\', $input); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $class |
122
|
|
|
* |
123
|
|
|
* @return array Class Name, Salt and Namespace |
124
|
|
|
*/ |
125
|
|
|
public static function extractFromClass($class) |
126
|
|
|
{ |
127
|
|
|
$class_name = array_pop($class); |
128
|
|
|
|
129
|
|
|
$salt = strtolower(array_pop($class) . '.' . $class_name); |
130
|
|
|
|
131
|
|
|
$namespace = implode('\\', $class); |
132
|
|
|
|
133
|
|
|
return array($class_name, $salt, $namespace); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Read a JSON file and return its content |
138
|
|
|
* |
139
|
|
|
* @param string $path Path to read from |
140
|
|
|
* @param bool $associative Convert objects to associative arrays |
141
|
|
|
* |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
public static function getJSON($path, $associative = false) |
145
|
|
|
{ |
146
|
|
|
return json_decode(file_get_contents($path), $associative); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Store any data as JSON to a file |
151
|
|
|
* |
152
|
|
|
* @param string $path Path to where the file should be stored |
153
|
|
|
* @param string $content Content to be stored |
154
|
|
|
* |
155
|
|
|
* @return int |
156
|
|
|
*/ |
157
|
|
|
public static function storeJSON($path, $content) |
158
|
|
|
{ |
159
|
|
|
if (S::$env['gt54']) { |
160
|
|
|
return file_put_contents( |
161
|
|
|
$path, |
162
|
|
|
json_encode( |
163
|
|
|
$content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return file_put_contents($path, json_encode($content)); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|