FeedSet.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  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-2009 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 (!is_null($uri)) {
  84. $link = rtrim($uri, '/') . '/' . ltrim($link, '/');
  85. if (!Zend_Uri::check($link)) {
  86. $link = null;
  87. }
  88. }
  89. }
  90. return $link;
  91. }
  92. /**
  93. * Supports lazy loading of feeds using Zend_Feed_Reader::import() but
  94. * delegates any other operations to the parent class.
  95. *
  96. * @param string $offset
  97. * @return mixed
  98. * @uses Zend_Feed_Reader
  99. */
  100. public function offsetGet($offset)
  101. {
  102. if ($offset == 'feed' && !$this->offsetExists('feed')) {
  103. if (!$this->offsetExists('href')) {
  104. return null;
  105. }
  106. $feed = Zend_Feed_Reader::import($this->offsetGet('href'));
  107. $this->offsetSet('feed', $feed);
  108. return $feed;
  109. }
  110. return parent::offsetGet($offset);
  111. }
  112. }