1<?php
2
3
4namespace ComboStrap;
5
6
7abstract class PathAbs implements Path
8{
9
10
11    /**
12     * @throws ExceptionNotFound
13     */
14    public function getExtension(): string
15    {
16        $extension = pathinfo($this->getLastName(), PATHINFO_EXTENSION);
17        if ($extension === "") {
18            throw new ExceptionNotFound("No extension found");
19        }
20        return $extension;
21
22    }
23
24    /**
25     * @return Mime based on the {@link PathAbs::getExtension()}
26     * @throws ExceptionNotFound
27     * @deprecated see {@link FileSystems::getMime()}
28     */
29    public function getMime(): Mime
30    {
31
32        return FileSystems::getMime($this);
33
34    }
35
36    /**
37     * @throws ExceptionNotFound
38     */
39    public function getLastNameWithoutExtension(): string
40    {
41        $lastName = $this->getLastName();
42        $lastPoint = strrpos($lastName, '.');
43        if ($lastPoint === false) {
44            return $lastName;
45        }
46        return substr($lastName, 0, $lastPoint);
47    }
48
49    /**
50     *
51     */
52    public function getNamesWithoutExtension()
53    {
54        $names = $this->getNames();
55        $sizeof = sizeof($names);
56        if ($sizeof == 0) {
57            return $names;
58        }
59        $lastName = $names[$sizeof - 1];
60        $index = strrpos($lastName, ".");
61        if ($index === false) {
62            return $names;
63        }
64        $names[$sizeof - 1] = substr($lastName, 0, $index);
65        return $names;
66    }
67
68    public function __toString()
69    {
70        return $this->toUriString();
71    }
72
73
74    public function toUriString(): string
75    {
76        return $this->toAbsoluteId();
77    }
78
79    /**
80     * @throws ExceptionCast when
81     * Utility {@link WikiPath::createFromPathObject()}
82     */
83    function toWikiPath(): WikiPath
84    {
85        if ($this instanceof WikiPath) {
86            return $this;
87        }
88        if ($this instanceof LocalPath) {
89            try {
90                return $this->toWikiPath();
91            } catch (ExceptionBadArgument|ExceptionCast $e) {
92                throw new ExceptionCast($e);
93            }
94        }
95        if ($this instanceof MarkupPath) {
96            try {
97                return $this->getPathObject()->toWikiPath();
98            } catch (ExceptionCast $e) {
99                throw new ExceptionCast($e);
100            }
101        }
102        throw new ExceptionCast("This is not a wiki path or local path");
103    }
104
105    /**
106     * @throws ExceptionCast when
107     */
108    function toLocalPath(): LocalPath
109    {
110        if ($this instanceof LocalPath) {
111            return $this;
112        }
113        if ($this instanceof WikiPath) {
114            return $this->toLocalPath();
115        }
116        if ($this instanceof MarkupPath) {
117            return $this->getPathObject()->toLocalPath();
118        }
119        throw new ExceptionCast("Unable to cast to LocalPath as this path is not a wiki path or a local path but a " . get_class($this));
120    }
121
122
123}
124