1 | <?php |
||
15 | class Path |
||
16 | { |
||
17 | /** |
||
18 | * Date placeholder replacement |
||
19 | * Replaces %{somevalue} with date({somevalue}) |
||
20 | * |
||
21 | * @param string $string |
||
22 | * @param int|null $time |
||
23 | * @return string |
||
24 | */ |
||
25 | 90 | public static function replaceDatePlaceholders(string $string, $time = null) : string |
|
26 | { |
||
27 | 90 | $time = $time === null ? time() : $time; |
|
28 | 90 | return preg_replace_callback( |
|
29 | 90 | '#%([a-zA-Z])#', |
|
30 | 90 | function ($match) use ($time) { |
|
31 | 55 | return date($match[1], $time); |
|
32 | 90 | }, |
|
33 | $string |
||
34 | ); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Does a given string contain a date placeholder |
||
39 | * |
||
40 | * @param string $string |
||
41 | * @return bool |
||
42 | */ |
||
43 | 85 | public static function isContainingPlaceholder(string $string) : bool |
|
47 | |||
48 | /** |
||
49 | * Replaces %TARGET_DIR% and %TARGET_FILE% in given string |
||
50 | * |
||
51 | * @param string $string |
||
52 | * @param string $target |
||
53 | * @return string |
||
54 | */ |
||
55 | 4 | public static function replaceTargetPlaceholders(string $string, string $target) : string |
|
62 | |||
63 | /** |
||
64 | * Create a regex that matches the raw path considering possible date placeholders |
||
65 | * |
||
66 | * @param string $stringWithDatePlaceholders |
||
67 | * @return string |
||
68 | */ |
||
69 | 28 | public static function datePlaceholdersToRegex(string $stringWithDatePlaceholders) : string |
|
90 | |||
91 | /** |
||
92 | * Determine if the path has a trailing slash |
||
93 | * |
||
94 | * @param string $string |
||
95 | * @return bool |
||
96 | */ |
||
97 | 94 | public static function hasTrailingSlash(string $string) : bool |
|
101 | |||
102 | /** |
||
103 | * Adds trailing slash to a string/path if not already there |
||
104 | * |
||
105 | * @param string $string |
||
106 | * @return string |
||
107 | */ |
||
108 | 24 | public static function withTrailingSlash(string $string) : string |
|
112 | |||
113 | /** |
||
114 | * Removes the trailing slash from a string/path |
||
115 | * |
||
116 | * @param string $string |
||
117 | * @return string |
||
118 | */ |
||
119 | 96 | public static function withoutTrailingSlash(string $string) : string |
|
123 | |||
124 | /** |
||
125 | * Determine if the path has a leading slash |
||
126 | * |
||
127 | * @param string $string |
||
128 | * @return bool |
||
129 | */ |
||
130 | 88 | public static function hasLeadingSlash(string $string) : bool |
|
134 | |||
135 | /** |
||
136 | * Adds leading slash to a string/path if not already there. |
||
137 | * |
||
138 | * @param string $string |
||
139 | * @return string |
||
140 | */ |
||
141 | 15 | public static function withLeadingSlash(string $string) : string |
|
145 | |||
146 | /** |
||
147 | * Removes the leading slash from a string/path |
||
148 | * |
||
149 | * @param string $string |
||
150 | * @return string |
||
151 | */ |
||
152 | 21 | public static function withoutLeadingSlash(string $string) : string |
|
156 | |||
157 | /** |
||
158 | * Removes trailing and leading sl |
||
159 | * |
||
160 | * @param string $string |
||
161 | * @return string |
||
162 | */ |
||
163 | 1 | public static function withoutLeadingOrTrailingSlash(string $string) : string |
|
167 | |||
168 | /** |
||
169 | * Is given path absolute |
||
170 | * |
||
171 | * @param string $path |
||
172 | * @return bool |
||
173 | */ |
||
174 | 79 | public static function isAbsolutePath(string $path) : bool |
|
200 | |||
201 | /** |
||
202 | * Is given path an absolute windows path |
||
203 | * |
||
204 | * @param string $path |
||
205 | * @return bool |
||
206 | */ |
||
207 | 3 | public static function isAbsoluteWindowsPath(string $path) : bool |
|
211 | |||
212 | /** |
||
213 | * Converts a path to an absolute one if necessary relative to a given base path |
||
214 | * |
||
215 | * @param string $path |
||
216 | * @param string $base |
||
217 | * @param boolean $useIncludePath |
||
218 | * @return string |
||
219 | */ |
||
220 | 72 | public static function toAbsolutePath(string $path, string $base, bool $useIncludePath = false) : string |
|
236 | |||
237 | /** |
||
238 | * Return list of directories in a given path without absolute root element |
||
239 | * |
||
240 | * @param string $path |
||
241 | * @return array |
||
242 | */ |
||
243 | 84 | public static function getDirectoryListFromPath(string $path) : array |
|
249 | |||
250 | /** |
||
251 | * Return list of directories in a given path with absolute root element |
||
252 | * |
||
253 | * @param string $path |
||
254 | * @return array |
||
255 | */ |
||
256 | 84 | public static function getDirectoryListFromAbsolutePath(string $path) : array |
|
264 | |||
265 | /** |
||
266 | * Returns directory depth of a given path |
||
267 | * |
||
268 | * @param string $path |
||
269 | * @return int |
||
270 | */ |
||
271 | 12 | public static function getPathDepth(string $path) : int |
|
275 | } |
||
276 |