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

Fragment   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 40
c 3
b 0
f 0
dl 0
loc 108
ccs 0
cts 66
cp 0
rs 10
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setPath() 0 5 1
A set() 0 6 1
A getQuery() 0 5 1
A setFragment() 0 7 1
A getPath() 0 5 1
A __toString() 0 3 1
A setQuery() 0 5 1
A __construct() 0 10 2
A doInitialize() 0 12 4
A getFragment() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Purl;
6
7
use function array_merge;
8
use function is_array;
9
use function parse_url;
10
use function sprintf;
11
12
/**
13
 * Fragment represents the part of a Url after the hashmark (#).
14
 *
15
 * @property Path|string $path
16
 * @property Query|string $query
17
 */
18
class Fragment extends AbstractPart
19
{
20
    /** @var string|null The original fragment string. */
21
    private ?string $fragment = null;
22
23
    /** @var mixed[] */
24
    protected array $data = [
25
        'path'  => null,
26
        'query' => null,
27
    ];
28
29
    /** @var string[] */
30
    protected array $partClassMap = [
31
        'path' => 'Purl\Path',
32
        'query' => 'Purl\Query',
33
    ];
34
35
    /**
36
     * @param string|Path|null $fragment
37
     */
38
    public function __construct($fragment = null, ?Query $query = null)
39
    {
40
        if ($fragment instanceof Path) {
41
            $this->initialized  = true;
42
            $this->data['path'] = $fragment;
43
        } else {
44
            $this->fragment = $fragment;
45
        }
46
47
        $this->data['query'] = $query;
48
    }
49
50
    /**
51
     * @param mixed $value
52
     */
53
    public function set(string $key, $value): AbstractPart
54
    {
55
        $this->initialize();
56
        $this->data[$key] = $this->preparePartValue($key, $value);
57
58
        return $this;
59
    }
60
61
    public function getFragment(): string
62
    {
63
        $this->initialize();
64
65
        return sprintf(
66
            '%s%s',
67
            (string) $this->path,
68
            (string) $this->query !== '' ? '?' . (string) $this->query : ''
69
        );
70
    }
71
72
    public function setFragment(string $fragment): AbstractPart
73
    {
74
        $this->initialized = false;
75
        $this->data        = [];
76
        $this->fragment    = $fragment;
77
78
        return $this;
79
    }
80
81
    public function setPath(Path $path): AbstractPart
82
    {
83
        $this->data['path'] = $path;
84
85
        return $this;
86
    }
87
88
    public function getPath(): Path
89
    {
90
        $this->initialize();
91
92
        return $this->data['path'];
93
    }
94
95
    public function setQuery(Query $query): AbstractPart
96
    {
97
        $this->data['query'] = $query;
98
99
        return $this;
100
    }
101
102
    public function getQuery(): Query
103
    {
104
        $this->initialize();
105
106
        return $this->data['query'];
107
    }
108
109
    public function __toString(): string
110
    {
111
        return $this->getFragment();
112
    }
113
114
    protected function doInitialize(): void
115
    {
116
        if ($this->fragment !== null) {
117
            $parsed = parse_url($this->fragment);
118
119
            if (is_array($parsed)) {
0 ignored issues
show
introduced by
The condition is_array($parsed) is always true.
Loading history...
120
                $this->data = array_merge($this->data, $parsed);
121
            }
122
        }
123
124
        foreach ($this->data as $key => $value) {
125
            $this->data[$key] = $this->preparePartValue($key, $value);
126
        }
127
    }
128
}
129