3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 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
28 * Import a DOMNodeList from any document containing a set of links
29 * for alternate versions of a document, which will normally refer to
30 * RSS/RDF/Atom feeds for the current document.
32 * All such links are stored internally, however the first instance of
33 * each RSS, RDF or Atom type has its URI stored as a public property
34 * as a shortcut where the use case is simply to get a quick feed ref.
36 * Note that feeds are not loaded at this point, but will be lazy
37 * loaded automatically when each links 'feed' array key is accessed.
39 * @param DOMNodeList $links
43 public function addLinks(DOMNodeList
$links, $uri)
45 foreach ($links as $link) {
46 if (strtolower($link->getAttribute('rel')) !== 'alternate'
47 ||
!$link->getAttribute('type') ||
!$link->getAttribute('href')) {
50 if (!isset($this->rss
) && $link->getAttribute('type') == 'application/rss+xml') {
51 $this->rss
= $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
52 } elseif (!isset($this->atom
) && $link->getAttribute('type') == 'application/atom+xml') {
53 $this->atom
= $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
54 } elseif (!isset($this->rdf
) && $link->getAttribute('type') == 'application/rdf+xml') {
55 $this->rdf
= $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
57 $this[] = new static(array(
59 'type' => $link->getAttribute('type'),
60 'href' => $this->absolutiseUri(trim($link->getAttribute('href')), $uri),
66 * Attempt to turn a relative URI into an absolute URI
68 protected function absolutiseUri($link, $uri = null)
70 $linkUri = Uri
::factory($link);
71 if (!$linkUri->isAbsolute() or !$linkUri->isValid()) {
73 $uri = Uri
::factory($uri);
75 if ($link[0] !== '/') {
76 $link = $uri->getPath() . '/' . $link;
79 $link = $uri->getScheme() . '://' . $uri->getHost() . '/' . $this->canonicalizePath($link);
80 if (!Uri
::factory($link)->isValid()) {
89 * Canonicalize relative path
91 protected function canonicalizePath($path)
93 $parts = array_filter(explode('/', $path));
95 foreach ($parts as $part) {
100 array_pop($absolutes);
102 $absolutes[] = $part;
105 return implode('/', $absolutes);
109 * Supports lazy loading of feeds using Reader::import() but
110 * delegates any other operations to the parent class.
112 * @param string $offset
115 public function offsetGet($offset)
117 if ($offset == 'feed' && !$this->offsetExists('feed')) {
118 if (!$this->offsetExists('href')) {
121 $feed = Reader
::import($this->offsetGet('href'));
122 $this->offsetSet('feed', $feed);
125 return parent
::offsetGet($offset);