/plugins/sfFeed2Plugin/lib/sfFeed.class.php

https://github.com/malacon/rbwords · PHP · 345 lines · 216 code · 47 blank · 82 comment · 10 complexity · 87dc1bd7b05abf4bf495d375eb2b0e47 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the sfFeed2 package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2007 Francois Zaninotto <francois.zaninotto@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * sfFeed.
  12. *
  13. * based on feedgenerator.py from django project
  14. *
  15. * @package sfFeed2
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @author Francois Zaninotto <francois.zaninotto@symfony-project.com>
  18. */
  19. class sfFeed
  20. {
  21. protected
  22. $items = array(),
  23. $image,
  24. $title,
  25. $link,
  26. $description,
  27. $language = 'en',
  28. $authorEmail,
  29. $authorName,
  30. $authorLink,
  31. $subtitle,
  32. $categories = array(),
  33. $feedUrl,
  34. $encoding = 'UTF-8';
  35. public function construct($feed_array = array())
  36. {
  37. if($feed_array)
  38. {
  39. $this->initialize($feed_array);
  40. }
  41. }
  42. /**
  43. * Defines the feed properties, based on an associative array.
  44. *
  45. * @param array an associative array of feed parameters
  46. *
  47. * @return sfFeed the current sfFeed object
  48. */
  49. public function initialize($feed_array)
  50. {
  51. $this->setItems(isset($feed_array['items']) ? $feed_array['items'] : '');
  52. $this->setImage(isset($feed_array['image']) ? $feed_array['image'] : '');
  53. $this->setTitle(isset($feed_array['title']) ? $feed_array['title'] : '');
  54. $this->setLink(isset($feed_array['link']) ? $feed_array['link'] : '');
  55. $this->setDescription(isset($feed_array['description']) ? $feed_array['description'] : '');
  56. $this->setLanguage(isset($feed_array['language']) ? $feed_array['language'] : $this->language);
  57. $this->setAuthorEmail(isset($feed_array['authorEmail']) ? $feed_array['authorEmail'] : '');
  58. $this->setAuthorName(isset($feed_array['authorName']) ? $feed_array['authorName'] : '');
  59. $this->setAuthorLink(isset($feed_array['authorLink']) ? $feed_array['authorLink'] : '');
  60. $this->setSubtitle(isset($feed_array['subtitle']) ? $feed_array['subtitle'] : '');
  61. $this->setCategories(isset($feed_array['categories']) ? $feed_array['categories'] : '');
  62. $this->setFeedUrl(isset($feed_array['feedUrl']) ? $feed_array['feedUrl'] : '');
  63. $this->setEncoding(isset($feed_array['encoding']) ? $feed_array['encoding'] : $this->encoding);
  64. return $this;
  65. }
  66. /**
  67. * Retrieves the feed items.
  68. *
  69. * @return array an array of sfFeedItem objects
  70. */
  71. public function getItems()
  72. {
  73. return $this->items;
  74. }
  75. /**
  76. * Defines the items of the feed.
  77. *
  78. * Caution: in previous versions, this method used to accept all kinds of objects.
  79. * Now only objects of class sfFeedItem are allowed.
  80. *
  81. * @param array an array of sfFeedItem objects
  82. *
  83. * @return sfFeed the current sfFeed object
  84. */
  85. public function setItems($items = array())
  86. {
  87. $this->items = array();
  88. $this->addItems($items);
  89. return $this;
  90. }
  91. /**
  92. * Adds one item to the feed.
  93. *
  94. * @param sfFeedItem an item object
  95. *
  96. * @return sfFeed the current sfFeed object
  97. */
  98. public function addItem($item)
  99. {
  100. if (!($item instanceof sfFeedItem))
  101. {
  102. // the object is of the wrong class
  103. $error = 'Parameter of addItem() is not of class sfFeedItem';
  104. throw new Exception($error);
  105. }
  106. $item->setFeed($this);
  107. $this->items[] = $item;
  108. return $this;
  109. }
  110. /**
  111. * Adds several items to the feed.
  112. *
  113. * @param array an array of sfFeedItem objects
  114. *
  115. * @return sfFeed the current sfFeed object
  116. */
  117. public function addItems($items)
  118. {
  119. if(is_array($items))
  120. {
  121. foreach($items as $item)
  122. {
  123. $this->addItem($item);
  124. }
  125. }
  126. return $this;
  127. }
  128. /**
  129. * Adds one item to the feed, based on an associative array.
  130. *
  131. * @param array an associative array
  132. *
  133. * @return sfFeed the current sfFeed object
  134. */
  135. public function addItemFromArray($item_array)
  136. {
  137. $this->items[] = new sfFeedItem($item_array);
  138. return $this;
  139. }
  140. /**
  141. * Removes the last items of the feed so that the total number doesn't bypass the limit defined as a parameter.
  142. *
  143. * @param integer the maximum number of items
  144. *
  145. * @return sfFeed the current sfFeed object
  146. */
  147. public function keepOnlyItems($count = 10)
  148. {
  149. if($count < count($this->items))
  150. {
  151. $this->items = array_slice($this->items, 0, $count);
  152. }
  153. return $this;
  154. }
  155. /**
  156. * Retrieves the feed image
  157. *
  158. * @return sfFeedImage actual sfFeedImage object
  159. */
  160. public function getImage()
  161. {
  162. return $this->image;
  163. }
  164. /**
  165. * Defines the image/icon of the feed
  166. *
  167. * @param image sfFeedImage object
  168. *
  169. * @return sfFeed the current sfFeed object
  170. */
  171. public function setImage($image)
  172. {
  173. $this->image = $image;
  174. return $this;
  175. }
  176. public function setTitle ($title)
  177. {
  178. $this->title = $title;
  179. //if an image is there that has no title yet set it as well
  180. if ($this->image instanceof sfFeedImage && !$this->image->getTitle())
  181. {
  182. $this->image->setTitle($title);
  183. }
  184. }
  185. public function getTitle ()
  186. {
  187. return $this->title;
  188. }
  189. public function setLink ($link)
  190. {
  191. $this->link = $link;
  192. //if an image is there that has no link yet set it as well
  193. if ($this->image instanceof sfFeedImage && !$this->image->getLink())
  194. {
  195. $this->image->setLink($link);
  196. }
  197. }
  198. public function getLink ()
  199. {
  200. return $this->link;
  201. }
  202. public function setDescription ($description)
  203. {
  204. $this->description = $description;
  205. }
  206. public function getDescription ()
  207. {
  208. return $this->description;
  209. }
  210. public function setLanguage ($language)
  211. {
  212. $this->language = $language;
  213. }
  214. public function getLanguage ()
  215. {
  216. return $this->language;
  217. }
  218. public function setAuthorEmail ($authorEmail)
  219. {
  220. $this->authorEmail = $authorEmail;
  221. }
  222. public function getAuthorEmail ()
  223. {
  224. return $this->authorEmail;
  225. }
  226. public function setAuthorName ($authorName)
  227. {
  228. $this->authorName = $authorName;
  229. }
  230. public function getAuthorName ()
  231. {
  232. return $this->authorName;
  233. }
  234. public function setAuthorLink ($authorLink)
  235. {
  236. $this->authorLink = $authorLink;
  237. }
  238. public function getAuthorLink ()
  239. {
  240. return $this->authorLink;
  241. }
  242. public function setSubtitle ($subtitle)
  243. {
  244. $this->subtitle = $subtitle;
  245. }
  246. public function getSubtitle ()
  247. {
  248. return $this->subtitle;
  249. }
  250. public function setCategories ($categories)
  251. {
  252. $this->categories = $categories;
  253. }
  254. public function getCategories ()
  255. {
  256. return $this->categories;
  257. }
  258. public function setFeedUrl ($feedUrl)
  259. {
  260. $this->feedUrl = $feedUrl;
  261. }
  262. public function getFeedUrl ()
  263. {
  264. return $this->feedUrl;
  265. }
  266. public function getEncoding()
  267. {
  268. return $this->encoding;
  269. }
  270. public function setEncoding($encoding)
  271. {
  272. $this->encoding = $encoding;
  273. }
  274. public function getLatestPostDate()
  275. {
  276. $updates = array();
  277. foreach ($this->getItems() as $item)
  278. {
  279. if ($item->getPubdate())
  280. {
  281. $updates[] = $item->getPubdate();
  282. }
  283. }
  284. if ($updates)
  285. {
  286. sort($updates);
  287. return array_pop($updates);
  288. }
  289. else
  290. {
  291. return time();
  292. }
  293. }
  294. public function asXml()
  295. {
  296. throw new sfException('You must use newInstance to get a real feed.');
  297. }
  298. }