FeedSet.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * @category Zend
  27. * @package Zend_Feed_Reader
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Feed_Reader_FeedSet extends ArrayObject
  32. {
  33. public $rss = null;
  34. public $rdf = null;
  35. public $atom = null;
  36. /**
  37. * Import a DOMNodeList from any document containing a set of links
  38. * for alternate versions of a document, which will normally refer to
  39. * RSS/RDF/Atom feeds for the current document.
  40. *
  41. * All such links are stored internally, however the first instance of
  42. * each RSS, RDF or Atom type has its URI stored as a public property
  43. * as a shortcut where the use case is simply to get a quick feed ref.
  44. *
  45. * Note that feeds are not loaded at this point, but will be lazy
  46. * loaded automatically when each links 'feed' array key is accessed.
  47. *
  48. * @param DOMNodeList $links
  49. * @return void
  50. */
  51. public function addLinks(DOMNodeList $links)
  52. {
  53. foreach ($links as $link) {
  54. if (strtolower($link->getAttribute('rel')) !== 'alternate'
  55. || !$link->getAttribute('type') || !$link->getAttribute('href')) {
  56. continue;
  57. }
  58. if (!isset($this->rss) && $link->getAttribute('type') == 'application/rss+xml') {
  59. $this->rss = trim($link->getAttribute('href'));
  60. } elseif(!isset($this->atom) && $link->getAttribute('type') == 'application/atom+xml') {
  61. $this->atom = trim($link->getAttribute('href'));
  62. } elseif(!isset($this->rdf) && $link->getAttribute('type') == 'application/rdf+xml') {
  63. $this->rdf = trim($link->getAttribute('href'));
  64. }
  65. $this[] = new self(array(
  66. 'rel' => 'alternate',
  67. 'type' => $link->getAttribute('type'),
  68. 'href' => trim($link->getAttribute('href')),
  69. ));
  70. }
  71. }
  72. /**
  73. * Supports lazy loading of feeds using Zend_Feed_Reader::import() but
  74. * delegates any other operations to the parent class.
  75. *
  76. * @param string $offset
  77. * @return mixed
  78. * @uses Zend_Feed_Reader
  79. */
  80. public function offsetGet($offset)
  81. {
  82. if ($offset == 'feed' && !$this->offsetExists('feed')) {
  83. if (!$this->offsetExists('href')) {
  84. return null;
  85. }
  86. $feed = Zend_Feed_Reader::import($this->offsetGet('href'));
  87. $this->offsetSet('feed', $feed);
  88. return $feed;
  89. }
  90. return parent::offsetGet($offset);
  91. }
  92. }