Entry.php 20 KB

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