PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/devblocks/libs/ZendFramework/Zend/Feed/Builder.php

https://github.com/sluther/portsensor
PHP | 383 lines | 177 code | 20 blank | 186 comment | 40 complexity | 57802848ffb59a19a05f9830a5c9f4fd MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  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
  17. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Builder.php 3941 2007-03-14 21:36:13Z darby $
  20. */
  21. /**
  22. * @see Zend_Feed_Builder_Interface
  23. */
  24. require_once 'Zend/Feed/Builder/Interface.php';
  25. /**
  26. * @see Zend_Feed_Builder_Header
  27. */
  28. require_once 'Zend/Feed/Builder/Header.php';
  29. /**
  30. * @see Zend_Feed_Builder_Entry
  31. */
  32. require_once 'Zend/Feed/Builder/Entry.php';
  33. /**
  34. * @see Zend_Feed_Exception
  35. */
  36. require_once 'Zend/Feed/Exception.php';
  37. /**
  38. * A simple implementation of Zend_Feed_Builder_Interface.
  39. *
  40. * Users are encouraged to make their own classes to implement Zend_Feed_Builder_Interface
  41. *
  42. * @category Zend
  43. * @package Zend_Feed
  44. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. */
  47. class Zend_Feed_Builder implements Zend_Feed_Builder_Interface
  48. {
  49. /**
  50. * The data of the feed
  51. *
  52. * @var $_data array
  53. */
  54. private $_data;
  55. /**
  56. * Header of the feed
  57. *
  58. * @var $_header Zend_Feed_Builder_Header
  59. */
  60. private $_header;
  61. /**
  62. * List of the entries of the feed
  63. *
  64. * @var $_entries array
  65. */
  66. private $_entries = array();
  67. /**
  68. * Constructor. The $data array must conform to the following format:
  69. * <code>
  70. * array(
  71. * 'title' => 'title of the feed', //required
  72. * 'link' => 'canonical url to the feed', //required
  73. * 'lastUpdate' => 'timestamp of the update date', // optional
  74. * 'published' => 'timestamp of the publication date', //optional
  75. * 'charset' => 'charset', // required
  76. * 'description' => 'short description of the feed', //optional
  77. * 'author' => 'author/publisher of the feed', //optional
  78. * 'email' => 'email of the author', //optional
  79. * 'webmaster' => 'email address for person responsible for technical issues' // optional, ignored if atom is used
  80. * 'copyright' => 'copyright notice', //optional
  81. * 'image' => 'url to image', //optional
  82. * 'generator' => 'generator', // optional
  83. * 'language' => 'language the feed is written in', // optional
  84. * 'ttl' => 'how long in minutes a feed can be cached before refreshing', // optional, ignored if atom is used
  85. * 'rating' => 'The PICS rating for the channel.', // optional, ignored if atom is used
  86. * 'cloud' => array(
  87. * 'domain' => 'domain of the cloud, e.g. rpc.sys.com' // required
  88. * 'port' => 'port to connect to' // optional, default to 80
  89. * 'path' => 'path of the cloud, e.g. /RPC2 //required
  90. * 'registerProcedure' => 'procedure to call, e.g. myCloud.rssPleaseNotify' // required
  91. * 'protocol' => 'protocol to use, e.g. soap or xml-rpc' // required
  92. * ), a cloud to be notified of updates // optional, ignored if atom is used
  93. * 'textInput' => array(
  94. * 'title' => 'the label of the Submit button in the text input area' // required,
  95. * 'description' => 'explains the text input area' // required
  96. * 'name' => 'the name of the text object in the text input area' // required
  97. * 'link' => 'the URL of the CGI script that processes text input requests' // required
  98. * ) // a text input box that can be displayed with the feed // optional, ignored if atom is used
  99. * 'skipHours' => array(
  100. * 'hour in 24 format', // e.g 13 (1pm)
  101. * // up to 24 rows whose value is a number between 0 and 23
  102. * ) // Hint telling aggregators which hours they can skip // optional, ignored if atom is used
  103. * 'skipDays ' => array(
  104. * 'a day to skip', // e.g Monday
  105. * // up to 7 rows whose value is a Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday
  106. * ) // Hint telling aggregators which days they can skip // optional, ignored if atom is used
  107. * 'itunes' => array(
  108. * 'author' => 'Artist column' // optional, default to the main author value
  109. * 'owner' => array(
  110. * 'name' => 'name of the owner' // optional, default to main author value
  111. * 'email' => 'email of the owner' // optional, default to main email value
  112. * ) // Owner of the podcast // optional
  113. * 'image' => 'album/podcast art' // optional, default to the main image value
  114. * 'subtitle' => 'short description' // optional, default to the main description value
  115. * 'summary' => 'longer description' // optional, default to the main description value
  116. * 'block' => 'Prevent an episode from appearing (yes|no)' // optional
  117. * 'category' => array(
  118. * array('main' => 'main category', // required
  119. * 'sub' => 'sub category' // optional
  120. * ),
  121. * // up to 3 rows
  122. * ) // 'Category column and in iTunes Music Store Browse' // required
  123. * 'explicit' => 'parental advisory graphic (yes|no|clean)' // optional
  124. * 'keywords' => 'a comma separated list of 12 keywords maximum' // optional
  125. * 'new-feed-url' => 'used to inform iTunes of new feed URL location' // optional
  126. * ) // Itunes extension data // optional, ignored if atom is used
  127. * 'entries' => array(
  128. * array(
  129. * 'title' => 'title of the feed entry', //required
  130. * 'link' => 'url to a feed entry', //required
  131. * 'description' => 'short version of a feed entry', // only text, no html, required
  132. * 'guid' => 'id of the article, if not given link value will used', //optional
  133. * 'content' => 'long version', // can contain html, optional
  134. * 'lastUpdate' => 'timestamp of the publication date', // optional
  135. * 'comments' => 'comments page of the feed entry', // optional
  136. * 'commentRss' => 'the feed url of the associated comments', // optional
  137. * 'source' => array(
  138. * 'title' => 'title of the original source' // required,
  139. * 'url' => 'url of the original source' // required
  140. * ) // original source of the feed entry // optional
  141. * 'category' => array(
  142. * array(
  143. * 'term' => 'first category label' // required,
  144. * 'scheme' => 'url that identifies a categorization scheme' // optional
  145. * ),
  146. * array(
  147. * //data for the second category and so on
  148. * )
  149. * ) // list of the attached categories // optional
  150. * 'enclosure' => array(
  151. * array(
  152. * 'url' => 'url of the linked enclosure' // required
  153. * 'type' => 'mime type of the enclosure' // optional
  154. * 'length' => 'length of the linked content in octets' // optional
  155. * ),
  156. * array(
  157. * //data for the second enclosure and so on
  158. * )
  159. * ) // list of the enclosures of the feed entry // optional
  160. * ),
  161. * array(
  162. * //data for the second entry and so on
  163. * )
  164. * )
  165. * );
  166. * </code>
  167. *
  168. * @param array $data
  169. * @return void
  170. */
  171. public function __construct(array $data)
  172. {
  173. $this->_data = $data;
  174. $this->_createHeader($data);
  175. if (isset($data['entries'])) {
  176. $this->_createEntries($data['entries']);
  177. }
  178. }
  179. /**
  180. * Returns an instance of Zend_Feed_Builder_Header
  181. * describing the header of the feed
  182. *
  183. * @return Zend_Feed_Builder_Header
  184. */
  185. public function getHeader()
  186. {
  187. return $this->_header;
  188. }
  189. /**
  190. * Returns an array of Zend_Feed_Builder_Entry instances
  191. * describing the entries of the feed
  192. *
  193. * @return array of Zend_Feed_Builder_Entry
  194. */
  195. public function getEntries()
  196. {
  197. return $this->_entries;
  198. }
  199. /**
  200. * Create the Zend_Feed_Builder_Header instance
  201. *
  202. * @param array $data
  203. * @throws Zend_Feed_Builder_Exception
  204. * @return void
  205. */
  206. private function _createHeader(array $data)
  207. {
  208. $mandatories = array('title', 'link', 'charset');
  209. foreach ($mandatories as $mandatory) {
  210. if (!isset($data[$mandatory])) {
  211. throw new Zend_Feed_Builder_Exception("$mandatory key is missing");
  212. }
  213. }
  214. $this->_header = new Zend_Feed_Builder_Header($data['title'], $data['link'], $data['charset']);
  215. if (isset($data['lastUpdate'])) {
  216. $this->_header->setLastUpdate($data['lastUpdate']);
  217. }
  218. if (isset($data['published'])) {
  219. $this->_header->setPublishedDate($data['published']);
  220. }
  221. if (isset($data['description'])) {
  222. $this->_header->setDescription($data['description']);
  223. }
  224. if (isset($data['author'])) {
  225. $this->_header->setAuthor($data['author']);
  226. }
  227. if (isset($data['email'])) {
  228. $this->_header->setEmail($data['email']);
  229. }
  230. if (isset($data['webmaster'])) {
  231. $this->_header->setWebmaster($data['webmaster']);
  232. }
  233. if (isset($data['copyright'])) {
  234. $this->_header->setCopyright($data['copyright']);
  235. }
  236. if (isset($data['image'])) {
  237. $this->_header->setImage($data['image']);
  238. }
  239. if (isset($data['generator'])) {
  240. $this->_header->setGenerator($data['generator']);
  241. }
  242. if (isset($data['language'])) {
  243. $this->_header->setLanguage($data['language']);
  244. }
  245. if (isset($data['ttl'])) {
  246. $this->_header->setTtl($data['ttl']);
  247. }
  248. if (isset($data['rating'])) {
  249. $this->_header->setRating($data['rating']);
  250. }
  251. if (isset($data['cloud'])) {
  252. $mandatories = array('domain', 'path', 'registerProcedure', 'protocol');
  253. foreach ($mandatories as $mandatory) {
  254. if (!isset($data['cloud'][$mandatory])) {
  255. throw new Zend_Feed_Builder_Exception("you have to define $mandatory property of your cloud");
  256. }
  257. }
  258. $uri_str = 'http://' . $data['cloud']['domain'] . $data['cloud']['path'];
  259. $this->_header->setCloud($uri_str, $data['cloud']['registerProcedure'], $data['cloud']['protocol']);
  260. }
  261. if (isset($data['textInput'])) {
  262. $mandatories = array('title', 'description', 'name', 'link');
  263. foreach ($mandatories as $mandatory) {
  264. if (!isset($data['textInput'][$mandatory])) {
  265. throw new Zend_Feed_Builder_Exception("you have to define $mandatory property of your textInput");
  266. }
  267. }
  268. $this->_header->setTextInput($data['textInput']['title'],
  269. $data['textInput']['description'],
  270. $data['textInput']['name'],
  271. $data['textInput']['link']);
  272. }
  273. if (isset($data['skipHours'])) {
  274. $this->_header->setSkipHours($data['skipHours']);
  275. }
  276. if (isset($data['skipDays'])) {
  277. $this->_header->setSkipDays($data['skipDays']);
  278. }
  279. if (isset($data['itunes'])) {
  280. $itunes = new Zend_Feed_Builder_Header_Itunes($data['itunes']['category']);
  281. if (isset($data['itunes']['author'])) {
  282. $itunes->setAuthor($data['itunes']['author']);
  283. }
  284. if (isset($data['itunes']['owner'])) {
  285. $name = isset($data['itunes']['owner']['name']) ? $data['itunes']['owner']['name'] : '';
  286. $email = isset($data['itunes']['owner']['email']) ? $data['itunes']['owner']['email'] : '';
  287. $itunes->setOwner($name, $email);
  288. }
  289. if (isset($data['itunes']['image'])) {
  290. $itunes->setImage($data['itunes']['image']);
  291. }
  292. if (isset($data['itunes']['subtitle'])) {
  293. $itunes->setSubtitle($data['itunes']['subtitle']);
  294. }
  295. if (isset($data['itunes']['summary'])) {
  296. $itunes->setSummary($data['itunes']['summary']);
  297. }
  298. if (isset($data['itunes']['block'])) {
  299. $itunes->setBlock($data['itunes']['block']);
  300. }
  301. if (isset($data['itunes']['explicit'])) {
  302. $itunes->setExplicit($data['itunes']['explicit']);
  303. }
  304. if (isset($data['itunes']['keywords'])) {
  305. $itunes->setKeywords($data['itunes']['keywords']);
  306. }
  307. if (isset($data['itunes']['new-feed-url'])) {
  308. $itunes->setNewFeedUrl($data['itunes']['new-feed-url']);
  309. }
  310. $this->_header->setITunes($itunes);
  311. }
  312. }
  313. /**
  314. * Create the array of article entries
  315. *
  316. * @param array $data
  317. * @throws Zend_Feed_Builder_Exception
  318. * @return void
  319. */
  320. private function _createEntries(array $data)
  321. {
  322. foreach ($data as $row) {
  323. $mandatories = array('title', 'link', 'description');
  324. foreach ($mandatories as $mandatory) {
  325. if (!isset($row[$mandatory])) {
  326. throw new Zend_Feed_Builder_Exception("$mandatory key is missing");
  327. }
  328. }
  329. $entry = new Zend_Feed_Builder_Entry($row['title'], $row['link'], $row['description']);
  330. if (isset($row['guid'])) {
  331. $entry->setId($row['guid']);
  332. }
  333. if (isset($row['content'])) {
  334. $entry->setContent($row['content']);
  335. }
  336. if (isset($row['lastUpdate'])) {
  337. $entry->setLastUpdate($row['lastUpdate']);
  338. }
  339. if (isset($row['comments'])) {
  340. $entry->setCommentsUrl($row['comments']);
  341. }
  342. if (isset($row['commentRss'])) {
  343. $entry->setCommentsRssUrl($row['commentRss']);
  344. }
  345. if (isset($row['source'])) {
  346. $mandatories = array('title', 'url');
  347. foreach ($mandatories as $mandatory) {
  348. if (!isset($row['source'][$mandatory])) {
  349. throw new Zend_Feed_Builder_Exception("$mandatory key of source property is missing");
  350. }
  351. }
  352. $entry->setSource($row['source']['title'], $row['source']['url']);
  353. }
  354. if (isset($row['category'])) {
  355. $entry->setCategories($row['category']);
  356. }
  357. if (isset($row['enclosure'])) {
  358. $entry->setEnclosures($row['enclosure']);
  359. }
  360. $this->_entries[] = $entry;
  361. }
  362. }
  363. }