PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Feed/Writer/Entry.php

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