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