1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cecil/Cecil package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cecil; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
14
|
|
|
|
15
|
|
|
class Util |
16
|
|
|
{ |
17
|
|
|
/** @var Filesystem */ |
18
|
|
|
protected static $fs; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Return a Symfony\Component\Filesystem instance. |
22
|
|
|
* |
23
|
|
|
* @return Filesystem |
24
|
|
|
*/ |
25
|
|
|
public static function getFS(): Filesystem |
26
|
|
|
{ |
27
|
|
|
if (!self::$fs instanceof Filesystem) { |
28
|
|
|
self::$fs = new Filesystem(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return self::$fs; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Checks if a date is valid. |
36
|
|
|
* |
37
|
|
|
* @param string $date |
38
|
|
|
* @param string $format |
39
|
|
|
* |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public static function isDateValid(string $date, string $format = 'Y-m-d'): bool |
43
|
|
|
{ |
44
|
|
|
$d = \DateTime::createFromFormat($format, $date); |
45
|
|
|
|
46
|
|
|
return $d && $d->format($format) === $date; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Date to DateTime. |
51
|
|
|
* |
52
|
|
|
* @param mixed $date |
53
|
|
|
* |
54
|
|
|
* @return \DateTime |
55
|
|
|
*/ |
56
|
|
|
public static function dateToDatetime($date): \DateTime |
57
|
|
|
{ |
58
|
|
|
// DateTime |
59
|
|
|
if ($date instanceof \DateTime) { |
60
|
|
|
return $date; |
61
|
|
|
} |
62
|
|
|
// timestamp or 'AAAA-MM-DD' |
63
|
|
|
if (is_numeric($date)) { |
64
|
|
|
return (new \DateTime())->setTimestamp($date); |
65
|
|
|
} |
66
|
|
|
// string (ie: '01/01/2019', 'today') |
67
|
|
|
if (is_string($date)) { |
68
|
|
|
return new \DateTime($date); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Format a class name. |
74
|
|
|
* |
75
|
|
|
* @param \object $class |
76
|
|
|
* @param array $options |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public static function formatClassName($class, array $options = []): string |
81
|
|
|
{ |
82
|
|
|
$lowercase = false; |
83
|
|
|
extract($options); |
84
|
|
|
|
85
|
|
|
$className = substr(strrchr(get_class($class), '\\'), 1); |
86
|
|
|
if ($lowercase) { |
|
|
|
|
87
|
|
|
$className = strtolower($className); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $className; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Test if a string is an external URL or not. |
95
|
|
|
* |
96
|
|
|
* @param string $url |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public static function isExternalUrl(string $url): bool |
101
|
|
|
{ |
102
|
|
|
return (bool) preg_match('~^(?:f|ht)tps?://~i', $url); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Test if a remote file exists or not. |
107
|
|
|
* |
108
|
|
|
* @param string $remoteFile |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public static function isUrlFileExists(string $remoteFile): bool |
113
|
|
|
{ |
114
|
|
|
$handle = @fopen($remoteFile, 'r'); |
115
|
|
|
if (is_resource($handle)) { |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Converts an array of strings into a path. |
124
|
|
|
* |
125
|
|
|
* @param string $path |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public static function joinPath(string ...$path): string |
130
|
|
|
{ |
131
|
|
|
array_walk($path, function (&$value) { |
132
|
|
|
$value = str_replace('\\', '/', $value); |
133
|
|
|
$value = rtrim($value, '/'); |
134
|
|
|
}); |
135
|
|
|
|
136
|
|
|
return implode('/', $path); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Converts an array of strings into a system path. |
141
|
|
|
* |
142
|
|
|
* @param string $path |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public static function joinFile(string ...$path): string |
147
|
|
|
{ |
148
|
|
|
array_walk($path, function (&$value) { |
149
|
|
|
$value = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $value); |
150
|
|
|
$value = rtrim($value, DIRECTORY_SEPARATOR); |
151
|
|
|
}); |
152
|
|
|
|
153
|
|
|
return implode(DIRECTORY_SEPARATOR, $path); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Converts an array to a string. |
158
|
|
|
* |
159
|
|
|
* ie: [0 => 'A', 1 => B] become "0:A, 1:B" |
160
|
|
|
* |
161
|
|
|
* @param array $array |
162
|
|
|
* @param string $separator Separtor between the key and the value in the result string |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public static function arrayToString(array $array, string $separator = ':'): string |
167
|
|
|
{ |
168
|
|
|
$string = ''; |
169
|
|
|
|
170
|
|
|
foreach ($array as $key => $value) { |
171
|
|
|
$string .= sprintf('%s%s%s, ', $key, $separator, $value); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return substr($string, 0, -2); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Combines an array into a string. |
179
|
|
|
* |
180
|
|
|
* @param array $array |
181
|
|
|
* @param string $keyToKey The key that become the key of the new array |
182
|
|
|
* @param string $keyToValue The key that become the value of the new array |
183
|
|
|
* @param string $separator The separtor between the key and the value in the result string |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public static function combineArrayToString( |
188
|
|
|
array $array, |
189
|
|
|
string $keyToKey, |
190
|
|
|
string $keyToValue, |
191
|
|
|
string $separator = ':' |
192
|
|
|
): string { |
193
|
|
|
$string = ''; |
194
|
|
|
|
195
|
|
|
foreach ($array as $subArray) { |
196
|
|
|
$string .= sprintf('%s%s%s, ', $subArray[$keyToKey], $separator, $subArray[$keyToValue]); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return substr($string, 0, -2); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Simplified version file_get_contents() with error handler. |
204
|
|
|
* |
205
|
|
|
* @param string $filename |
206
|
|
|
* |
207
|
|
|
* @return string|false |
208
|
|
|
*/ |
209
|
|
|
public static function fileGetContents($filename, $use_include_path = false, $context = null, $offset = 0, $maxlen = null) |
210
|
|
|
{ |
211
|
|
|
set_error_handler( |
212
|
|
|
function ($severity, $message, $file, $line) { |
213
|
|
|
throw new \ErrorException($message, 0, $severity, $file, $line, null); |
214
|
|
|
} |
215
|
|
|
); |
216
|
|
|
$return = file_get_contents($filename, $use_include_path, $context, $offset, $maxlen); |
217
|
|
|
restore_error_handler(); |
218
|
|
|
|
219
|
|
|
return $return; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|