HttpHelpers.php in Feeds 8.3
File
src/Component/HttpHelpers.php
View source
<?php
namespace Drupal\feeds\Component;
class HttpHelpers {
use XmlParserTrait;
public static function findLinkHeader(array $headers, $relation) {
$headers = array_change_key_case($headers);
if (!isset($headers['link'])) {
return FALSE;
}
foreach ((array) $headers['link'] as $link) {
if ($link = static::parseLinkRelation($link, $relation)) {
return $link;
}
}
return FALSE;
}
public static function parseLinkRelation($link_header, $relation) {
if (!preg_match_all('/<([^>]*)>\\s*;.*?rel\\s*=(.+?)(?:;|$)/is', trim($link_header), $matches)) {
return '';
}
foreach ($matches[2] as $delta => $match) {
$match = trim($match);
$len = strlen($match);
if ($match[0] === '"' && $match[$len - 1] === '"') {
$match = substr($match, 1, $len - 2);
}
preg_replace('/\\s+/s', ' ', trim($match));
if (in_array($relation, explode(' ', $match), TRUE)) {
return $matches[1][$delta];
}
}
return '';
}
public static function findRelationFromXml($xml, $relation) {
if (!isset($xml[0])) {
return FALSE;
}
$document = static::getDomDocument($xml);
$xpath = new \DOMXPath($document);
$list = $xpath
->query('//*[local-name() = "link" and @rel = "' . $relation . '"]/@href');
if ($list->length === 0) {
return FALSE;
}
return $list
->item(0)->value;
}
}