forked from kzap/Eden-PHP-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath.php
309 lines (261 loc) · 7.2 KB
/
Path.php
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php //-->
/*
* This file is part of the Utility package of the Eden PHP Library.
* (c) 2013-2014 Openovate Labs
*
* Copyright and license information can be found at LICENSE
* distributed with this package.
*/
namespace Eden\System;
use Eden\Type\StringType;
use Eden\System\Path\Exception as PathException;
/**
* General available methods for common pathing issues
*
* @vendor Eden
* @package Utility
* @author Christian Blanquera [email protected]
*/
class Path extends StringType implements \ArrayAccess
{
const ERROR_FULL_PATH_NOT_FOUND = 'The path %s or %s was not found.';
/**
* Preset and auto format the path
*
* @param *string
* @return void
*/
public function __construct($path)
{
//argument 1 must be a string
Argument::i()->test(1, 'string');
parent::__construct($this->format($path));
}
/**
* When object to string just give the path
*
* @return string
*/
public function __toString()
{
return $this->data;
}
/**
* Attempts to get the full absolute path
* as described on the server. The path
* given must exist.
*
* @param string|null root path
* @return Eden\System\Path
*/
public function absolute($root = null)
{
//argument 1 must be a string or null
Argument::i()->test(1, 'string', 'null');
//if path is a directory or file
if(is_dir($this->data) || is_file($this->data)) {
return $this;
}
//if root is null
if(is_null($root)) {
//assume the root is doc root
$root = $_SERVER['DOCUMENT_ROOT'];
}
//get the absolute path
$absolute = $this->format($this->format($root).$this->data);
//if absolute is a directory or file
if(is_dir($absolute) || is_file($absolute)) {
$this->data = $absolute;
return $this;
}
//if we are here then it means that no path was found so we should throw an exception
Exception::i()
->setMessage(self::ERROR_FULL_PATH_NOT_FOUND)
->addVariable($this->data)
->addVariable($absolute)
->trigger();
}
/**
* Adds a path to the existing one
*
* @param *string[,string..]
* @return Eden\System\Path
*/
public function append($path)
{
//argument 1 must be a string
$argument = Argument::i()->test(1, 'string');
//each argument will be a path
$paths = func_get_args();
//for each path
foreach($paths as $i => $path) {
//check for type errors
$argument->test($i + 1, $path, 'string');
//add to path
$this->data .= $this->format($path);
}
return $this;
}
/**
* Returns the path array
*
* @return array
*/
public function getArray()
{
return explode('/', $this->data);
}
/**
* isset using the ArrayAccess interface
*
* @param *scalar|null|bool
* @return bool
*/
public function offsetExists($offset)
{
//argument 1 must be scalar, null or bool
Argument::i()->test(1, 'scalar', 'null', 'bool');
return in_array($offset, $this->getArray());
}
/**
* returns data using the ArrayAccess interface
*
* @param *scalar|null|bool
* @return string|null
*/
public function offsetGet($offset)
{
//argument 1 must be scalar, null or bool
Argument::i()->test(1, 'scalar', 'null', 'bool');
$pathArray = $this->getArray();
if($offset == 'first') {
$offset = 0;
}
if($offset == 'last') {
$offset = count($pathArray) - 1;
}
if(is_numeric($offset)) {
return isset($pathArray[$offset]) ? $pathArray[$offset] : null;
}
return null;
}
/**
* Sets data using the ArrayAccess interface
*
* @param *scalar|null|bool
* @param *mixed
* @return void
*/
public function offsetSet($offset, $value)
{
//argument 1 must be scalar, null or bool
Argument::i()->test(1, 'scalar', 'null', 'bool');
if (is_null($offset)) {
$this->append($value);
} else if($offset == 'prepend') {
$this->prepend($value);
} else if($offset == 'replace') {
$this->replace($value);
} else {
$pathArray = $this->getArray();
if($offset > 0 && $offset < count($pathArray)) {
$pathArray[$offset] = $value;
$this->data = implode('/', $pathArray);
}
}
}
/**
* unsets using the ArrayAccess interface
*
* @param *scalar|null|bool
* @return bool
*/
public function offsetUnset($offset)
{
}
/**
* Adds a path before the existing one
*
* @param *string[,string..]
* @return Eden\System\Path
*/
public function prepend($path)
{
//argument 1 must be a string
$error = Argument::i()->test(1, 'string');
//each argument will be a path
$paths = func_get_args();
//for each path
foreach($paths as $i => $path) {
//check for type errors
$error->test($i + 1, $path, 'string');
//add to path
$this->data = $this->format($path).$this->data;
}
return $this;
}
/**
* Remove the last path
*
* @return Eden\System\Path
*/
public function pop()
{
//get the path array
$pathArray = $this->getArray();
//remove the last
$path = array_pop($pathArray);
//set path
$this->data = implode('/', $pathArray);
return $path;
}
/**
* Replaces the last path with this one
*
* @param *string
* @return Eden\System\Path
*/
public function replace($path)
{
//argument 1 must be a string
Argument::i()->test(1, 'string');
//get the path array
$pathArray = $this->getArray();
//pop out the last
array_pop($pathArray);
//push in the new
$pathArray[] = $path;
//assign back to path
$this->data = implode('/', $pathArray);
return $this;
}
/**
* Formats the path
* 1. Must start with forward slash
* 2. Must not end with forward slash
* 3. Must not have double forward slashes
*
* @param *string
* @return string
*/
protected function format($path)
{
//replace back slash with forward
$path = str_replace('\\', '/', $path);
//replace double forward slash with 1 forward slash
$path = str_replace('//', '/', $path);
//if there is a last forward slash
if(substr($path, -1, 1) == '/') {
//remove it
$path = substr($path, 0, -1);
}
//if the path does not start with a foward slash
//and the path does not have a colon
//(this is a test for windows)
if(substr($path, 0, 1) != '/' && !preg_match("/^[A-Za-z]+\:/", $path)) {
//it's safe to add a slash
$path = '/'.$path;
}
return $path;
}
}