1 | <?php |
||
11 | class Path |
||
12 | { |
||
13 | /** |
||
14 | * Platform specific directory separator character. |
||
15 | * This is backslash ('\') on Windows, slash ('/') on Unix, and colon (':') on Mac. |
||
16 | */ |
||
17 | const DIRECTORY_SEPARATOR_CHAR = '\\'; |
||
18 | |||
19 | /** |
||
20 | * Platform specific alternate directory separator character. |
||
21 | * This is backslash ('\') on Unix, and slash ('/') on Windows and MacOS. |
||
22 | */ |
||
23 | const ALTDIRECTORY_SEPARATOR_CHAR = '/'; |
||
24 | |||
25 | /** |
||
26 | * Platform specific volume separator character. |
||
27 | * This is colon (':') on Windows and MacOS, and slash ('/') on Unix. |
||
28 | * |
||
29 | * This is mostly useful for parsing paths like |
||
30 | * "c:\windows" or "MacVolume:System Folder". |
||
31 | */ |
||
32 | const VOLUME_SEPARATOR_CHAR = ':'; |
||
33 | |||
34 | const INVALID_PATH_ASCII = [ |
||
35 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
||
36 | 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, |
||
37 | 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, |
||
38 | 31, 34, 60, 62, 124 |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Changes the extension of a path string. |
||
43 | * |
||
44 | * If path is null, the function returns null. |
||
45 | * |
||
46 | * @param string $path The path information to modify. |
||
47 | * The path cannot contain any of the characters |
||
48 | * defined in invalidPathChars. |
||
49 | * If it does not contain a file extension, |
||
50 | * the new file extension is appended to the path. |
||
51 | * @param string $extension The new extension (with or without a leading period). |
||
52 | * Specify null to remove an existing extension from path. |
||
53 | * If it is null, any existing extension is removed from path. |
||
54 | * |
||
55 | * @return string A file path with the same root, directory, |
||
56 | * and base name parts as path, |
||
57 | * but with the file extension changed |
||
58 | * to the specified extension. |
||
59 | */ |
||
60 | public static function changeExtension(string $path, string $extension = null): string |
||
96 | |||
97 | /** |
||
98 | * Gets an array containing the characters that are not allowed in path names. |
||
99 | */ |
||
100 | public static function invalidPathChars(): array |
||
110 | |||
111 | /** |
||
112 | * Gets a value indicating whether the specified path string contains a root. |
||
113 | * |
||
114 | * A path is considered rooted if it starts with a backslash ("\") |
||
115 | * or a drive letter and a colon (":"). |
||
116 | * |
||
117 | * @param string $path The path to test. |
||
118 | * |
||
119 | * @return bool true if path contains a root; otherwise, false. |
||
120 | */ |
||
121 | public static function isPathRooted(string $path): bool |
||
141 | |||
142 | /** |
||
143 | * Combines two strings into a path. |
||
144 | * |
||
145 | * @param string $path1 The first path to combine. |
||
146 | * @param string $path2 The second path to combine. |
||
147 | * |
||
148 | * @return string The combined paths. |
||
149 | * If one of the specified paths is a zero-length string, |
||
150 | * this method returns the other path. |
||
151 | * If path2 contains an absolute path, |
||
152 | * this method returns path2. |
||
153 | */ |
||
154 | public static function combine(string $path1, string $path2): string |
||
185 | |||
186 | /** |
||
187 | * Check if contains one or more of the invalid characters |
||
188 | * defined in invalidPathChars. |
||
189 | */ |
||
190 | private static function checkInvalidPathChars(string $path): void |
||
196 | |||
197 | /** |
||
198 | * Indicates if the given path contains invalid characters. |
||
199 | * (", <, >, or any ASCII char whose integer representation |
||
200 | * is in the range of 0 through 31) |
||
201 | */ |
||
202 | private static function hasIllegalCharacter(string $path): bool |
||
211 | } |
||
212 |