/library/Zend/Feed/Reader/FeedSet.php

https://bitbucket.org/hamidrezas/melobit · PHP · 148 lines · 74 code · 13 blank · 61 comment · 23 complexity · 655d939ad24a6d311a958280d075fb47 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed_Reader
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: FeedSet.php 24594 2012-01-05 21:27:01Z matthew $
  20. */
  21. /**
  22. * @see Zend_Feed_Reader
  23. */
  24. require_once 'Zend/Feed/Reader.php';
  25. /**
  26. * @see Zend_Uri
  27. */
  28. require_once 'Zend/Uri.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Feed_Reader
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Feed_Reader_FeedSet extends ArrayObject
  36. {
  37. public $rss = null;
  38. public $rdf = null;
  39. public $atom = null;
  40. /**
  41. * Import a DOMNodeList from any document containing a set of links
  42. * for alternate versions of a document, which will normally refer to
  43. * RSS/RDF/Atom feeds for the current document.
  44. *
  45. * All such links are stored internally, however the first instance of
  46. * each RSS, RDF or Atom type has its URI stored as a public property
  47. * as a shortcut where the use case is simply to get a quick feed ref.
  48. *
  49. * Note that feeds are not loaded at this point, but will be lazy
  50. * loaded automatically when each links 'feed' array key is accessed.
  51. *
  52. * @param DOMNodeList $links
  53. * @param string $uri
  54. * @return void
  55. */
  56. public function addLinks(DOMNodeList $links, $uri)
  57. {
  58. foreach ($links as $link) {
  59. if (strtolower($link->getAttribute('rel')) !== 'alternate'
  60. || !$link->getAttribute('type') || !$link->getAttribute('href')) {
  61. continue;
  62. }
  63. if (!isset($this->rss) && $link->getAttribute('type') == 'application/rss+xml') {
  64. $this->rss = $this->_absolutiseUri(trim($link->getAttribute('href')), $uri);
  65. } elseif(!isset($this->atom) && $link->getAttribute('type') == 'application/atom+xml') {
  66. $this->atom = $this->_absolutiseUri(trim($link->getAttribute('href')), $uri);
  67. } elseif(!isset($this->rdf) && $link->getAttribute('type') == 'application/rdf+xml') {
  68. $this->rdf = $this->_absolutiseUri(trim($link->getAttribute('href')), $uri);
  69. }
  70. $this[] = new self(array(
  71. 'rel' => 'alternate',
  72. 'type' => $link->getAttribute('type'),
  73. 'href' => $this->_absolutiseUri(trim($link->getAttribute('href')), $uri),
  74. ));
  75. }
  76. }
  77. /**
  78. * Attempt to turn a relative URI into an absolute URI
  79. */
  80. protected function _absolutiseUri($link, $uri = null)
  81. {
  82. if (!Zend_Uri::check($link)) {
  83. if ($uri !== null) {
  84. $uri = Zend_Uri::factory($uri);
  85. if ($link[0] !== '/') {
  86. $link = $uri->getPath() . '/' . $link;
  87. }
  88. $link = $uri->getScheme() . '://' . $uri->getHost() . '/' . $this->_canonicalizePath($link);
  89. if (!Zend_Uri::check($link)) {
  90. $link = null;
  91. }
  92. }
  93. }
  94. return $link;
  95. }
  96. /**
  97. * Canonicalize relative path
  98. */
  99. protected function _canonicalizePath($path)
  100. {
  101. $parts = array_filter(explode('/', $path));
  102. $absolutes = array();
  103. foreach ($parts as $part) {
  104. if ('.' == $part) {
  105. continue;
  106. }
  107. if ('..' == $part) {
  108. array_pop($absolutes);
  109. } else {
  110. $absolutes[] = $part;
  111. }
  112. }
  113. return implode('/', $absolutes);
  114. }
  115. /**
  116. * Supports lazy loading of feeds using Zend_Feed_Reader::import() but
  117. * delegates any other operations to the parent class.
  118. *
  119. * @param string $offset
  120. * @return mixed
  121. * @uses Zend_Feed_Reader
  122. */
  123. public function offsetGet($offset)
  124. {
  125. if ($offset == 'feed' && !$this->offsetExists('feed')) {
  126. if (!$this->offsetExists('href')) {
  127. return null;
  128. }
  129. $feed = Zend_Feed_Reader::import($this->offsetGet('href'));
  130. $this->offsetSet('feed', $feed);
  131. return $feed;
  132. }
  133. return parent::offsetGet($offset);
  134. }
  135. }