Completed
Push — master ( c23ac8...e7d945 )
by Pol
02:54
created

Path::withScheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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