PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Feed/Writer/Feed/FeedAbstract.php

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