Conditions | 8 |
Paths | 36 |
Total Lines | 37 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Tests | 23 |
CRAP Score | 8.0327 |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
15 | 7 | public static function create($response, $url = '') |
|
16 | { |
||
17 | 7 | $html = $response->getBody(); |
|
18 | 7 | $html = Helper::safeEncodeStr((string)$html); |
|
19 | |||
20 | 7 | $supportedEncodings = []; |
|
21 | 7 | foreach (mb_list_encodings() as $encoding) { |
|
22 | 7 | if ($encoding == 'UTF-8' || $encoding == 'UTF8') { |
|
23 | 7 | continue; |
|
24 | } |
||
25 | |||
26 | 7 | $supportedEncodings[] = $encoding; |
|
27 | 7 | $supportedEncodings = array_merge($supportedEncodings, mb_encoding_aliases($encoding)); |
|
28 | 7 | } |
|
29 | |||
30 | 7 | $contentType = $response->getHeaderLine('content-type'); |
|
31 | |||
32 | 7 | if ($contentType) { |
|
33 | 1 | preg_match("!^.*charset=([A-Za-z0-9-]{4,})$!", $contentType, $contentTypeData); |
|
34 | 1 | $encoding = strtoupper(trim($contentTypeData[1])); |
|
35 | 1 | } else { |
|
36 | 7 | preg_match("!.*<meta.*charset=\"?([A-Za-z0-9-]{4,})\"!mi", $html, $metaContentType); |
|
37 | 7 | $encoding = !empty($metaContentType[1]) ? strtoupper(trim($metaContentType[1])) : ''; |
|
38 | } |
||
39 | |||
40 | 7 | if (in_array($encoding, $supportedEncodings)) { |
|
41 | 2 | $html = mb_convert_encoding($html, 'UTF-8', $encoding); |
|
42 | 2 | } |
|
43 | |||
44 | 7 | $page = new ElementFinder((string)$html); |
|
45 | |||
46 | 7 | if ($url) { |
|
47 | LinkConverter::convertUrlsToAbsolute($page, $url); |
||
48 | } |
||
49 | |||
50 | 7 | return $page; |
|
51 | } |
||
52 | } |