Entry.php 21 KB

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