|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Crawler; |
|
4
|
|
|
|
|
5
|
|
|
class Url |
|
6
|
|
|
{ |
|
7
|
|
|
/** @var null|string */ |
|
8
|
|
|
public $scheme; |
|
9
|
|
|
|
|
10
|
|
|
/** @var null|string */ |
|
11
|
|
|
public $host; |
|
12
|
|
|
|
|
13
|
|
|
/** @var int */ |
|
14
|
|
|
public $port = 80; |
|
15
|
|
|
|
|
16
|
|
|
/** @var null|string */ |
|
17
|
|
|
public $path; |
|
18
|
|
|
|
|
19
|
|
|
/** @var null|string */ |
|
20
|
|
|
public $query; |
|
21
|
|
|
|
|
22
|
|
|
/** @var \Spatie\Crawler\HtmlNode */ |
|
23
|
|
|
public $node; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param HtmlNode $node |
|
27
|
|
|
* |
|
28
|
|
|
* @return static |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function create(HtmlNode $node) |
|
31
|
|
|
{ |
|
32
|
|
|
return new static($node, null); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $url |
|
37
|
|
|
* |
|
38
|
|
|
* @return static |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function createFromString(string $url) |
|
41
|
|
|
{ |
|
42
|
|
|
return new static(null, $url); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function __construct($node, $url = null) |
|
46
|
|
|
{ |
|
47
|
|
|
if (! is_null($node)) { |
|
48
|
|
|
$url = $node->getNode()->getAttribute('href'); |
|
49
|
|
|
} else { |
|
50
|
|
|
$url = $url; |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
$urlProperties = parse_url($url); |
|
53
|
|
|
|
|
54
|
|
|
foreach (['scheme', 'host', 'path', 'port', 'query'] as $property) { |
|
55
|
|
|
if (isset($urlProperties[$property])) { |
|
56
|
|
|
$this->$property = $urlProperties[$property]; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
$this->node = (! is_null($node)) ? $node : null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function isRelative(): bool |
|
63
|
|
|
{ |
|
64
|
|
|
return is_null($this->host); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function isProtocolIndependent(): bool |
|
68
|
|
|
{ |
|
69
|
|
|
return is_null($this->scheme); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function hasCrawlableScheme(): bool |
|
73
|
|
|
{ |
|
74
|
|
|
return in_array($this->scheme, [null, 'http', 'https']); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $scheme |
|
79
|
|
|
* |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setScheme(string $scheme) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->scheme = $scheme; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $host |
|
91
|
|
|
* |
|
92
|
|
|
* @return $this |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setHost(string $host) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->host = $host; |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param $port |
|
103
|
|
|
* |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setPort(int $port) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->port = $port; |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
|
|
public function removeFragment() |
|
117
|
|
|
{ |
|
118
|
|
|
$this->path = explode('#', $this->path)[0]; |
|
119
|
|
|
|
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return null|string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function path() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->path; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param int|null $index |
|
133
|
|
|
* |
|
134
|
|
|
* @return array|null|string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function segments(int $index = null) |
|
137
|
|
|
{ |
|
138
|
|
|
$segments = collect(explode('/', $this->path())) |
|
139
|
|
|
->filter(function ($value) { |
|
140
|
|
|
return $value !== ''; |
|
141
|
|
|
}) |
|
142
|
|
|
->values() |
|
143
|
|
|
->toArray(); |
|
144
|
|
|
|
|
145
|
|
|
if (! is_null($index)) { |
|
146
|
|
|
return $this->segment($index); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $segments; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param int $index |
|
154
|
|
|
* |
|
155
|
|
|
* @return string|null |
|
156
|
|
|
*/ |
|
157
|
|
|
public function segment(int $index) |
|
158
|
|
|
{ |
|
159
|
|
|
if (! isset($this->segments()[$index - 1])) { |
|
160
|
|
|
return; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $this->segments()[$index - 1]; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function isEqual(Url $otherUrl): bool |
|
167
|
|
|
{ |
|
168
|
|
|
return (string) $this === (string) $otherUrl; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
public function __toString() |
|
175
|
|
|
{ |
|
176
|
|
|
$path = $this->startsWith($this->path, '/') ? substr($this->path, 1) : $this->path; |
|
177
|
|
|
|
|
178
|
|
|
$port = ($this->port === 80 ? '' : ":{$this->port}"); |
|
179
|
|
|
|
|
180
|
|
|
$queryString = (is_null($this->query) ? '' : "?{$this->query}"); |
|
181
|
|
|
|
|
182
|
|
|
return "{$this->scheme}://{$this->host}{$port}/{$path}{$queryString}"; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param string|null $haystack |
|
187
|
|
|
* @param string|array $needles |
|
188
|
|
|
* |
|
189
|
|
|
* @return bool |
|
190
|
|
|
*/ |
|
191
|
|
|
public function startsWith($haystack, $needles): bool |
|
192
|
|
|
{ |
|
193
|
|
|
foreach ((array) $needles as $needle) { |
|
194
|
|
|
if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) { |
|
195
|
|
|
return true; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return false; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
This checks looks for cases where a variable has been assigned to itself.
This assignement can be removed without consequences.