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