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-2010 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-2010 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'])
  386. || !is_string($category['scheme'])
  387. || !Zend_Uri::check($category['scheme'])
  388. ) {
  389. require_once 'Zend/Feed/Exception.php';
  390. throw new Zend_Feed_Exception('The Atom scheme or RSS domain of'
  391. . ' a category must be a valid URI');
  392. }
  393. }
  394. if (!isset($this->_data['categories'])) {
  395. $this->_data['categories'] = array();
  396. }
  397. $this->_data['categories'][] = $category;
  398. }
  399. /**
  400. * Set an array of feed categories
  401. *
  402. * @param array $categories
  403. */
  404. public function addCategories(array $categories)
  405. {
  406. foreach ($categories as $category) {
  407. $this->addCategory($category);
  408. }
  409. }
  410. /**
  411. * Get a single author
  412. *
  413. * @param int $index
  414. * @return string|null
  415. */
  416. public function getAuthor($index = 0)
  417. {
  418. if (isset($this->_data['authors'][$index])) {
  419. return $this->_data['authors'][$index];
  420. } else {
  421. return null;
  422. }
  423. }
  424. /**
  425. * Get an array with feed authors
  426. *
  427. * @return array
  428. */
  429. public function getAuthors()
  430. {
  431. if (!array_key_exists('authors', $this->_data)) {
  432. return null;
  433. }
  434. return $this->_data['authors'];
  435. }
  436. /**
  437. * Get the copyright entry
  438. *
  439. * @return string|null
  440. */
  441. public function getCopyright()
  442. {
  443. if (!array_key_exists('copyright', $this->_data)) {
  444. return null;
  445. }
  446. return $this->_data['copyright'];
  447. }
  448. /**
  449. * Get the feed creation date
  450. *
  451. * @return string|null
  452. */
  453. public function getDateCreated()
  454. {
  455. if (!array_key_exists('dateCreated', $this->_data)) {
  456. return null;
  457. }
  458. return $this->_data['dateCreated'];
  459. }
  460. /**
  461. * Get the feed modification date
  462. *
  463. * @return string|null
  464. */
  465. public function getDateModified()
  466. {
  467. if (!array_key_exists('dateModified', $this->_data)) {
  468. return null;
  469. }
  470. return $this->_data['dateModified'];
  471. }
  472. /**
  473. * Get the feed description
  474. *
  475. * @return string|null
  476. */
  477. public function getDescription()
  478. {
  479. if (!array_key_exists('description', $this->_data)) {
  480. return null;
  481. }
  482. return $this->_data['description'];
  483. }
  484. /**
  485. * Get the feed generator entry
  486. *
  487. * @return string|null
  488. */
  489. public function getGenerator()
  490. {
  491. if (!array_key_exists('generator', $this->_data)) {
  492. return null;
  493. }
  494. return $this->_data['generator'];
  495. }
  496. /**
  497. * Get the feed ID
  498. *
  499. * @return string|null
  500. */
  501. public function getId()
  502. {
  503. if (!array_key_exists('id', $this->_data)) {
  504. return null;
  505. }
  506. return $this->_data['id'];
  507. }
  508. /**
  509. * Get the feed language
  510. *
  511. * @return string|null
  512. */
  513. public function getLanguage()
  514. {
  515. if (!array_key_exists('language', $this->_data)) {
  516. return null;
  517. }
  518. return $this->_data['language'];
  519. }
  520. /**
  521. * Get a link to the HTML source
  522. *
  523. * @return string|null
  524. */
  525. public function getLink()
  526. {
  527. if (!array_key_exists('link', $this->_data)) {
  528. return null;
  529. }
  530. return $this->_data['link'];
  531. }
  532. /**
  533. * Get a link to the XML feed
  534. *
  535. * @return string|null
  536. */
  537. public function getFeedLinks()
  538. {
  539. if (!array_key_exists('feedLinks', $this->_data)) {
  540. return null;
  541. }
  542. return $this->_data['feedLinks'];
  543. }
  544. /**
  545. * Get the feed title
  546. *
  547. * @return string|null
  548. */
  549. public function getTitle()
  550. {
  551. if (!array_key_exists('title', $this->_data)) {
  552. return null;
  553. }
  554. return $this->_data['title'];
  555. }
  556. /**
  557. * Get the feed character encoding
  558. *
  559. * @return string|null
  560. */
  561. public function getEncoding()
  562. {
  563. if (!array_key_exists('encoding', $this->_data)) {
  564. return 'UTF-8';
  565. }
  566. return $this->_data['encoding'];
  567. }
  568. /**
  569. * Get the feed's base url
  570. *
  571. * @return string|null
  572. */
  573. public function getBaseUrl()
  574. {
  575. if (!array_key_exists('baseUrl', $this->_data)) {
  576. return null;
  577. }
  578. return $this->_data['baseUrl'];
  579. }
  580. /**
  581. * Get the URLs used as Pubsubhubbub hubs endpoints
  582. *
  583. * @return string|null
  584. */
  585. public function getHubs()
  586. {
  587. if (!array_key_exists('hubs', $this->_data)) {
  588. return null;
  589. }
  590. return $this->_data['hubs'];
  591. }
  592. /**
  593. * Get the feed categories
  594. *
  595. * @return string|null
  596. */
  597. public function getCategories()
  598. {
  599. if (!array_key_exists('categories', $this->_data)) {
  600. return null;
  601. }
  602. return $this->_data['categories'];
  603. }
  604. /**
  605. * Resets the instance and deletes all data
  606. *
  607. * @return void
  608. */
  609. public function reset()
  610. {
  611. $this->_data = array();
  612. }
  613. /**
  614. * Creates a new Zend_Feed_Writer_Entry data container for use. This is NOT
  615. * added to the current feed automatically, but is necessary to create a
  616. * container with some initial values preset based on the current feed data.
  617. *
  618. * @return Zend_Feed_Writer_Entry
  619. */
  620. public function createEntry()
  621. {
  622. $entry = new Zend_Feed_Writer_Entry;
  623. if ($this->getEncoding()) {
  624. $entry->setEncoding($this->getEncoding());
  625. }
  626. $entry->setType($this->getType());
  627. return $entry;
  628. }
  629. /**
  630. * Appends a Zend_Feed_Writer_Entry object representing a new entry/item
  631. * the feed data container's internal group of entries.
  632. *
  633. * @param Zend_Feed_Writer_Entry $entry
  634. */
  635. public function addEntry(Zend_Feed_Writer_Entry $entry)
  636. {
  637. $this->_entries[] = $entry;
  638. }
  639. /**
  640. * Removes a specific indexed entry from the internal queue. Entries must be
  641. * added to a feed container in order to be indexed.
  642. *
  643. * @param int $index
  644. */
  645. public function removeEntry($index)
  646. {
  647. if (isset($this->_entries[$index])) {
  648. unset($this->_entries[$index]);
  649. }
  650. require_once 'Zend/Feed/Exception.php';
  651. throw new Zend_Feed_Exception('Undefined index: ' . $index . '. Entry does not exist.');
  652. }
  653. /**
  654. * Retrieve a specific indexed entry from the internal queue. Entries must be
  655. * added to a feed container in order to be indexed.
  656. *
  657. * @param int $index
  658. */
  659. public function getEntry($index = 0)
  660. {
  661. if (isset($this->_entries[$index])) {
  662. return $this->_entries[$index];
  663. }
  664. require_once 'Zend/Feed/Exception.php';
  665. throw new Zend_Feed_Exception('Undefined index: ' . $index . '. Entry does not exist.');
  666. }
  667. /**
  668. * Orders all indexed entries by date, thus offering date ordered readable
  669. * content where a parser (or Homo Sapien) ignores the generic rule that
  670. * XML element order is irrelevant and has no intrinsic meaning.
  671. *
  672. * Using this method will alter the original indexation.
  673. *
  674. * @return void
  675. */
  676. public function orderByDate()
  677. {
  678. /**
  679. * Could do with some improvement for performance perhaps
  680. */
  681. $timestamp = time();
  682. $entries = array();
  683. foreach ($this->_entries as $entry) {
  684. if ($entry->getDateModified()) {
  685. $timestamp = (int) $entry->getDateModified()->get(Zend_Date::TIMESTAMP);
  686. } elseif ($entry->getDateCreated()) {
  687. $timestamp = (int) $entry->getDateCreated()->get(Zend_Date::TIMESTAMP);
  688. }
  689. $entries[$timestamp] = $entry;
  690. }
  691. krsort($entries, SORT_NUMERIC);
  692. $this->_entries = array_values($entries);
  693. }
  694. /**
  695. * Get the number of feed entries.
  696. * Required by the Iterator interface.
  697. *
  698. * @return int
  699. */
  700. public function count()
  701. {
  702. return count($this->_entries);
  703. }
  704. /**
  705. * Return the current entry
  706. *
  707. * @return Zend_Feed_Reader_Entry_Interface
  708. */
  709. public function current()
  710. {
  711. return $this->_entries[$this->key()];
  712. }
  713. /**
  714. * Return the current feed key
  715. *
  716. * @return unknown
  717. */
  718. public function key()
  719. {
  720. return $this->_entriesKey;
  721. }
  722. /**
  723. * Move the feed pointer forward
  724. *
  725. * @return void
  726. */
  727. public function next()
  728. {
  729. ++$this->_entriesKey;
  730. }
  731. /**
  732. * Reset the pointer in the feed object
  733. *
  734. * @return void
  735. */
  736. public function rewind()
  737. {
  738. $this->_entriesKey = 0;
  739. }
  740. /**
  741. * Check to see if the iterator is still valid
  742. *
  743. * @return boolean
  744. */
  745. public function valid()
  746. {
  747. return 0 <= $this->_entriesKey && $this->_entriesKey < $this->count();
  748. }
  749. /**
  750. * Set the current feed type being exported to "rss" or "atom". This allows
  751. * other objects to gracefully choose whether to execute or not, depending
  752. * on their appropriateness for the current type, e.g. renderers.
  753. *
  754. * @param string $type
  755. */
  756. public function setType($type)
  757. {
  758. $this->_type = $type;
  759. }
  760. /**
  761. * Retrieve the current or last feed type exported.
  762. *
  763. * @return string Value will be "rss" or "atom"
  764. */
  765. public function getType()
  766. {
  767. return $this->_type;
  768. }
  769. /**
  770. * Attempt to build and return the feed resulting from the data set
  771. *
  772. * @param $type The feed type "rss" or "atom" to export as
  773. * @return string
  774. */
  775. public function export($type, $ignoreExceptions = false)
  776. {
  777. $this->setType(strtolower($type));
  778. $type = ucfirst($this->getType());
  779. if ($type !== 'Rss' && $type !== 'Atom') {
  780. require_once 'Zend/Feed/Exception.php';
  781. throw new Zend_Feed_Exception('Invalid feed type specified: ' . $type . '.'
  782. . ' Should be one of "rss" or "atom".');
  783. }
  784. $renderClass = 'Zend_Feed_Writer_Renderer_Feed_' . $type;
  785. $renderer = new $renderClass($this);
  786. if ($ignoreExceptions) {
  787. $renderer->ignoreExceptions();
  788. }
  789. return $renderer->render()->saveXml();
  790. }
  791. /**
  792. * Unset a specific data point
  793. *
  794. * @param string $name
  795. */
  796. public function remove($name)
  797. {
  798. if (isset($this->_data[$name])) {
  799. unset($this->_data[$name]);
  800. }
  801. }
  802. /**
  803. * Method overloading: call given method on first extension implementing it
  804. *
  805. * @param string $method
  806. * @param array $args
  807. * @return mixed
  808. * @throws Zend_Feed_Exception if no extensions implements the method
  809. */
  810. public function __call($method, $args)
  811. {
  812. foreach ($this->_extensions as $extension) {
  813. try {
  814. return call_user_func_array(array($extension, $method), $args);
  815. } catch (Zend_Feed_Writer_Exception_InvalidMethodException $e) {
  816. }
  817. }
  818. require_once 'Zend/Feed/Exception.php';
  819. throw new Zend_Feed_Exception('Method: ' . $method
  820. . ' does not exist and could not be located on a registered Extension');
  821. }
  822. /**
  823. * Load extensions from Zend_Feed_Writer
  824. *
  825. * @return void
  826. */
  827. protected function _loadExtensions()
  828. {
  829. $all = Zend_Feed_Writer::getExtensions();
  830. $exts = $all['feed'];
  831. foreach ($exts as $ext) {
  832. $className = Zend_Feed_Writer::getPluginLoader()->getClassName($ext);
  833. $this->_extensions[$ext] = new $className();
  834. $this->_extensions[$ext]->setEncoding($this->getEncoding());
  835. }
  836. }
  837. }