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