|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_paths; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_paths\Interfaces\IPaths; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Stuff |
|
11
|
|
|
* @package kalanis\kw_paths |
|
12
|
|
|
* Stuff helping to parse the paths |
|
13
|
|
|
* Do not use pathinfo(), because has problems with code pages |
|
14
|
|
|
*/ |
|
15
|
|
|
class Stuff |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Return path with no extra dots and slashes |
|
19
|
|
|
* Do not call realpath() which do the similar thing |
|
20
|
|
|
* @param string $path |
|
21
|
|
|
* @param string $delimiter |
|
22
|
|
|
* @throws PathsException |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
4 |
|
public static function sanitize(string $path, string $delimiter = DIRECTORY_SEPARATOR): string |
|
26
|
|
|
{ |
|
27
|
4 |
|
return static::arrayToPath(array_filter(array_filter(static::pathToArray($path, $delimiter), [Stuff::class, 'notDots'])), $delimiter); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
7 |
|
public static function notDots(string $content): bool |
|
31
|
|
|
{ |
|
32
|
7 |
|
return !in_array($content, ['.', '..']); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $path |
|
37
|
|
|
* @param string $delimiter |
|
38
|
|
|
* @throws PathsException |
|
39
|
|
|
* @return array<int, string> |
|
40
|
|
|
*/ |
|
41
|
7 |
|
public static function pathToArray(string $path, string $delimiter = DIRECTORY_SEPARATOR): array |
|
42
|
|
|
{ |
|
43
|
7 |
|
return Extras\PathTransform::get()->expandName($path, $delimiter); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string[] $path |
|
48
|
|
|
* @param string $delimiter |
|
49
|
|
|
* @throws PathsException |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
10 |
|
public static function arrayToPath(array $path, string $delimiter = DIRECTORY_SEPARATOR): string |
|
53
|
|
|
{ |
|
54
|
10 |
|
return Extras\PathTransform::get()->compactName($path, $delimiter); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $path |
|
59
|
|
|
* @param string $delimiter |
|
60
|
|
|
* @throws PathsException |
|
61
|
|
|
* @return array<int, string> |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public static function linkToArray(string $path, string $delimiter = IPaths::SPLITTER_SLASH): array |
|
64
|
|
|
{ |
|
65
|
3 |
|
return Extras\PathTransform::get()->expandName($path, $delimiter); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string[] $path |
|
70
|
|
|
* @param string $delimiter |
|
71
|
|
|
* @throws PathsException |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public static function arrayToLink(array $path, string $delimiter = IPaths::SPLITTER_SLASH): string |
|
75
|
|
|
{ |
|
76
|
1 |
|
return Extras\PathTransform::get()->compactName($path, $delimiter); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Path to file (with trailing slash) |
|
81
|
|
|
* @param string $path |
|
82
|
|
|
* @param string $delimiter |
|
83
|
|
|
* @return string |
|
84
|
|
|
* Do not use dirname(), because has problems with code pages |
|
85
|
|
|
*/ |
|
86
|
4 |
|
public static function directory(string $path, string $delimiter = DIRECTORY_SEPARATOR): string |
|
87
|
|
|
{ |
|
88
|
4 |
|
$pos = mb_strrpos($path, $delimiter); |
|
89
|
4 |
|
return (false !== $pos) ? mb_substr($path, 0, $pos + 1) : '' ; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Name of file from the whole path |
|
94
|
|
|
* @param string $path |
|
95
|
|
|
* @param string $delimiter |
|
96
|
|
|
* @return string |
|
97
|
|
|
* Do not use basename(), because has problems with code pages |
|
98
|
|
|
*/ |
|
99
|
4 |
|
public static function filename(string $path, string $delimiter = DIRECTORY_SEPARATOR): string |
|
100
|
|
|
{ |
|
101
|
4 |
|
$pos = mb_strrpos($path, $delimiter); |
|
102
|
4 |
|
return (false !== $pos) ? mb_substr($path, $pos + 1) : $path ; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Base of file (part before the "dot") |
|
107
|
|
|
* @param string $path |
|
108
|
|
|
* @param string $delimiter |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
12 |
|
public static function fileBase(string $path, string $delimiter = IPaths::SPLITTER_DOT): string |
|
112
|
|
|
{ |
|
113
|
12 |
|
$pos = mb_strrpos($path, $delimiter); |
|
114
|
12 |
|
return (false !== $pos) && (0 < $pos) ? mb_substr($path, 0, $pos) : $path ; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Extension of file (part after the "dot" if it exists) |
|
119
|
|
|
* @param string $path |
|
120
|
|
|
* @param string $delimiter |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
12 |
|
public static function fileExt(string $path, string $delimiter = IPaths::SPLITTER_DOT): string |
|
124
|
|
|
{ |
|
125
|
12 |
|
$pos = mb_strrpos($path, $delimiter); |
|
126
|
12 |
|
return ((false !== $pos) && (0 < $pos)) ? mb_substr($path, $pos + 1) : '' ; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Remove ending slash |
|
131
|
|
|
* @param string $path |
|
132
|
|
|
* @param string $delimiter |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
2 |
|
public static function removeEndingSlash(string $path, string $delimiter = DIRECTORY_SEPARATOR): string |
|
136
|
|
|
{ |
|
137
|
2 |
|
return ($delimiter == mb_substr($path, -1, 1)) ? mb_substr($path, 0, -1) : $path ; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Return correct name with no non-ascii characters |
|
142
|
|
|
* @param string $name |
|
143
|
|
|
* @param int $maxLen |
|
144
|
|
|
* @param string $delimiter |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
8 |
|
public static function canonize(string $name, int $maxLen = 127, string $delimiter = IPaths::SPLITTER_DOT): string |
|
148
|
|
|
{ |
|
149
|
8 |
|
$fName = preg_replace('/((&[[:alpha:]]{1,6};)|(&#[[:alnum:]]{1,7};))/', '', $name); // remove ascii-escaped chars |
|
150
|
8 |
|
$fName = preg_replace('/[^[:alnum:]_\s\-\.]/', '', strval($fName)); // remove non-alnum + dots |
|
151
|
8 |
|
$fName = preg_replace('/[\s]/', '_', strval($fName)); // whitespaces to underscore |
|
152
|
8 |
|
$fName = strval($fName); |
|
153
|
8 |
|
$ext = static::fileExt($fName); |
|
154
|
8 |
|
$base = static::fileBase($fName); |
|
155
|
8 |
|
$extLen = strlen($ext); |
|
156
|
8 |
|
$cut = substr($base, 0, ($maxLen - $extLen)); |
|
157
|
8 |
|
return ($extLen) ? $cut . $delimiter . $ext : $cut ; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param string $param |
|
162
|
|
|
* @return array<string|int, string|int|float|bool|array<mixed>> |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public static function httpStringIntoArray(string $param): array |
|
165
|
|
|
{ |
|
166
|
2 |
|
parse_str($param, $result); |
|
167
|
2 |
|
return $result; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param array<string|int, string|int|float|bool|array<string|int>> $param |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
2 |
|
public static function arrayIntoHttpString(array $param): string |
|
175
|
|
|
{ |
|
176
|
2 |
|
return http_build_query($param); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|