1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<?php namespace amekusa\plz; main::required;
/**
* Path utilities
*
* To get started, place the following line around top of your code.
* ```php
* use amekusa\plz\path;
* ```
*/
abstract class path {
/**
* Returns a normalized path
* @example Absolute path
* ```php
* echo path::normal('/srv//http/example.com///');
* ```
* ```php
* /srv/http/example.com/
* ```
* @example Relative path
* ```php
* echo path::normal('images//favicon.svg');
* ```
* ```php
* images/favicon.svg
* ```
* @example Normalize to slash
* ```php
* echo path::normal('xxx\yyy\zzz');
* ```
* ```php
* xxx/yyy/zzz
* ```
* @example Normalize to backslash
* ```php
* echo path::normal('xxx/yyy/zzz', '\\');
* ```
* ```php
* xxx\yyy\zzz
* ```
* @param string $X Path to normalize
* @param string $Separator [`'/'`] Directory separator
*/
static function normal($X, $Separator = '/') {
return preg_replace('/(?:\/|\\\\|'.preg_quote('/', '/').')+/',
$Separator, $X);
}
/**
* Returns the extension of a file path
* @example Demonstration
* ```php
* echo path::ext('choosy-developers-choose.gif');
* ```
* ```php
* .gif
* ```
* @param string $X A file path
* @return string The extension of `$X`
*/
static function ext($X) {
return substr($X, strrpos($X, '.'));
}
/**
* Returns a single path that is concatenated with supplied paths
* @example Demonstration
* ```php
* echo path::join('/srv', 'http/', '/example.com');
* ```
* ```php
* /srv/http/example.com
* ```
* @example Passing an array
* ```php
* echo path::join(array ('/srv', 'http', 'example.com'));
* ```
* ```php
* /srv/http/example.com
* ```
* @param array|string* $Paths Pieces of a path
*/
static function join($Paths) {
return path::join_with('/', func_num_args() > 1 ? func_get_args() :
(type::is_iterable($Paths) ? $Paths : type::arr($Paths)));
}
static function join_with($Separator, $Paths) {
if (!$Paths) return '';
return path::normal(implode($Separator, array_filter($Paths,
__NAMESPACE__.'\type::str')), $Separator);
}
}