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;
16 class FeedSet extends ArrayObject
25 * Import a DOMNodeList from any document containing a set of links
26 * for alternate versions of a document, which will normally refer to
27 * RSS/RDF/Atom feeds for the current document.
29 * All such links are stored internally, however the first instance of
30 * each RSS, RDF or Atom type has its URI stored as a public property
31 * as a shortcut where the use case is simply to get a quick feed ref.
33 * Note that feeds are not loaded at this point, but will be lazy
34 * loaded automatically when each links 'feed' array key is accessed.
36 * @param DOMNodeList $links
40 public function addLinks(DOMNodeList $links, $uri)
42 foreach ($links as $link) {
43 if (strtolower($link->getAttribute('rel')) !== 'alternate'
44 || ! $link->getAttribute('type') || ! $link->getAttribute('href')) {
47 if (! isset($this->rss) && $link->getAttribute('type') == 'application/rss+xml') {
48 $this->rss = $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
49 } elseif (! isset($this->atom) && $link->getAttribute('type') == 'application/atom+xml') {
50 $this->atom = $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
51 } elseif (! isset($this->rdf) && $link->getAttribute('type') == 'application/rdf+xml') {
52 $this->rdf = $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
54 $this[] = new static([
56 'type' => $link->getAttribute('type'),
57 'href' => $this->absolutiseUri(trim($link->getAttribute('href')), $uri),
63 * Attempt to turn a relative URI into an absolute URI
66 * @param string $uri OPTIONAL
67 * @return string|null absolutised link or null if invalid
69 protected function absolutiseUri($link, $uri = null)
71 $linkUri = Uri::factory($link);
72 if ($linkUri->isAbsolute()) {
73 // invalid absolute link can not be recovered
74 return $linkUri->isValid() ? $link : null;
79 $uri = Uri::factory($uri);
80 $scheme = $uri->getScheme() ?: $scheme;
83 if ($linkUri->getHost()) {
84 $link = $this->resolveSchemeRelativeUri($link, $scheme);
85 } elseif ($uri !== null) {
86 $link = $this->resolveRelativeUri($link, $scheme, $uri->getHost(), $uri->getPath());
89 if (! Uri::factory($link)->isValid()) {
97 * Resolves scheme relative link to absolute
100 * @param string $scheme
103 private function resolveSchemeRelativeUri($link, $scheme)
105 $link = ltrim($link, '/');
106 return sprintf('%s://%s', $scheme, $link);
110 * Resolves relative link to absolute
112 * @param string $link
113 * @param string $scheme
114 * @param string $host
115 * @param string $uriPath
118 private function resolveRelativeUri($link, $scheme, $host, $uriPath)
120 if ($link[0] !== '/') {
121 $link = $uriPath . '/' . $link;
127 $this->canonicalizePath($link)
132 * Canonicalize relative path
134 protected function canonicalizePath($path)
136 $parts = array_filter(explode('/', $path));
138 foreach ($parts as $part) {
143 array_pop($absolutes);
145 $absolutes[] = $part;
148 return implode('/', $absolutes);
152 * Supports lazy loading of feeds using Reader::import() but
153 * delegates any other operations to the parent class.
155 * @param string $offset
158 public function offsetGet($offset)
160 if ($offset == 'feed' && ! $this->offsetExists('feed')) {
161 if (! $this->offsetExists('href')) {
164 $feed = Reader::import($this->offsetGet('href'));
165 $this->offsetSet('feed', $feed);
168 return parent::offsetGet($offset);