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