Entry.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. * @category Zend
  31. * @package Zend_Feed_Writer
  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_Writer_Entry
  36. {
  37. /**
  38. * Internal array containing all data associated with this entry or item.
  39. *
  40. * @var array
  41. */
  42. protected $_data = array();
  43. /**
  44. * Registered extensions
  45. *
  46. * @var array
  47. */
  48. protected $_extensions = array();
  49. /**
  50. * Holds the value "atom" or "rss" depending on the feed type set when
  51. * when last exported.
  52. *
  53. * @var string
  54. */
  55. protected $_type = null;
  56. /**
  57. * Constructor: Primarily triggers the registration of core extensions and
  58. * loads those appropriate to this data container.
  59. *
  60. * @return void
  61. */
  62. public function __construct()
  63. {
  64. Zend_Feed_Writer::registerCoreExtensions();
  65. $this->_loadExtensions();
  66. }
  67. /**
  68. * Set a single author
  69. *
  70. * @param int $index
  71. * @return string|null
  72. */
  73. public function addAuthor($name, $email = null, $uri = null)
  74. {
  75. $author = array();
  76. if (is_array($name)) {
  77. if (!array_key_exists('name', $name) || empty($name['name']) || !is_string($name['name'])) {
  78. require_once 'Zend/Feed/Exception.php';
  79. throw new Zend_Feed_Exception('Invalid parameter: author array must include a "name" key with a non-empty string value');
  80. }
  81. $author['name'] = $name['name'];
  82. if (isset($name['email'])) {
  83. if (empty($name['email']) || !is_string($name['email'])) {
  84. require_once 'Zend/Feed/Exception.php';
  85. throw new Zend_Feed_Exception('Invalid parameter: "email" array value must be a non-empty string');
  86. }
  87. $author['email'] = $name['email'];
  88. }
  89. if (isset($name['uri'])) {
  90. if (empty($name['uri']) || !is_string($name['uri']) || !Zend_Uri::check($name['uri'])) {
  91. require_once 'Zend/Feed/Exception.php';
  92. throw new Zend_Feed_Exception('Invalid parameter: "uri" array value must be a non-empty string and valid URI/IRI');
  93. }
  94. $author['uri'] = $name['uri'];
  95. }
  96. } else {
  97. if (empty($name['name']) || !is_string($name['name'])) {
  98. require_once 'Zend/Feed/Exception.php';
  99. throw new Zend_Feed_Exception('Invalid parameter: "name" must be a non-empty string value');
  100. }
  101. $author['name'] = $name;
  102. if (isset($email)) {
  103. if (empty($email) || !is_string($email)) {
  104. require_once 'Zend/Feed/Exception.php';
  105. throw new Zend_Feed_Exception('Invalid parameter: "email" value must be a non-empty string');
  106. }
  107. $author['email'] = $email;
  108. }
  109. if (isset($uri)) {
  110. if (empty($uri) || !is_string($uri) || !Zend_Uri::check($uri)) {
  111. require_once 'Zend/Feed/Exception.php';
  112. throw new Zend_Feed_Exception('Invalid parameter: "uri" value must be a non-empty string and valid URI/IRI');
  113. }
  114. $author['uri'] = $uri;
  115. }
  116. }
  117. $this->_data['authors'][] = $author;
  118. }
  119. /**
  120. * Set an array with feed authors
  121. *
  122. * @return array
  123. */
  124. public function addAuthors(array $authors)
  125. {
  126. foreach($authors as $author) {
  127. $this->addAuthor($author);
  128. }
  129. }
  130. /**
  131. * Set the feed character encoding
  132. *
  133. * @return string|null
  134. */
  135. public function setEncoding($encoding)
  136. {
  137. if (empty($encoding) || !is_string($encoding)) {
  138. require_once 'Zend/Feed/Exception.php';
  139. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
  140. }
  141. $this->_data['encoding'] = $encoding;
  142. }
  143. /**
  144. * Get the feed character encoding
  145. *
  146. * @return string|null
  147. */
  148. public function getEncoding()
  149. {
  150. if (!array_key_exists('encoding', $this->_data)) {
  151. return 'UTF-8';
  152. }
  153. return $this->_data['encoding'];
  154. }
  155. /**
  156. * Set the copyright entry
  157. *
  158. * @return string|null
  159. */
  160. public function setCopyright($copyright)
  161. {
  162. if (empty($copyright) || !is_string($copyright)) {
  163. require_once 'Zend/Feed/Exception.php';
  164. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
  165. }
  166. $this->_data['copyright'] = $copyright;
  167. }
  168. /**
  169. * Set the entry's content
  170. *
  171. * @return string|null
  172. */
  173. public function setContent($content)
  174. {
  175. if (empty($content) || !is_string($content)) {
  176. require_once 'Zend/Feed/Exception.php';
  177. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
  178. }
  179. $this->_data['content'] = $content;
  180. }
  181. /**
  182. * Set the feed creation date
  183. *
  184. * @return string|null
  185. */
  186. public function setDateCreated($date = null)
  187. {
  188. $zdate = null;
  189. if (is_null($date)) {
  190. $zdate = new Zend_Date;
  191. } elseif (ctype_digit($date) && strlen($date) == 10) {
  192. $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP);
  193. } elseif ($date instanceof Zend_Date) {
  194. $zdate = $date;
  195. } else {
  196. require_once 'Zend/Feed/Exception.php';
  197. throw new Zend_Feed_Exception('Invalid Zend_Date object or UNIX Timestamp passed as parameter');
  198. }
  199. $this->_data['dateCreated'] = $zdate;
  200. }
  201. /**
  202. * Set the feed modification date
  203. *
  204. * @return string|null
  205. */
  206. public function setDateModified($date = null)
  207. {
  208. $zdate = null;
  209. if (is_null($date)) {
  210. $zdate = new Zend_Date;
  211. } elseif (ctype_digit($date) && strlen($date) == 10) {
  212. $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP);
  213. } elseif ($date instanceof Zend_Date) {
  214. $zdate = $date;
  215. } else {
  216. require_once 'Zend/Feed/Exception.php';
  217. throw new Zend_Feed_Exception('Invalid Zend_Date object or UNIX Timestamp passed as parameter');
  218. }
  219. $this->_data['dateModified'] = $zdate;
  220. }
  221. /**
  222. * Set the feed description
  223. *
  224. * @return string|null
  225. */
  226. public function setDescription($description)
  227. {
  228. if (empty($description) || !is_string($description)) {
  229. require_once 'Zend/Feed/Exception.php';
  230. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
  231. }
  232. $this->_data['description'] = $description;
  233. }
  234. /**
  235. * Set the feed ID
  236. *
  237. * @return string|null
  238. */
  239. public function setId($id)
  240. {
  241. if (empty($id) || !is_string($id)) {
  242. require_once 'Zend/Feed/Exception.php';
  243. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
  244. }
  245. $this->_data['id'] = $id;
  246. }
  247. /**
  248. * Set a link to the HTML source of this entry
  249. *
  250. * @return string|null
  251. */
  252. public function setLink($link)
  253. {
  254. if (empty($link) || !is_string($link) || !Zend_Uri::check($link)) {
  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['link'] = $link;
  259. }
  260. /**
  261. * Set the number of comments associated with this entry
  262. *
  263. * @return string|null
  264. */
  265. public function setCommentCount($count)
  266. {
  267. if (empty($count) || !is_numeric($count) || (int) $count < 0) {
  268. require_once 'Zend/Feed/Exception.php';
  269. throw new Zend_Feed_Exception('Invalid parameter: "count" must be a non-empty integer number');
  270. }
  271. $this->_data['commentCount'] = (int) $count;
  272. }
  273. /**
  274. * Set a link to a HTML page containing comments associated with this entry
  275. *
  276. * @return string|null
  277. */
  278. public function setCommentLink($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: "link" must be a non-empty string and valid URI/IRI');
  283. }
  284. $this->_data['commentLink'] = $link;
  285. }
  286. /**
  287. * Set a link to an XML feed for any comments associated with this entry
  288. *
  289. * @return string|null
  290. */
  291. public function setCommentFeedLink(array $link)
  292. {
  293. if (!isset($link['uri']) || !is_string($link['uri']) || !Zend_Uri::check($link['uri'])) {
  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 (!isset($link['type']) || !in_array($link['type'], array('atom', 'rss', 'rdf'))) {
  298. require_once 'Zend/Feed/Exception.php';
  299. throw new Zend_Feed_Exception('Invalid parameter: "type" must be one'
  300. . ' of "atom", "rss" or "rdf"');
  301. }
  302. if (!isset($this->_data['commentFeedLinks'])) {
  303. $this->_data['commentFeedLinks'] = array();
  304. }
  305. $this->_data['commentFeedLinks'][] = $link;
  306. }
  307. /**
  308. * Set a links to an XML feed for any comments associated with this entry.
  309. * Each link is an array with keys "uri" and "type", where type is one of:
  310. * "atom", "rss" or "rdf".
  311. *
  312. * @return string|null
  313. */
  314. public function setCommentFeedLinks(array $links)
  315. {
  316. foreach ($links as $link) {
  317. $this->setCommentFeedLink($link);
  318. }
  319. }
  320. /**
  321. * Set the feed title
  322. *
  323. * @return string|null
  324. */
  325. public function setTitle($title)
  326. {
  327. if (empty($title) || !is_string($title)) {
  328. require_once 'Zend/Feed/Exception.php';
  329. throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
  330. }
  331. $this->_data['title'] = $title;
  332. }
  333. /**
  334. * Get an array with feed authors
  335. *
  336. * @return array
  337. */
  338. public function getAuthors()
  339. {
  340. if (!array_key_exists('authors', $this->_data)) {
  341. return null;
  342. }
  343. return $this->_data['authors'];
  344. }
  345. /**
  346. * Get the entry content
  347. *
  348. * @return string
  349. */
  350. public function getContent()
  351. {
  352. if (!array_key_exists('content', $this->_data)) {
  353. return null;
  354. }
  355. return $this->_data['content'];
  356. }
  357. /**
  358. * Get the entry copyright information
  359. *
  360. * @return string
  361. */
  362. public function getCopyright()
  363. {
  364. if (!array_key_exists('copyright', $this->_data)) {
  365. return null;
  366. }
  367. return $this->_data['copyright'];
  368. }
  369. /**
  370. * Get the entry creation date
  371. *
  372. * @return string
  373. */
  374. public function getDateCreated()
  375. {
  376. if (!array_key_exists('dateCreated', $this->_data)) {
  377. return null;
  378. }
  379. return $this->_data['dateCreated'];
  380. }
  381. /**
  382. * Get the entry modification date
  383. *
  384. * @return string
  385. */
  386. public function getDateModified()
  387. {
  388. if (!array_key_exists('dateModified', $this->_data)) {
  389. return null;
  390. }
  391. return $this->_data['dateModified'];
  392. }
  393. /**
  394. * Get the entry description
  395. *
  396. * @return string
  397. */
  398. public function getDescription()
  399. {
  400. if (!array_key_exists('description', $this->_data)) {
  401. return null;
  402. }
  403. return $this->_data['description'];
  404. }
  405. /**
  406. * Get the entry ID
  407. *
  408. * @return string
  409. */
  410. public function getId()
  411. {
  412. if (!array_key_exists('id', $this->_data)) {
  413. return null;
  414. }
  415. return $this->_data['id'];
  416. }
  417. /**
  418. * Get a link to the HTML source
  419. *
  420. * @return string|null
  421. */
  422. public function getLink()
  423. {
  424. if (!array_key_exists('link', $this->_data)) {
  425. return null;
  426. }
  427. return $this->_data['link'];
  428. }
  429. /**
  430. * Get all links
  431. *
  432. * @return array
  433. */
  434. public function getLinks()
  435. {
  436. if (!array_key_exists('links', $this->_data)) {
  437. return null;
  438. }
  439. return $this->_data['links'];
  440. }
  441. /**
  442. * Get the entry title
  443. *
  444. * @return string
  445. */
  446. public function getTitle()
  447. {
  448. if (!array_key_exists('title', $this->_data)) {
  449. return null;
  450. }
  451. return $this->_data['title'];
  452. }
  453. /**
  454. * Get the number of comments/replies for current entry
  455. *
  456. * @return integer
  457. */
  458. public function getCommentCount()
  459. {
  460. if (!array_key_exists('commentCount', $this->_data)) {
  461. return null;
  462. }
  463. return $this->_data['commentCount'];
  464. }
  465. /**
  466. * Returns a URI pointing to the HTML page where comments can be made on this entry
  467. *
  468. * @return string
  469. */
  470. public function getCommentLink()
  471. {
  472. if (!array_key_exists('commentLink', $this->_data)) {
  473. return null;
  474. }
  475. return $this->_data['commentLink'];
  476. }
  477. /**
  478. * Returns an array of URIs pointing to a feed of all comments for this entry
  479. * where the array keys indicate the feed type (atom, rss or rdf).
  480. *
  481. * @return string
  482. */
  483. public function getCommentFeedLinks()
  484. {
  485. if (!array_key_exists('commentFeedLinks', $this->_data)) {
  486. return null;
  487. }
  488. return $this->_data['commentFeedLinks'];
  489. }
  490. /**
  491. * Adds an enclosure to the entry.
  492. *
  493. * @param array $enclosures
  494. */
  495. public function setEnclosure(array $enclosure)
  496. {
  497. if (!isset($enclosure['type'])) {
  498. require_once 'Zend/Feed/Exception.php';
  499. throw new Zend_Feed_Exception('Enclosure "type" is not set');
  500. }
  501. if (!isset($enclosure['length'])) {
  502. require_once 'Zend/Feed/Exception.php';
  503. throw new Zend_Feed_Exception('Enclosure "length" is not set');
  504. }
  505. if (!isset($enclosure['uri'])) {
  506. require_once 'Zend/Feed/Exception.php';
  507. throw new Zend_Feed_Exception('Enclosure "uri" is not set');
  508. }
  509. if (!Zend_Uri::check($enclosure['uri'])) {
  510. require_once 'Zend/Feed/Exception.php';
  511. throw new Zend_Feed_Exception('Enclosure "uri" is not a valid URI/IRI');
  512. }
  513. if ((int) $enclosure['length'] <= 0) {
  514. require_once 'Zend/Feed/Exception.php';
  515. throw new Zend_Feed_Exception('Enclosure "length" must be an integer'
  516. . ' indicating the content\'s length in bytes');
  517. }
  518. $this->_data['enclosure'] = $enclosure;
  519. }
  520. /**
  521. * Retrieve an array of all enclosures to be added to entry.
  522. *
  523. * @return array
  524. */
  525. public function getEnclosure()
  526. {
  527. if (!array_key_exists('enclosure', $this->_data)) {
  528. return null;
  529. }
  530. return $this->_data['enclosure'];
  531. }
  532. /**
  533. * Unset a specific data point
  534. *
  535. * @param string $name
  536. */
  537. public function remove($name)
  538. {
  539. if (isset($this->_data[$name])) {
  540. unset($this->_data[$name]);
  541. }
  542. }
  543. /**
  544. * Get registered extensions
  545. *
  546. * @return array
  547. */
  548. public function getExtensions()
  549. {
  550. return $this->_extensions;
  551. }
  552. /**
  553. * Return an Extension object with the matching name (postfixed with _Entry)
  554. *
  555. * @param string $name
  556. * @return object
  557. */
  558. public function getExtension($name)
  559. {
  560. if (array_key_exists($name . '_Entry', $this->_extensions)) {
  561. return $this->_extensions[$name . '_Entry'];
  562. }
  563. return null;
  564. }
  565. /**
  566. * Set the current feed type being exported to "rss" or "atom". This allows
  567. * other objects to gracefully choose whether to execute or not, depending
  568. * on their appropriateness for the current type, e.g. renderers.
  569. *
  570. * @param string $type
  571. */
  572. public function setType($type)
  573. {
  574. $this->_type = $type;
  575. }
  576. /**
  577. * Retrieve the current or last feed type exported.
  578. *
  579. * @return string Value will be "rss" or "atom"
  580. */
  581. public function getType()
  582. {
  583. return $this->_type;
  584. }
  585. /**
  586. * Method overloading: call given method on first extension implementing it
  587. *
  588. * @param string $method
  589. * @param array $args
  590. * @return mixed
  591. * @throws Zend_Feed_Exception if no extensions implements the method
  592. */
  593. public function __call($method, $args)
  594. {
  595. foreach ($this->_extensions as $extension) {
  596. try {
  597. return call_user_func_array(array($extension, $method), $args);
  598. } catch (Zend_Feed_Writer_Exception_InvalidMethodException $e) {
  599. }
  600. }
  601. require_once 'Zend/Feed/Exception.php';
  602. throw new Zend_Feed_Exception('Method: ' . $method
  603. . ' does not exist and could not be located on a registered Extension');
  604. }
  605. /**
  606. * Load extensions from Zend_Feed_Writer
  607. *
  608. * @return void
  609. */
  610. protected function _loadExtensions()
  611. {
  612. $all = Zend_Feed_Writer::getExtensions();
  613. $exts = $all['entry'];
  614. foreach ($exts as $ext) {
  615. $className = Zend_Feed_Writer::getPluginLoader()->getClassName($ext);
  616. $this->_extensions[$ext] = new $className();
  617. $this->_extensions[$ext]->setEncoding($this->getEncoding());
  618. }
  619. }
  620. }