PageRenderTime 86ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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