xref: /plugin/combo/ComboStrap/LocalPath.php (revision c3437056399326d621a01da73b649707fbb0ae69)
1<?php
2
3
4namespace ComboStrap;
5
6/**
7 * Class LocalPath
8 * @package ComboStrap
9 * A local file system path
10 */
11class LocalPath extends PathAbs
12{
13
14    private const FILE_SYSTEM_DIRECTORY_SEPARATOR = DIRECTORY_SEPARATOR;
15
16    private $path;
17
18    /**
19     * LocalPath constructor.
20     * @param $path
21     */
22    public function __construct($path)
23    {
24        $this->path = $path;
25    }
26
27
28    public static function create(string $filePath): LocalPath
29    {
30        return new LocalPath($filePath);
31    }
32
33    public static function createFromPath(string $string): LocalPath
34    {
35        return new LocalPath($string);
36    }
37
38    function getScheme(): string
39    {
40        return LocalFs::SCHEME;
41    }
42
43    function getLastName()
44    {
45        $names = $this->getNames();
46        $sizeof = sizeof($names);
47        if ($sizeof === 0) {
48            return null;
49        }
50        return $names[$sizeof - 1];
51
52    }
53
54    public function getExtension()
55    {
56        return pathinfo($this->path, PATHINFO_EXTENSION);
57    }
58
59    function getNames()
60    {
61        $directorySeparator = $this->getDirectorySeparator();
62        return explode($directorySeparator, $this->path);
63    }
64
65    function getDokuwikiId()
66    {
67        throw new ExceptionComboRuntime("Not implemented");
68    }
69
70
71    function toString()
72    {
73        return $this->path;
74    }
75
76    public function getParent(): ?Path
77    {
78        $absolutePath = pathinfo($this->path, PATHINFO_DIRNAME);
79        if (empty($absolutePath)) {
80            return null;
81        }
82        return new LocalPath($absolutePath);
83    }
84
85    function toAbsolutePath(): Path
86    {
87        $path = realpath($this->path);
88        if ($path !== false) {
89            // Path return false when the file does not exist
90            return new LocalPath($path);
91        }
92        return $this;
93
94    }
95
96    /**
97     * @return string
98     */
99    private function getDirectorySeparator(): string
100    {
101        $directorySeparator = self::FILE_SYSTEM_DIRECTORY_SEPARATOR;
102        if (
103            $directorySeparator === '\\'
104            &&
105            strpos($this->path, "/") !== false
106        ) {
107            $directorySeparator = "/";
108        }
109        return $directorySeparator;
110    }
111
112
113}
114