1 : <?php
2 : namespace phMagick\Core;
3 :
4 : use phMagick\Core\Exception\PathNotFoundException;
5 : class Path
6 : {
7 : private $path;
8 : public function __construct($path = null)
9 : {
10 15 : $this->setPath($path);
11 15 : }
12 :
13 : private function setPath($path)
14 : {
15 15 : $this->path = $path;
16 15 : }
17 :
18 : private function getPath()
19 : {
20 5 : return $this->path;
21 : }
22 :
23 : function format()
24 : {
25 :
26 4 : $path = $this->getPath();
27 :
28 4 : $path = str_replace('\\', DIRECTORY_SEPARATOR, $path);
29 :
30 :
31 4 : if ( is_dir($path)) {
32 4 : $dirname = $path;
33 4 : $file = '';
34 4 : } elseif (is_file($path)) {
35 1 : $dirname = dirname($path);
36 1 : $file = basename($path);
37 1 : } else {
38 0 : throw new PathNotFoundException($path . ' not found');
39 : }
40 :
41 :
42 4 : $dirname = rtrim($dirname, DIRECTORY_SEPARATOR);
43 4 : $dirname .= DIRECTORY_SEPARATOR;
44 :
45 :
46 4 : $path = $dirname . $file ;
47 :
48 4 : $path = str_replace(' ','\ ',$path) ;
49 4 : $this->setPath($path);
50 4 : return $path;
51 : }
52 :
53 : public function __toString()
54 : {
55 : try {
56 0 : $ret = $this->format();
57 0 : } Catch (\Exception $e) {
58 0 : $ret = '';
59 : }
60 :
61 0 : return $ret;
62 : }
63 :
64 : function up()
65 : {
66 0 : $this->setPath( dirname($this->getPath()) );
67 0 : return $this;
68 : }
69 :
70 : function add($item)
71 : {
72 1 : $this->setPath (
73 1 : $this->getPath() . DIRECTORY_SEPARATOR . $item
74 1 : );
75 1 : return $this;
76 : }
|