3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Feed\Reader
;
18 class FeedSet
extends ArrayObject
27 * Import a DOMNodeList from any document containing a set of links
28 * for alternate versions of a document, which will normally refer to
29 * RSS/RDF/Atom feeds for the current document.
31 * All such links are stored internally, however the first instance of
32 * each RSS, RDF or Atom type has its URI stored as a public property
33 * as a shortcut where the use case is simply to get a quick feed ref.
35 * Note that feeds are not loaded at this point, but will be lazy
36 * loaded automatically when each links 'feed' array key is accessed.
38 * @param DOMNodeList $links
42 public function addLinks(DOMNodeList
$links, $uri)
44 foreach ($links as $link) {
45 if (strtolower($link->getAttribute('rel')) !== 'alternate'
46 ||
!$link->getAttribute('type') ||
!$link->getAttribute('href')) {
49 if (!isset($this->rss
) && $link->getAttribute('type') == 'application/rss+xml') {
50 $this->rss
= $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
51 } elseif (!isset($this->atom
) && $link->getAttribute('type') == 'application/atom+xml') {
52 $this->atom
= $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
53 } elseif (!isset($this->rdf
) && $link->getAttribute('type') == 'application/rdf+xml') {
54 $this->rdf
= $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
56 $this[] = new static(array(
58 'type' => $link->getAttribute('type'),
59 'href' => $this->absolutiseUri(trim($link->getAttribute('href')), $uri),
65 * Attempt to turn a relative URI into an absolute URI
67 protected function absolutiseUri($link, $uri = null)
69 $linkUri = Uri
::factory($link);
70 if (!$linkUri->isAbsolute() or !$linkUri->isValid()) {
72 $uri = Uri
::factory($uri);
74 if ($link[0] !== '/') {
75 $link = $uri->getPath() . '/' . $link;
78 $link = $uri->getScheme() . '://' . $uri->getHost() . '/' . $this->canonicalizePath($link);
79 if (!Uri
::factory($link)->isValid()) {
88 * Canonicalize relative path
90 protected function canonicalizePath($path)
92 $parts = array_filter(explode('/', $path));
94 foreach ($parts as $part) {
99 array_pop($absolutes);
101 $absolutes[] = $part;
104 return implode('/', $absolutes);
108 * Supports lazy loading of feeds using Reader::import() but
109 * delegates any other operations to the parent class.
111 * @param string $offset
114 public function offsetGet($offset)
116 if ($offset == 'feed' && !$this->offsetExists('feed')) {
117 if (!$this->offsetExists('href')) {
120 $feed = Reader
::import($this->offsetGet('href'));
121 $this->offsetSet('feed', $feed);
124 return parent
::offsetGet($offset);