1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Purl package, a project by Jonathan H. Wage. |
5
|
|
|
* |
6
|
|
|
* (c) 2013 Jonathan H. Wage |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Purl; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Fragment represents the part of a Url after the hash-mark (#). |
16
|
|
|
* |
17
|
|
|
* @author Jonathan H. Wage <[email protected]> |
18
|
|
|
* |
19
|
|
|
* @property \Purl\Path $path |
20
|
|
|
* @property \Purl\Query $query |
21
|
|
|
*/ |
22
|
|
|
class Fragment extends AbstractPart |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string The original fragment string. |
26
|
|
|
*/ |
27
|
|
|
private $fragment; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Construct a new Fragment instance. |
31
|
|
|
* |
32
|
|
|
* @param string|Path|null $fragment Path instance of string fragment. |
33
|
|
|
* @param Query|null $query |
34
|
|
|
*/ |
35
|
65 |
|
public function __construct($fragment = null, Query $query = null) |
36
|
|
|
{ |
37
|
65 |
|
$this->data = [ |
38
|
|
|
'path' => null, |
39
|
|
|
'query' => null, |
40
|
|
|
]; |
41
|
|
|
|
42
|
65 |
|
$this->partClassMap = [ |
43
|
|
|
'path' => Path::class, |
44
|
|
|
'query' => Query::class, |
45
|
|
|
]; |
46
|
|
|
|
47
|
65 |
|
if ($fragment instanceof Path) { |
48
|
4 |
|
$this->initialized = true; |
49
|
4 |
|
$this->data['path'] = $fragment; |
50
|
|
|
} else { |
51
|
64 |
|
$this->fragment = $fragment; |
52
|
|
|
} |
53
|
|
|
|
54
|
65 |
|
$this->data['query'] = $query; |
55
|
65 |
|
} |
56
|
|
|
|
57
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection */ |
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
* @override |
61
|
|
|
*/ |
62
|
1 |
|
public function set($key, $value) |
63
|
|
|
{ |
64
|
1 |
|
$this->initialize(); |
65
|
1 |
|
$this->data[$key] = $this->preparePartValue($key, $value); |
66
|
|
|
|
67
|
1 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Builds a string fragment from this Fragment instance internal data and returns it. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
24 |
|
public function getFragment(): string |
76
|
|
|
{ |
77
|
24 |
|
$this->initialize(); |
78
|
|
|
|
79
|
24 |
|
return sprintf( |
80
|
24 |
|
'%s%s', $this->path, $this->query->getQuery() |
81
|
11 |
|
? '?' . $this->query->getQuery() |
82
|
24 |
|
: '' |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the string fragment for this Fragment instance and sets initialized to false. |
88
|
|
|
* |
89
|
|
|
* @param $fragment |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
1 |
|
public function setFragment($fragment) |
94
|
|
|
{ |
95
|
1 |
|
$this->initialized = false; |
96
|
1 |
|
$this->data = []; |
97
|
1 |
|
$this->fragment = $fragment; |
98
|
|
|
|
99
|
1 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the Path instance. |
104
|
|
|
* |
105
|
|
|
* @param Path $path |
106
|
|
|
* |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
1 |
|
public function setPath(Path $path) |
110
|
|
|
{ |
111
|
1 |
|
$this->data['path'] = $path; |
112
|
|
|
|
113
|
1 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the Path instance. |
118
|
|
|
* |
119
|
|
|
* @return Path |
120
|
|
|
*/ |
121
|
1 |
|
public function getPath(): Path |
122
|
|
|
{ |
123
|
1 |
|
$this->initialize(); |
124
|
|
|
|
125
|
1 |
|
return $this->data['path']; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set the Query instance. |
130
|
|
|
* |
131
|
|
|
* @param Query $query |
132
|
|
|
* |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
1 |
|
public function setQuery(Query $query) |
136
|
|
|
{ |
137
|
1 |
|
$this->data['query'] = $query; |
138
|
|
|
|
139
|
1 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get the Query instance. |
144
|
|
|
* |
145
|
|
|
* @return Query |
146
|
|
|
*/ |
147
|
1 |
|
public function getQuery(): Query |
148
|
|
|
{ |
149
|
1 |
|
$this->initialize(); |
150
|
|
|
|
151
|
1 |
|
return $this->data['query']; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritDoc |
156
|
|
|
*/ |
157
|
22 |
|
public function __toString() |
158
|
|
|
{ |
159
|
22 |
|
return $this->getFragment(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritDoc |
164
|
|
|
*/ |
165
|
22 |
|
protected function doInitialize() |
166
|
|
|
{ |
167
|
22 |
|
if ($this->fragment) { |
168
|
9 |
|
$this->data = array_merge($this->data, parse_url($this->fragment)); |
169
|
|
|
} |
170
|
|
|
|
171
|
22 |
|
foreach ($this->data as $key => &$value) { |
172
|
22 |
|
$value = $this->preparePartValue($key, $value); |
173
|
|
|
} |
174
|
22 |
|
} |
175
|
|
|
} |
176
|
|
|
|