Feed.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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_Writer
  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_Date
  23. */
  24. require_once 'Zend/Date.php';
  25. /**
  26. * @see Zend_Date
  27. */
  28. require_once 'Zend/Uri.php';
  29. /**
  30. * @see Zend_Feed_Writer
  31. */
  32. require_once 'Zend/Feed/Writer.php';
  33. /**
  34. * @see Zend_Feed_Writer_Entry
  35. */
  36. require_once 'Zend/Feed/Writer/Entry.php';
  37. /**
  38. * @see Zend_Feed_Writer_Renderer_Feed_Atom
  39. */
  40. require_once 'Zend/Feed/Writer/Renderer/Feed/Atom.php';
  41. /**
  42. * @see Zend_Feed_Writer_Renderer_Feed_Rss
  43. */
  44. require_once 'Zend/Feed/Writer/Renderer/Feed/Rss.php';
  45. /**
  46. * @category Zend
  47. * @package Zend_Feed_Writer
  48. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  49. * @license http://framework.zend.com/license/new-bsd New BSD License
  50. */
  51. class Zend_Feed_Writer_Feed implements Iterator, Countable
  52. {
  53. /**
  54. * Contains all Feed level date to append in feed output
  55. *
  56. * @var array
  57. */
  58. protected $_data = array();
  59. /**
  60. * Contains all entry objects
  61. *
  62. * @var array
  63. */
  64. protected $_entries = array();
  65. /**
  66. * A pointer for the iterator to keep track of the entries array
  67. *
  68. * @var int
  69. */
  70. protected $_entriesKey = 0;
  71. /**
  72. * Holds the value "atom" or "rss" depending on the feed type set when
  73. * when last exported.
  74. *
  75. * @var string
  76. */
  77. protected $_type = null;
  78. /**
  79. * Constructor: Primarily triggers the registration of core extensions and
  80. * loads those appropriate to this data container.
  81. *
  82. * @return void
  83. */
  84. public function __construct()
  85. {
  86. Zend_Feed_Writer::registerCoreExtensions();
  87. $this->_loadExtensions();
  88. }
  89. /**
  90. * Set a single author
  91. *
  92. * @param int $index
  93. * @return string|null
  94. */
  95. public function addAuthor($name, $email = null, $uri = null)
  96. {
  97. $author = array();
  98. if (is_array($name)) {
  99. if (!array_key_exists('name', $name) || empty($name['name']) || !is_string($name['name'])) {
  100. require_once 'Zend/Feed/Exception.php';
  101. throw new Zend_Feed_Exception('Invalid parameter: author array must include a "name" key with a non-empty string value');
  102. }
  103. $author['name'] = $name['name'];
  104. if (isset($name['email'])) {
  105. if (empty($name['email']) || !is_string($name['email'])) {
  106. require_once 'Zend/Feed/Exception.php';
  107. throw new Zend_Feed_Exception('Invalid parameter: "email" array value must be a non-empty string');
  108. }
  109. $author['email'] = $name['email'];
  110. }
  111. if (isset($name['uri'])) {
  112. if (empty($name['uri']) || !is_string($name['uri']) || !Zend_Uri::check($name['uri'])) {
  113. require_once 'Zend/Feed/Exception.php';
  114. throw new Zend_Feed_Exception('Invalid parameter: "uri" array value must be a non-empty string and valid URI/IRI');
  115. }
  116. $author['uri'] = $name['uri'];
  117. }
  118. } else {
  119. if (empty($name['name']) || !is_string($name['name'])) {
  120. require_once 'Zend/Feed/Exception.php';
  121. throw new Zend_Feed_Exception('Invalid parameter: "name" must be a non-empty string value');
  122. }
  123. $author['name'] = $name;
  124. if (isset($email)) {
  125. if (empty($email) || !is_string($email)) {
  126. require_once 'Zend/Feed/Exception.php';
  127. throw new Zend_Feed_Exception('Invalid parameter: "email" value must be a non-empty string');
  128. }
  129. $author['email'] = $email;
  130. }
  131. if (isset($uri)) {
  132. if (empty($uri) || !is_string($uri) || !Zend_Uri::check($uri)) {
  133. require_once 'Zend/Feed/Exception.php';
  134. throw new Zend_Feed_Exception('Invalid parameter: "uri" value must be a non-empty string and valid URI/IRI');
  135. }
  136. $author['uri'] = $uri;
  137. }
  138. }
  139. $this->_data['authors'][] = $author;
  140. }
  141. /**
  142. * Set an array with feed authors
  143. *
  144. * @return array
  145. */
  146. public function addAuthors(array $authors)
  147. {
  148. foreach($authors as $author) {
  149. $this->addAuthor($author);
  150. }
  151. }
  152. /**
  153. * Set the copyright entry
  154. *
  155. * @return string|null
  156. */
  157. public function setCopyright($copyright)
  158. {
  159. if (empty($copyright) || !is_string($copyright)) {
  160. require_once 'Zend/Feed/Exception.php';
  161. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
  162. }
  163. $this->_data['copyright'] = $copyright;
  164. }
  165. /**
  166. * Set the feed creation date
  167. *
  168. * @param null|integer|Zend_Date
  169. */
  170. public function setDateCreated($date = null)
  171. {
  172. $zdate = null;
  173. if (is_null($date)) {
  174. $zdate = new Zend_Date;
  175. } elseif (ctype_digit($date) && strlen($date) == 10) {
  176. $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP);
  177. } elseif ($date instanceof Zend_Date) {
  178. $zdate = $date;
  179. } else {
  180. require_once 'Zend/Feed/Exception.php';
  181. throw new Zend_Feed_Exception('Invalid Zend_Date object or UNIX Timestamp passed as parameter');
  182. }
  183. $this->_data['dateCreated'] = $zdate;
  184. }
  185. /**
  186. * Set the feed modification date
  187. *
  188. * @param null|integer|Zend_Date
  189. */
  190. public function setDateModified($date = null)
  191. {
  192. $zdate = null;
  193. if (is_null($date)) {
  194. $zdate = new Zend_Date;
  195. } elseif (ctype_digit($date) && strlen($date) == 10) {
  196. $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP);
  197. } elseif ($date instanceof Zend_Date) {
  198. $zdate = $date;
  199. } else {
  200. require_once 'Zend/Feed/Exception.php';
  201. throw new Zend_Feed_Exception('Invalid Zend_Date object or UNIX Timestamp passed as parameter');
  202. }
  203. $this->_data['dateModified'] = $zdate;
  204. }
  205. /**
  206. * Set the feed description
  207. *
  208. * @return string|null
  209. */
  210. public function setDescription($description)
  211. {
  212. if (empty($description) || !is_string($description)) {
  213. require_once 'Zend/Feed/Exception.php';
  214. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
  215. }
  216. $this->_data['description'] = $description;
  217. }
  218. /**
  219. * Set the feed generator entry
  220. *
  221. * @return string|null
  222. */
  223. public function setGenerator($name, $version = null, $uri = null)
  224. {
  225. if (empty($name) || !is_string($name)) {
  226. require_once 'Zend/Feed/Exception.php';
  227. throw new Zend_Feed_Exception('Invalid parameter: "name" must be a non-empty string');
  228. }
  229. $generator = array('name' => $name);
  230. if (isset($version)) {
  231. if (empty($version) || !is_string($version)) {
  232. require_once 'Zend/Feed/Exception.php';
  233. throw new Zend_Feed_Exception('Invalid parameter: "version" must be a non-empty string');
  234. }
  235. $generator['version'] = $version;
  236. }
  237. if (isset($uri)) {
  238. if (empty($uri) || !is_string($uri) || !Zend_Uri::check($uri)) {
  239. require_once 'Zend/Feed/Exception.php';
  240. throw new Zend_Feed_Exception('Invalid parameter: "uri" must be a non-empty string and a valid URI/IRI');
  241. }
  242. $generator['uri'] = $uri;
  243. }
  244. $this->_data['generator'] = $generator;
  245. }
  246. /**
  247. * Set the feed ID - URI or URN (via PCRE pattern) supported
  248. *
  249. * @return string|null
  250. */
  251. public function setId($id)
  252. {
  253. if ((empty($id) || !is_string($id) || !Zend_Uri::check($id)) &&
  254. !preg_match("#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#", $id)) {
  255. require_once 'Zend/Feed/Exception.php';
  256. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string and valid URI/IRI');
  257. }
  258. $this->_data['id'] = $id;
  259. }
  260. /**
  261. * Set the feed language
  262. *
  263. * @return string|null
  264. */
  265. public function setLanguage($language)
  266. {
  267. if (empty($language) || !is_string($language)) {
  268. require_once 'Zend/Feed/Exception.php';
  269. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
  270. }
  271. $this->_data['language'] = $language;
  272. }
  273. /**
  274. * Set a link to the HTML source
  275. *
  276. * @return string|null
  277. */
  278. public function setLink($link)
  279. {
  280. if (empty($link) || !is_string($link) || !Zend_Uri::check($link)) {
  281. require_once 'Zend/Feed/Exception.php';
  282. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string and valid URI/IRI');
  283. }
  284. $this->_data['link'] = $link;
  285. }
  286. /**
  287. * Set a link to an XML feed for any feed type/version
  288. *
  289. * @return string|null
  290. */
  291. public function setFeedLink($link, $type)
  292. {
  293. if (empty($link) || !is_string($link) || !Zend_Uri::check($link)) {
  294. require_once 'Zend/Feed/Exception.php';
  295. throw new Zend_Feed_Exception('Invalid parameter: "link"" must be a non-empty string and valid URI/IRI');
  296. }
  297. if (!in_array(strtolower($type), array('rss', 'rdf', 'atom'))) {
  298. require_once 'Zend/Feed/Exception.php';
  299. throw new Zend_Feed_Exception('Invalid parameter: "type"; You must declare the type of feed the link points to, i.e. RSS, RDF or Atom');
  300. }
  301. $this->_data['feedLinks'][strtolower($type)] = $link;
  302. }
  303. /**
  304. * Set the feed title
  305. *
  306. * @return string|null
  307. */
  308. public function setTitle($title)
  309. {
  310. if (empty($title) || !is_string($title)) {
  311. require_once 'Zend/Feed/Exception.php';
  312. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
  313. }
  314. $this->_data['title'] = $title;
  315. }
  316. /**
  317. * Set the feed character encoding
  318. *
  319. * @param string $encoding
  320. */
  321. public function setEncoding($encoding)
  322. {
  323. if (empty($encoding) || !is_string($encoding)) {
  324. require_once 'Zend/Feed/Exception.php';
  325. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
  326. }
  327. $this->_data['encoding'] = $encoding;
  328. }
  329. /**
  330. * Set the feed's base URL
  331. *
  332. * @param string $url
  333. */
  334. public function setBaseUrl($url)
  335. {
  336. if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
  337. require_once 'Zend/Feed/Exception.php';
  338. throw new Zend_Feed_Exception('Invalid parameter: "url" array value'
  339. . ' must be a non-empty string and valid URI/IRI');
  340. }
  341. $this->_data['baseUrl'] = $url;
  342. }
  343. /**
  344. * Add a Pubsubhubbub hub endpoint URL
  345. *
  346. * @param string $url
  347. */
  348. public function addHub($url)
  349. {
  350. if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
  351. require_once 'Zend/Feed/Exception.php';
  352. throw new Zend_Feed_Exception('Invalid parameter: "url" array value'
  353. . ' must be a non-empty string and valid URI/IRI');
  354. }
  355. if (!isset($this->_data['hubs'])) {
  356. $this->_data['hubs'] = array();
  357. }
  358. $this->_data['hubs'][] = $url;
  359. }
  360. /**
  361. * Add Pubsubhubbub hub endpoint URLs
  362. *
  363. * @param array $urls
  364. */
  365. public function addHubs(array $urls)
  366. {
  367. foreach ($urls as $url) {
  368. $this->addHub($url);
  369. }
  370. }
  371. /**
  372. * Add a feed category
  373. *
  374. * @param string $category
  375. */
  376. public function addCategory(array $category)
  377. {
  378. if (!isset($category['term'])) {
  379. require_once 'Zend/Feed/Exception.php';
  380. throw new Zend_Feed_Exception('Each category must be an array and '
  381. . 'contain at least a "term" element containing the machine '
  382. . ' readable category name');
  383. }
  384. if (isset($category['scheme'])) {
  385. if (empty($category['scheme']) || !is_string($category['scheme'])
  386. || !Zend_Uri::check($category['scheme'])) {
  387. require_once 'Zend/Feed/Exception.php';
  388. throw new Zend_Feed_Exception('The Atom scheme or RSS domain of'
  389. . ' a category must be a valid URI');
  390. }
  391. }
  392. if (!isset($this->_data['categories'])) {
  393. $this->_data['categories'] = array();
  394. }
  395. $this->_data['categories'][] = $category;
  396. }
  397. /**
  398. * Set an array of feed categories
  399. *
  400. * @param array $categories
  401. */
  402. public function addCategories(array $categories)
  403. {
  404. foreach ($categories as $category) {
  405. $this->addCategory($category);
  406. }
  407. }
  408. /**
  409. * Get a single author
  410. *
  411. * @param int $index
  412. * @return string|null
  413. */
  414. public function getAuthor($index = 0)
  415. {
  416. if (isset($this->_data['authors'][$index])) {
  417. return $this->_data['authors'][$index];
  418. } else {
  419. return null;
  420. }
  421. }
  422. /**
  423. * Get an array with feed authors
  424. *
  425. * @return array
  426. */
  427. public function getAuthors()
  428. {
  429. if (!array_key_exists('authors', $this->_data)) {
  430. return null;
  431. }
  432. return $this->_data['authors'];
  433. }
  434. /**
  435. * Get the copyright entry
  436. *
  437. * @return string|null
  438. */
  439. public function getCopyright()
  440. {
  441. if (!array_key_exists('copyright', $this->_data)) {
  442. return null;
  443. }
  444. return $this->_data['copyright'];
  445. }
  446. /**
  447. * Get the feed creation date
  448. *
  449. * @return string|null
  450. */
  451. public function getDateCreated()
  452. {
  453. if (!array_key_exists('dateCreated', $this->_data)) {
  454. return null;
  455. }
  456. return $this->_data['dateCreated'];
  457. }
  458. /**
  459. * Get the feed modification date
  460. *
  461. * @return string|null
  462. */
  463. public function getDateModified()
  464. {
  465. if (!array_key_exists('dateModified', $this->_data)) {
  466. return null;
  467. }
  468. return $this->_data['dateModified'];
  469. }
  470. /**
  471. * Get the feed description
  472. *
  473. * @return string|null
  474. */
  475. public function getDescription()
  476. {
  477. if (!array_key_exists('description', $this->_data)) {
  478. return null;
  479. }
  480. return $this->_data['description'];
  481. }
  482. /**
  483. * Get the feed generator entry
  484. *
  485. * @return string|null
  486. */
  487. public function getGenerator()
  488. {
  489. if (!array_key_exists('generator', $this->_data)) {
  490. return null;
  491. }
  492. return $this->_data['generator'];
  493. }
  494. /**
  495. * Get the feed ID
  496. *
  497. * @return string|null
  498. */
  499. public function getId()
  500. {
  501. if (!array_key_exists('id', $this->_data)) {
  502. return null;
  503. }
  504. return $this->_data['id'];
  505. }
  506. /**
  507. * Get the feed language
  508. *
  509. * @return string|null
  510. */
  511. public function getLanguage()
  512. {
  513. if (!array_key_exists('language', $this->_data)) {
  514. return null;
  515. }
  516. return $this->_data['language'];
  517. }
  518. /**
  519. * Get a link to the HTML source
  520. *
  521. * @return string|null
  522. */
  523. public function getLink()
  524. {
  525. if (!array_key_exists('link', $this->_data)) {
  526. return null;
  527. }
  528. return $this->_data['link'];
  529. }
  530. /**
  531. * Get a link to the XML feed
  532. *
  533. * @return string|null
  534. */
  535. public function getFeedLinks()
  536. {
  537. if (!array_key_exists('feedLinks', $this->_data)) {
  538. return null;
  539. }
  540. return $this->_data['feedLinks'];
  541. }
  542. /**
  543. * Get the feed title
  544. *
  545. * @return string|null
  546. */
  547. public function getTitle()
  548. {
  549. if (!array_key_exists('title', $this->_data)) {
  550. return null;
  551. }
  552. return $this->_data['title'];
  553. }
  554. /**
  555. * Get the feed character encoding
  556. *
  557. * @return string|null
  558. */
  559. public function getEncoding()
  560. {
  561. if (!array_key_exists('encoding', $this->_data)) {
  562. return 'UTF-8';
  563. }
  564. return $this->_data['encoding'];
  565. }
  566. /**
  567. * Get the feed's base url
  568. *
  569. * @return string|null
  570. */
  571. public function getBaseUrl()
  572. {
  573. if (!array_key_exists('baseUrl', $this->_data)) {
  574. return null;
  575. }
  576. return $this->_data['baseUrl'];
  577. }
  578. /**
  579. * Get the URLs used as Pubsubhubbub hubs endpoints
  580. *
  581. * @return string|null
  582. */
  583. public function getHubs()
  584. {
  585. if (!array_key_exists('hubs', $this->_data)) {
  586. return null;
  587. }
  588. return $this->_data['hubs'];
  589. }
  590. /**
  591. * Get the feed categories
  592. *
  593. * @return string|null
  594. */
  595. public function getCategories()
  596. {
  597. if (!array_key_exists('categories', $this->_data)) {
  598. return null;
  599. }
  600. return $this->_data['categories'];
  601. }
  602. /**
  603. * Resets the instance and deletes all data
  604. *
  605. * @return void
  606. */
  607. public function reset()
  608. {
  609. $this->_data = array();
  610. }
  611. /**
  612. * Creates a new Zend_Feed_Writer_Entry data container for use. This is NOT
  613. * added to the current feed automatically, but is necessary to create a
  614. * container with some initial values preset based on the current feed data.
  615. *
  616. * @return Zend_Feed_Writer_Entry
  617. */
  618. public function createEntry()
  619. {
  620. $entry = new Zend_Feed_Writer_Entry;
  621. if ($this->getEncoding()) {
  622. $entry->setEncoding($this->getEncoding());
  623. }
  624. $entry->setType($this->getType());
  625. return $entry;
  626. }
  627. /**
  628. * Appends a Zend_Feed_Writer_Entry object representing a new entry/item
  629. * the feed data container's internal group of entries.
  630. *
  631. * @param Zend_Feed_Writer_Entry $entry
  632. */
  633. public function addEntry(Zend_Feed_Writer_Entry $entry)
  634. {
  635. $this->_entries[] = $entry;
  636. }
  637. /**
  638. * Removes a specific indexed entry from the internal queue. Entries must be
  639. * added to a feed container in order to be indexed.
  640. *
  641. * @param int $index
  642. */
  643. public function removeEntry($index)
  644. {
  645. if (isset($this->_entries[$index])) {
  646. unset($this->_entries[$index]);
  647. }
  648. require_once 'Zend/Feed/Exception.php';
  649. throw new Zend_Feed_Exception('Undefined index: ' . $index . '. Entry does not exist.');
  650. }
  651. /**
  652. * Retrieve a specific indexed entry from the internal queue. Entries must be
  653. * added to a feed container in order to be indexed.
  654. *
  655. * @param int $index
  656. */
  657. public function getEntry($index = 0)
  658. {
  659. if (isset($this->_entries[$index])) {
  660. return $this->_entries[$index];
  661. }
  662. require_once 'Zend/Feed/Exception.php';
  663. throw new Zend_Feed_Exception('Undefined index: ' . $index . '. Entry does not exist.');
  664. }
  665. /**
  666. * Orders all indexed entries by date, thus offering date ordered readable
  667. * content where a parser (or Homo Sapien) ignores the generic rule that
  668. * XML element order is irrelevant and has no intrinsic meaning.
  669. *
  670. * Using this method will alter the original indexation.
  671. *
  672. * @return void
  673. */
  674. public function orderByDate()
  675. {
  676. /**
  677. * Could do with some improvement for performance perhaps
  678. */
  679. $timestamp = time();
  680. $entries = array();
  681. foreach ($this->_entries as $entry) {
  682. if ($entry->getDateModified()) {
  683. $timestamp = (int) $entry->getDateModified()->get(Zend_Date::TIMESTAMP);
  684. } elseif ($entry->getDateCreated()) {
  685. $timestamp = (int) $entry->getDateCreated()->get(Zend_Date::TIMESTAMP);
  686. }
  687. $entries[$timestamp] = $entry;
  688. }
  689. krsort($entries, SORT_NUMERIC);
  690. $this->_entries = array_values($entries);
  691. }
  692. /**
  693. * Get the number of feed entries.
  694. * Required by the Iterator interface.
  695. *
  696. * @return int
  697. */
  698. public function count()
  699. {
  700. return count($this->_entries);
  701. }
  702. /**
  703. * Return the current entry
  704. *
  705. * @return Zend_Feed_Reader_Entry_Interface
  706. */
  707. public function current()
  708. {
  709. return $this->_entries[$this->key()];
  710. }
  711. /**
  712. * Return the current feed key
  713. *
  714. * @return unknown
  715. */
  716. public function key()
  717. {
  718. return $this->_entriesKey;
  719. }
  720. /**
  721. * Move the feed pointer forward
  722. *
  723. * @return void
  724. */
  725. public function next()
  726. {
  727. ++$this->_entriesKey;
  728. }
  729. /**
  730. * Reset the pointer in the feed object
  731. *
  732. * @return void
  733. */
  734. public function rewind()
  735. {
  736. $this->_entriesKey = 0;
  737. }
  738. /**
  739. * Check to see if the iterator is still valid
  740. *
  741. * @return boolean
  742. */
  743. public function valid()
  744. {
  745. return 0 <= $this->_entriesKey && $this->_entriesKey < $this->count();
  746. }
  747. /**
  748. * Set the current feed type being exported to "rss" or "atom". This allows
  749. * other objects to gracefully choose whether to execute or not, depending
  750. * on their appropriateness for the current type, e.g. renderers.
  751. *
  752. * @param string $type
  753. */
  754. public function setType($type)
  755. {
  756. $this->_type = $type;
  757. }
  758. /**
  759. * Retrieve the current or last feed type exported.
  760. *
  761. * @return string Value will be "rss" or "atom"
  762. */
  763. public function getType()
  764. {
  765. return $this->_type;
  766. }
  767. /**
  768. * Attempt to build and return the feed resulting from the data set
  769. *
  770. * @param $type The feed type "rss" or "atom" to export as
  771. * @return string
  772. */
  773. public function export($type, $ignoreExceptions = false)
  774. {
  775. $this->setType(strtolower($type));
  776. $type = ucfirst($this->getType());
  777. if ($type !== 'Rss' && $type !== 'Atom') {
  778. require_once 'Zend/Feed/Exception.php';
  779. throw new Zend_Feed_Exception('Invalid feed type specified: ' . $type . '.'
  780. . ' Should be one of "rss" or "atom".');
  781. }
  782. $renderClass = 'Zend_Feed_Writer_Renderer_Feed_' . $type;
  783. $renderer = new $renderClass($this);
  784. if ($ignoreExceptions) {
  785. $renderer->ignoreExceptions();
  786. }
  787. return $renderer->render()->saveXml();
  788. }
  789. /**
  790. * Unset a specific data point
  791. *
  792. * @param string $name
  793. */
  794. public function remove($name)
  795. {
  796. if (isset($this->_data[$name])) {
  797. unset($this->_data[$name]);
  798. }
  799. }
  800. /**
  801. * Method overloading: call given method on first extension implementing it
  802. *
  803. * @param string $method
  804. * @param array $args
  805. * @return mixed
  806. * @throws Zend_Feed_Exception if no extensions implements the method
  807. */
  808. public function __call($method, $args)
  809. {
  810. foreach ($this->_extensions as $extension) {
  811. try {
  812. return call_user_func_array(array($extension, $method), $args);
  813. } catch (Zend_Feed_Writer_Exception_InvalidMethodException $e) {
  814. }
  815. }
  816. require_once 'Zend/Feed/Exception.php';
  817. throw new Zend_Feed_Exception('Method: ' . $method
  818. . ' does not exist and could not be located on a registered Extension');
  819. }
  820. /**
  821. * Load extensions from Zend_Feed_Writer
  822. *
  823. * @return void
  824. */
  825. protected function _loadExtensions()
  826. {
  827. $all = Zend_Feed_Writer::getExtensions();
  828. $exts = $all['feed'];
  829. foreach ($exts as $ext) {
  830. $className = Zend_Feed_Writer::getPluginLoader()->getClassName($ext);
  831. $this->_extensions[$ext] = new $className();
  832. $this->_extensions[$ext]->setEncoding($this->getEncoding());
  833. }
  834. }
  835. }