123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- declare(strict_types = 1);
- namespace Component\FreeNetwork\Util\LrddMethod;
- use Component\FreeNetwork\Util\LrddMethod;
- use XML_XRD_Element_Link;
- class LrddMethodLinkHtml extends LRDDMethod
- {
-
- public function discover($uri)
- {
- $response = self::fetchUrl($uri);
- return self::parse($response->getContent());
- }
-
- public function parse($html)
- {
- $links = [];
- preg_match('/<head(\s[^>]*)?>(.*?)<\/head>/is', $html, $head_matches);
- if (\count($head_matches) != 3) {
- return [];
- }
- [, , $head_html] = $head_matches;
- preg_match_all('/<link\s[^>]*>/i', $head_html, $link_matches);
- foreach ($link_matches[0] as $link_html) {
- $link_url = null;
- $link_rel = null;
- $link_type = null;
- preg_match('/\srel=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $rel_matches);
- if (\count($rel_matches) > 3) {
- $link_rel = $rel_matches[3];
- } elseif (\count($rel_matches) > 1) {
- $link_rel = $rel_matches[1];
- }
- preg_match('/\shref=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $href_matches);
- if (\count($href_matches) > 3) {
- $link_uri = $href_matches[3];
- } elseif (\count($href_matches) > 1) {
- $link_uri = $href_matches[1];
- }
- preg_match('/\stype=(("|\')([^\\2]*?)\\2|[^"\'\s]+)/i', $link_html, $type_matches);
- if (\count($type_matches) > 3) {
- $link_type = $type_matches[3];
- } elseif (\count($type_matches) > 1) {
- $link_type = $type_matches[1];
- }
- $links[] = new XML_XRD_Element_Link($link_rel, $link_uri, $link_type);
- }
- return $links;
- }
- }
|