FeedSet.php in Zircon Profile 8.0
File
vendor/zendframework/zend-feed/src/Reader/FeedSet.php
View source
<?php
namespace Zend\Feed\Reader;
use ArrayObject;
use DOMNodeList;
use Zend\Feed\Uri;
class FeedSet extends ArrayObject {
public $rss = null;
public $rdf = null;
public $atom = null;
public function addLinks(DOMNodeList $links, $uri) {
foreach ($links as $link) {
if (strtolower($link
->getAttribute('rel')) !== 'alternate' || !$link
->getAttribute('type') || !$link
->getAttribute('href')) {
continue;
}
if (!isset($this->rss) && $link
->getAttribute('type') == 'application/rss+xml') {
$this->rss = $this
->absolutiseUri(trim($link
->getAttribute('href')), $uri);
}
elseif (!isset($this->atom) && $link
->getAttribute('type') == 'application/atom+xml') {
$this->atom = $this
->absolutiseUri(trim($link
->getAttribute('href')), $uri);
}
elseif (!isset($this->rdf) && $link
->getAttribute('type') == 'application/rdf+xml') {
$this->rdf = $this
->absolutiseUri(trim($link
->getAttribute('href')), $uri);
}
$this[] = new static([
'rel' => 'alternate',
'type' => $link
->getAttribute('type'),
'href' => $this
->absolutiseUri(trim($link
->getAttribute('href')), $uri),
]);
}
}
protected function absolutiseUri($link, $uri = null) {
$linkUri = Uri::factory($link);
if (!$linkUri
->isAbsolute() or !$linkUri
->isValid()) {
if ($uri !== null) {
$uri = Uri::factory($uri);
if ($link[0] !== '/') {
$link = $uri
->getPath() . '/' . $link;
}
$link = sprintf('%s://%s/%s', $uri
->getScheme() ?: 'http', $uri
->getHost(), $this
->canonicalizePath($link));
if (!Uri::factory($link)
->isValid()) {
$link = null;
}
}
}
return $link;
}
protected function canonicalizePath($path) {
$parts = array_filter(explode('/', $path));
$absolutes = [];
foreach ($parts as $part) {
if ('.' == $part) {
continue;
}
if ('..' == $part) {
array_pop($absolutes);
}
else {
$absolutes[] = $part;
}
}
return implode('/', $absolutes);
}
public function offsetGet($offset) {
if ($offset == 'feed' && !$this
->offsetExists('feed')) {
if (!$this
->offsetExists('href')) {
return;
}
$feed = Reader::import($this
->offsetGet('href'));
$this
->offsetSet('feed', $feed);
return $feed;
}
return parent::offsetGet($offset);
}
}