Completed
Pull Request — master (#84)
by
unknown
02:04
created

Path   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 43
ccs 0
cts 27
cp 0
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A __construct() 0 3 1
A setPath() 0 4 1
A getPath() 0 7 1
A doInitialize() 0 3 1
A getSegments() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Purl;
6
7
use function array_map;
8
use function explode;
9
use function implode;
10
use function str_replace;
11
12
/**
13
 * Path represents the part of a Url after the domain suffix and before the hashmark (#).
14
 */
15
class Path extends AbstractPart
16
{
17
    /** @var string|null The original path string. */
18
    private ?string $path = null;
19
20
    public function __construct(?string $path = null)
21
    {
22
        $this->path = $path;
23
    }
24
25
    public function getPath(): string
26
    {
27
        $this->initialize();
28
29
        return implode('/', array_map(static function ($value) {
30
            return str_replace(' ', '%20', $value);
31
        }, $this->data));
32
    }
33
34
    public function setPath(string $path): void
35
    {
36
        $this->initialized = false;
37
        $this->path        = $path;
38
    }
39
40
    /**
41
     * @return mixed[]
42
     */
43
    public function getSegments(): array
44
    {
45
        $this->initialize();
46
47
        return $this->data;
48
    }
49
50
    public function __toString(): string
51
    {
52
        return $this->getPath();
53
    }
54
55
    protected function doInitialize(): void
56
    {
57
        $this->data = explode('/', (string) $this->path);
58
    }
59
}
60