Path::__toString()   A
last analyzed

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
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 $path;
19
20 45
    public function __construct(?string $path = null)
21
    {
22 45
        $this->path = $path;
23 45
    }
24
25 30
    public function getPath() : string
26
    {
27 30
        $this->initialize();
28
29
        return implode('/', array_map(static function ($value) {
30 30
            return str_replace(' ', '%20', $value);
31 30
        }, $this->data));
32
    }
33
34 2
    public function setPath(string $path) : void
35
    {
36 2
        $this->initialized = false;
37 2
        $this->path        = $path;
38 2
    }
39
40
    /**
41
     * @return mixed[]
42
     */
43 1
    public function getSegments() : array
44
    {
45 1
        $this->initialize();
46
47 1
        return $this->data;
48
    }
49
50 28
    public function __toString() : string
51
    {
52 28
        return $this->getPath();
53
    }
54
55 31
    protected function doInitialize() : void
56
    {
57 31
        $this->data = explode('/', (string) $this->path);
58 31
    }
59
}
60