-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath.php
More file actions
122 lines (97 loc) · 2.15 KB
/
Path.php
File metadata and controls
122 lines (97 loc) · 2.15 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* PhpPath
* @link https://github.com/masicek/PhpPath
* @author Viktor Mašíček <viktor@masicek.net>
* @license "New" BSD License
*/
namespace PhpPath;
require_once __DIR__ . '/Exceptions.php';
/**
* Collection of static functions for better work with path to directory/file
*
* @author Viktor Mašíček <viktor@masicek.net>
*/
class Path
{
/**
* Version of PhpPath
*/
const VERSION = '0.2.1';
/**
* Check if the directory exists
*
* @param string $path Checked path
*
* @throws Exception Directory not exists
* @return string Input path
*/
static public function checkDirectory($path)
{
$pathFiltered = self::make($path);
if (!is_dir($pathFiltered))
{
throw new NotExistsPathException('Directory "' . $path . '" not exists.');
}
return $path;
}
/**
* Check if the file exists
*
* @param string $path Checked path
*
* @throws Exception File not exists
* @return string Input path
*/
static public function checkFile($path)
{
$pathFiltered = self::make($path);
if (!is_file($pathFiltered))
{
throw new NotExistsPathException('File "' . $path . '" not exists.');
}
return $path;
}
/**
* Make path from list of arguments.
*
* @return string
*/
static public function make()
{
$pathParts = func_get_args();
$ds = DIRECTORY_SEPARATOR;
$path = implode($ds, $pathParts);
// correct separator
$path = str_replace('/', $ds, $path);
$path = str_replace('\\', $ds, $path);
// replace "/./" and "//"
$path = str_replace($ds . $ds, $ds, $path);
$path = str_replace($ds . '.' . $ds, $ds, $path);
return $path;
}
/**
* Make path from list of arguments and check if the directory exists
*
* @return string
*/
static public function makeAndCheckDirectory()
{
$args = func_get_args();
$path = call_user_func_array('self::make', $args);
self::checkDirectory($path);
return $path;
}
/**
* Make path from list of arguments and check if the file exists
*
* @return string
*/
static public function makeAndCheckFile()
{
$args = func_get_args();
$path = call_user_func_array('self::make', $args);
self::checkFile($path);
return $path;
}
}