convert to autocracy
[mdref/mdref] / mdref / Path.php
1 <?php
2
3 namespace mdref;
4
5 class Path
6 {
7 /**
8 * Computed path
9 * @var string
10 */
11 protected $path = "";
12
13 /**
14 * The base directory where path is located
15 * @var string
16 */
17 protected $baseDir = "";
18
19 function __construct($baseDir = "", $path = "") {
20 $this->baseDir = $baseDir;
21 $this->path = $path;
22 }
23
24 function __invoke($path) {
25 $that = clone $this;
26 $that->path = $path;
27 return $that;
28 }
29
30 function __toString() {
31 return $this->getFullPath();
32 }
33
34 function getBaseDir() {
35 return $this->baseDir;
36 }
37
38 function getPathName() {
39 return $this->path;
40 }
41
42 function getFullPath($ext = "") {
43 return $this->baseDir . DIRECTORY_SEPARATOR . $this->path . $ext;
44 }
45
46 function getSubPath($path) {
47 return trim(substr($path, strlen($this->baseDir)), DIRECTORY_SEPARATOR);
48 }
49
50 function isFile($ext = ".md") {
51 return is_file($this->getFullPath($ext));
52 }
53
54 function toHtml() {
55 if ($this->isFile()) {
56 return htmlspecialchars(file_get_contents($this->getFullPath()));
57 } elseif ($this->isFile("")) {
58 return htmlspecialchars(file_get_contents($this->getFullPath("")));
59 }
60 }
61 }