1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xparse\Parser\Helper; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response; |
6
|
|
|
use Xparse\ElementFinder\ElementFinder; |
7
|
|
|
use Xparse\ElementFinder\Helper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Create ElementFinder. |
11
|
|
|
* Convert charset to UTF-8 |
12
|
|
|
* Convert relative links to absolute |
13
|
|
|
*/ |
14
|
|
|
class ElementFinderFactory { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array|null |
18
|
|
|
*/ |
19
|
|
|
private static $supportedEncodings = null; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Response $response |
24
|
|
|
* @param string $affectedUrl |
25
|
|
|
* @return ElementFinder |
26
|
|
|
*/ |
27
|
16 |
|
public static function create(Response $response, $affectedUrl = '') { |
28
|
16 |
|
$html = $response->getBody(); |
29
|
16 |
|
$html = Helper::safeEncodeStr((string) $html); |
30
|
|
|
|
31
|
16 |
|
$supportedEncodings = self::getSupportedEncodings(); |
32
|
|
|
|
33
|
16 |
|
$contentType = $response->getHeaderLine('content-type'); |
34
|
|
|
|
35
|
16 |
|
if (!empty($contentType)) { |
36
|
3 |
|
preg_match("!^.*charset=([A-Za-z0-9-]{4,})$!", $contentType, $contentTypeData); |
37
|
3 |
|
$encoding = !empty($contentTypeData[1]) ? strtoupper(trim($contentTypeData[1])) : ''; |
38
|
3 |
|
} |
39
|
|
|
|
40
|
16 |
|
if (empty($encoding)){ |
41
|
14 |
|
preg_match("!.*<meta.*charset=[\"']?[ \t]*([A-Za-z0-9-]{4,})[ \t]*[\"']!mi", $html, $metaContentType); |
42
|
14 |
|
$encoding = !empty($metaContentType[1]) ? strtoupper(trim($metaContentType[1])) : ''; |
43
|
14 |
|
} |
44
|
|
|
|
45
|
16 |
|
if (in_array($encoding, $supportedEncodings)) { |
46
|
7 |
|
$html = mb_convert_encoding($html, 'UTF-8', $encoding); |
47
|
7 |
|
} |
48
|
|
|
|
49
|
16 |
|
$page = new ElementFinder((string) $html); |
50
|
|
|
|
51
|
16 |
|
if ($affectedUrl) { |
52
|
|
|
LinkConverter::convertUrlsToAbsolute($page, $affectedUrl); |
53
|
|
|
} |
54
|
|
|
|
55
|
16 |
|
return $page; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
16 |
|
private static function getSupportedEncodings() { |
63
|
16 |
|
if (self::$supportedEncodings !== null) { |
64
|
15 |
|
return self::$supportedEncodings; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
self::$supportedEncodings = []; |
68
|
1 |
|
foreach (mb_list_encodings() as $encoding) { |
69
|
1 |
|
if ($encoding == 'UTF-8' or $encoding == 'UTF8') { |
70
|
1 |
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
self::$supportedEncodings[] = $encoding; |
74
|
1 |
|
self::$supportedEncodings = array_merge(self::$supportedEncodings, mb_encoding_aliases($encoding)); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
1 |
|
return self::$supportedEncodings; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |