Passed
Push — master ( 62941e...7e45f3 )
by Pol
02:18
created

Path::getScheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpvfs\Utils;
6
7
class Path implements PathInterface, \IteratorAggregate
8
{
9
    /**
10
     * @var bool
11
     */
12
    private $absolute;
13
14
    /**
15
     * @var string[]
16
     */
17
    private $fragments;
18
19
    /**
20
     * @var string
21
     */
22
    private $scheme = '';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 7
    public function __toString()
28
    {
29 7
        if ('' !== $this->getScheme()) {
30 1
            $root = $this->getScheme() . '://';
31
        } else {
32 7
            $root = $this->isAbsolute() ? '/' : '';
33
        }
34
35 7
        return $root . \implode('/', $this->getFragments());
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 17
    public function basename(): string
42
    {
43 17
        return \basename(($this->isAbsolute() ? '/' : '') . \implode('/', $this->getFragments()));
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 16
    public function dirname(): string
50
    {
51 16
        return \dirname(($this->isAbsolute() ? '/' : '') . \implode('/', $this->getFragments()));
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 27
    public static function fromString(string $id): Path
58
    {
59 27
        $instance = new self();
60
61 27
        if (false !== $parsed = \parse_url($id)) {
62 26
            if (\array_key_exists('scheme', $parsed)) {
63 7
                $instance->scheme = $parsed['scheme'];
64
65 7
                $id = \DIRECTORY_SEPARATOR . $parsed['host'] . $parsed['path'];
66
            }
67
        }
68
69 27
        $instance->fragments = \explode(
70 27
            \DIRECTORY_SEPARATOR,
71 27
            \ltrim($id, \DIRECTORY_SEPARATOR)
72
        );
73 27
        $instance->absolute = 0 === \strpos($id, '/');
74
75 27
        return $instance;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 3
    public function getFirstPart(): string
82
    {
83 3
        $first = \reset($this->fragments);
84
85 3
        return empty($first) ?
86 1
            \DIRECTORY_SEPARATOR :
87 3
            $first;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 19
    public function getIterator()
94
    {
95 19
        yield from $this->getFragments();
96 19
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 3
    public function getLastPart(): string
102
    {
103 3
        if (false !== $end = \end($this->fragments)) {
104 3
            return $end;
105
        }
106
107
        throw new \Exception('Error getting last part of path.');
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 7
    public function getScheme(): string
114
    {
115 7
        return $this->scheme;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 25
    public function isAbsolute(): bool
122
    {
123 25
        return $this->absolute;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 5
    public function isRoot(): bool
130
    {
131 5
        return '' === $this->basename();
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 5
    public function isValid(): bool
138
    {
139 5
        if (\preg_match('/^[^*?"<>|:]*$/', \trim((string) $this->withScheme(null), ' /'))) {
140 3
            return true;
141
        }
142
143 2
        return false;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 3
    public function shift(): string
150
    {
151 3
        if (null !== $shift = \array_shift($this->fragments)) {
152 3
            return $shift;
153
        }
154
155
        throw new \Exception('Error while shifting the path.');
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 6
    public function withScheme(?string $scheme): Path
162
    {
163 6
        $clone = clone $this;
164
165 6
        $clone->scheme = $scheme ?? '';
166
167 6
        return $clone;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173 25
    protected function getFragments(): array
174
    {
175 25
        $fragments = $this->fragments;
176
177 25
        if (empty($fragments[0])) {
178 4
            $fragments[0] = \DIRECTORY_SEPARATOR;
179
        }
180
181 25
        return $fragments;
182
    }
183
}
184