-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.php
More file actions
376 lines (332 loc) · 7.4 KB
/
Copy pathpath.php
File metadata and controls
376 lines (332 loc) · 7.4 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?PHP
namespace phprax;
use phprax\path;
/**
* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
*
* Colon delimited path handler
*
* @category Utility
* @package Phprax
* @author James Dornan <james@catch22.com>
* @copyright 2003-2008 James Dornan <james@catch22.com>
* @since Class available since Release 0.5
*/
/**
* Class: path
*
* @see \Iterator
* @see \ArrayAccess
* @see \Countable
*/
class path implements \Iterator, \ArrayAccess, \Countable
{
/**
* _path
*
* @var array
* @access private
*/
private $_path = [];
/**
* _options
*
* @var array
* @access private
*/
private $_options = ['exists' => false, 'file' => false, 'dir' => false,
'readable' => false];
/**
* _dilimiter
*
* @var string
* @access private
*/
private $_dilimiter = ':';
/**
* __construct
*
* @param mixed $path
* @param array $options
* @return void
* @access public
*/
public function __construct($path = false, array $options)
{
if (is_array($options) && count($options) > 0)
$this->setOptions($options);
if ($path)
$this->load($path);
}
/**
* setOptions
*
* @param array $options
* @return void
* @access public
*/
public function setOptions(array $options)
{
$this->_options = array_merge($this->_options, $options);
}
/**
* load
*
* @param mixed $path
* @return void
* @access public
*/
public function load($path)
{
if (!is_string($path))
throw new \Exception('Illegal non-string path.');
if (($data = explode($this->_dilimiter, (string) trim($path))) === false)
throw new \Exception('Illegal path string supplied.');
foreach(array_map('trim', $data) as $key=>$value) {
$this->offsetSet($key, $value);
}
}
/**
* Return the path as a string
*
* @return string
* @access public
*/
public function save()
{
return is_array($this->_path) ?
implode($this->_dilimiter, $this->_path) : '';
}
/**
* sleep
*
* @return array
* @access public
*/
public function __sleep()
{
return ['_options', '_path'];
}
/**
* wakeup
*
* @return void
* @access public
*/
public function __wakeup()
{
}
/**
* method used when object called as a string
*
* @return string
* @access public
*/
public function __toString()
{
return $this->save();
}
/**
* invoke magic method
*
* @param mixed
*
* @return mixed
* @access public
*/
public function __invoke()
{
$argc = func_num_args();
$argv = func_get_args();
// No argument, then export data as an array.
if ($argv == 0)
return $this->toArray();
// Import an array
if ($argv == 1 && is_array($argv[0]))
return $this->load($argv[0]);
if ($argv == 1 && is_string($argv[0]))
$this->_path[] = $data;
}
/**
* unshift
*
* @param string $value
* @return void
*/
public function unshift($value)
{
if (!is_string($value))
throw new \Exception('Illegal non-string value');
array_unshift($this->_path, trim($value));
}
/**
* replace
*
* @param string $oldvalue
* @param string $newvalue
* @return void
*/
public function replace($oldvalue, $newvalue)
{
$key = array_search($oldvalue, $this->_path);
if ($key === false)
throw new \Exception('Value not found');
$this[$key] = $newvalue;
}
/**
* check, whether a value exists
*
* @param int $offset array key
*
* @return boolean
* @access public
*/
public function offsetExists($offset)
{
return isset($this->_path[$offset]);
}
/**
* get a value
*
* @param int $offset array key
*
* @return string
* @access public
*/
public function offsetGet($offset)
{
return isset($this->_path[$offset]) ? $this->_path[$offset] : '';
}
/**
* set a property
*
* @param int $offset array key
* @param string $value array value
*
* @return void
* @access public
*/
public function offsetSet($offset, $value)
{
if (!is_numeric($offset) && $offset !== NULL)
throw new \Exception('Illegal non-numeric index');
if (!is_string($value))
throw new \Exception('Illegal non-string value');
if ($this->_options['exists'] && !file_exists($value))
throw new \Exception('Value does not exist.');
if ($this->_options['readable'] && !is_readable($value))
throw new \Exception('Value is not readable.');
if (is_link($value))
$pathname = readlink($value);
else
$pathname = $value;
if ($this->_options['file'] && !$this->_options['dir'] &&
!is_file($pathname)) {
throw new \Exception('Value is not a regular file.');
}
if ($this->_options['dir'] && !$this->_options['file'] &&
!is_dir($pathname)) {
throw new \Exception('Value is not a directory.');
}
if ($this->_options['dir'] && $this->_options['file'] &&
(!is_dir($pathname) && !is_file($pathname))) {
throw new \Exception('Value is not a file or directory.');
}
$this->_path[$offset] = $value;
}
/**
* unset a property
*
* @param int $offset array key
*
* @return void
* @access public
*/
public function offsetUnset($offset)
{
if (!is_numeric($offset))
throw new \Exception('Illegal non-numeric index');
unset($this->_path[$offset]);
}
/**
*
* @return void
* @access public
*/
public function rewind()
{
return reset($this->_path);
}
/**
* Return the current array element
*
* @return mixed
* @access public
*/
public function current()
{
return current($this->_path);
}
/**
* Return the key of the current array element
*
* @return int
* @access public
*/
public function key()
{
return key($this->_path);
}
/**
* next, advance one element in the array
*
* @return void
* @access public
*/
public function next()
{
return next($this->_path);
}
/**
* Is the current element valid?
*
* @return boolean
* @access public
*/
public function valid()
{
return $this->current() ? true : false;
}
/**
* Count of array elements
*
* @return int
* @access public
*/
public function count()
{
return count($this->_path);
}
/**
* to array
*
* @return array
* @access public
*/
public function toArray()
{
return $this->_path;
}
/**
* keys
*
* return an array of all the keys
*
* @return array
* @access public
*/
public function keys()
{
return array_keys($this->_path);
}
}