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