PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Roumen/Feed/Feed.php

https://gitlab.com/pomirleanu.florentin/laravel-feed
PHP | 489 lines | 220 code | 65 blank | 204 comment | 44 complexity | 054e35b011122a69a5abb8bf48ec5981 MD5 | raw file
  1. <?php namespace Roumen\Feed;
  2. /**
  3. * Feed generator class for laravel-feed package.
  4. *
  5. * @author Roumen Damianoff <roumen@dawebs.com>
  6. * @version 2.10.2
  7. * @link https://roumen.it/projects/laravel-feed
  8. * @license http://opensource.org/licenses/mit-license.php MIT License
  9. */
  10. use Illuminate\Support\Facades\Config;
  11. use Illuminate\Support\Facades\Response;
  12. use Illuminate\Support\Facades\View;
  13. use Illuminate\Support\Facades\Cache;
  14. class Feed
  15. {
  16. /**
  17. * @var array
  18. */
  19. private $items = [];
  20. /**
  21. * @var string
  22. */
  23. public $title = 'My feed title';
  24. /**
  25. * @var string
  26. */
  27. public $description = 'My feed description';
  28. /**
  29. * @var string
  30. */
  31. public $link;
  32. /**
  33. * @var string
  34. */
  35. public $logo;
  36. /**
  37. * @var string
  38. */
  39. public $icon;
  40. /**
  41. * @var string
  42. */
  43. public $cover;
  44. /**
  45. * @var string
  46. */
  47. public $color;
  48. /**
  49. * @var string
  50. */
  51. public $ga;
  52. /**
  53. * @var boolean
  54. */
  55. public $related = false;
  56. /**
  57. * @var string
  58. */
  59. public $copyright = '';
  60. /**
  61. * @var string
  62. */
  63. public $pubdate;
  64. /**
  65. * @var string
  66. */
  67. public $lang;
  68. /**
  69. * @var string
  70. */
  71. public $charset = 'utf-8';
  72. /**
  73. * @var string
  74. */
  75. public $ctype = null;
  76. /**
  77. * @var integer
  78. */
  79. private $caching = 0;
  80. /**
  81. * @var string
  82. */
  83. private $cacheKey = 'laravel-feed';
  84. /**
  85. * @var boolean
  86. */
  87. private $shortening = false;
  88. /**
  89. * @var integer
  90. */
  91. private $shorteningLimit = 150;
  92. /**
  93. * @var string
  94. */
  95. private $dateFormat = 'datetime';
  96. /**
  97. * @var array
  98. */
  99. private $namespaces = [];
  100. /**
  101. * @var string
  102. */
  103. private $customView = null;
  104. /**
  105. * Add new item to $items array
  106. *
  107. * @param string $title
  108. * @param string $author
  109. * @param string $link
  110. * @param string $pubdate
  111. * @param string $description
  112. * @param string $content
  113. * @param array $enclosure (optional)
  114. * @param string $category (optional)
  115. *
  116. * @return void
  117. */
  118. public function add($title, $author, $link, $pubdate, $description, $content='', $enclosure = [], $category='')
  119. {
  120. // shortening the description
  121. if ($this->shortening)
  122. {
  123. $description = mb_substr($description, 0, $this->shorteningLimit, 'UTF-8');
  124. }
  125. // add to items
  126. $this->setItem([
  127. 'title' => $title,
  128. 'author' => $author,
  129. 'link' => $link,
  130. 'pubdate' => $pubdate,
  131. 'description' => $description,
  132. 'content' => $content,
  133. 'enclosure' => $enclosure,
  134. 'category' => $category
  135. ]);
  136. }
  137. /**
  138. * Add new items to $items array
  139. *
  140. * @param array $a
  141. *
  142. * @return void
  143. */
  144. public function addItem(array $item)
  145. {
  146. // if is multidimensional
  147. if (array_key_exists(1, $item))
  148. {
  149. foreach ($item as $i)
  150. {
  151. $this->addItem($i);
  152. }
  153. return;
  154. }
  155. if ($this->shortening)
  156. {
  157. $item['description'] = mb_substr($item['description'], 0, $this->shorteningLimit, 'UTF-8');
  158. }
  159. $this->setItem($item);
  160. }
  161. /**
  162. * Returns aggregated feed with all items from $items array
  163. *
  164. * @param string $format (options: 'atom', 'rss')
  165. * @param carbon|datetime|integer $cache (0 - turns off the cache)
  166. * @param string $key
  167. *
  168. * @return view
  169. */
  170. public function render($format = null, $cache = null, $key = null)
  171. {
  172. if ($format == null && $this->customView == null) $format = "atom";
  173. if ($this->customView == null) $this->customView = $format;
  174. if ($cache != null) $this->caching = $cache;
  175. if ($key != null) $this->cacheKey = $key;
  176. if ($this->ctype == null)
  177. {
  178. ($format == 'rss') ? $this->ctype = 'application/rss+xml' : $this->ctype = 'application/atom+xml';
  179. }
  180. // if cache is on and there is cached feed => return it
  181. if ($this->caching > 0 && Cache::has($this->cacheKey))
  182. {
  183. return Response::make(Cache::get($this->cacheKey), 200, array('Content-Type' => $this->ctype.'; charset='.$this->charset));
  184. }
  185. if (empty($this->lang)) $this->lang = Config::get('application.language');
  186. if (empty($this->link)) $this->link = Config::get('application.url');
  187. if (empty($this->pubdate)) $this->pubdate = date('D, d M Y H:i:s O');
  188. foreach($this->items as $k => $v)
  189. {
  190. $this->items[$k]['title'] = htmlentities(strip_tags($this->items[$k]['title']));
  191. $this->items[$k]['pubdate'] = $this->formatDate($this->items[$k]['pubdate'], $format);
  192. }
  193. $channel = [
  194. 'title' => htmlentities(strip_tags($this->title)),
  195. 'description' => $this->description,
  196. 'logo' => $this->logo,
  197. 'icon' => $this->icon,
  198. 'color' => $this->color,
  199. 'cover' => $this->cover,
  200. 'ga' => $this->ga,
  201. 'related' => $this->related,
  202. 'link' => $this->link,
  203. 'pubdate' => $this->formatDate($this->pubdate, $format),
  204. 'lang' => $this->lang,
  205. 'copyright' => $this->copyright
  206. ];
  207. $viewData = [
  208. 'items' => $this->items,
  209. 'channel' => $channel,
  210. 'namespaces' => $this->getNamespaces()
  211. ];
  212. // if cache is on put this feed in cache and return it
  213. if ($this->caching > 0)
  214. {
  215. Cache::put($this->cacheKey, View::make($this->getView($this->customView), $viewData)->render(), $this->caching);
  216. return Response::make(Cache::get($this->cacheKey), 200, array('Content-Type' => $this->ctype.'; charset='.$this->charset));
  217. }
  218. else if ($this->caching == 0)
  219. {
  220. // if cache is 0 delete the key (if exists) and return response
  221. $this->clearCache();
  222. return Response::make(View::make($this->getView($this->customView), $viewData), 200, array('Content-Type' => $this->ctype.'; charset='.$this->charset));
  223. }
  224. else if ($this->caching < 0)
  225. {
  226. // if cache is negative value delete the key (if exists) and return cachable object
  227. $this->clearCache();
  228. return View::make($this->getView($this->customView), $viewData)->render();
  229. }
  230. }
  231. /**
  232. * Create link
  233. *
  234. * @param string $url
  235. * @param string $type
  236. * @param string $title
  237. * @param string $lang
  238. *
  239. * @return string
  240. */
  241. public static function link($url, $type='atom', $title=null, $lang=null)
  242. {
  243. if ($type == 'rss') $type = 'application/rss+xml';
  244. if ($type == 'atom') $type = 'application/atom+xml';
  245. if ($title != null) $title = ' title="'.$title.'"';
  246. if ($lang != null) $lang = ' hreflang="'.$lang.'"';
  247. return '<link rel="alternate"'.$lang.' type="'.$type.'" href="'.$url.'"'.$title.'>';
  248. }
  249. /**
  250. * Check if feed is cached
  251. *
  252. * @return bool
  253. */
  254. public function isCached()
  255. {
  256. if (Cache::has($this->cacheKey))
  257. {
  258. return true;
  259. }
  260. return false;
  261. }
  262. /**
  263. * Clear the cache
  264. *
  265. * @return void
  266. */
  267. public function clearCache()
  268. {
  269. if ($this->isCached()) Cache::forget($this->cacheKey);
  270. }
  271. /**
  272. * Set cache duration and key
  273. *
  274. * @return void
  275. */
  276. public function setCache($duration=60, $key="laravel-feed")
  277. {
  278. $this->cacheKey = $key;
  279. $this->caching = $duration;
  280. if ($duration < 1) $this->clearCache();
  281. }
  282. /**
  283. * Get view name
  284. *
  285. * @param string $format
  286. *
  287. * @return void
  288. */
  289. public function getView($format)
  290. {
  291. // if a custom view is set
  292. if ($this->customView !== null && View::exists($this->customView))
  293. {
  294. return $this->customView;
  295. }
  296. // else return default view
  297. return 'feed::'.$format;
  298. }
  299. /**
  300. * Set Custom view
  301. *
  302. * @param string $name
  303. *
  304. * @return void
  305. */
  306. public function setView($name=null)
  307. {
  308. $this->customView = $name;
  309. }
  310. /**
  311. * Set maximum characters lenght for text shortening
  312. *
  313. * @param integer $l
  314. *
  315. * @return void
  316. */
  317. public function setTextLimit($l=150)
  318. {
  319. $this->shorteningLimit = $l;
  320. }
  321. /**
  322. * Turn on/off text shortening for item content
  323. *
  324. * @param boolean $b
  325. *
  326. * @return void
  327. */
  328. public function setShortening($b=false)
  329. {
  330. $this->shortening = $b;
  331. }
  332. /**
  333. * Format datetime string, timestamp integer or carbon object in valid feed format
  334. *
  335. * @param string/integer $date
  336. *
  337. * @return string
  338. */
  339. private function formatDate($date, $format='atom')
  340. {
  341. if ($format == "atom")
  342. {
  343. switch ($this->dateFormat)
  344. {
  345. case "carbon":
  346. $date = date('c', strtotime($date->toDateTimeString()));
  347. break;
  348. case "timestamp":
  349. $date = date('c', $date);
  350. break;
  351. case "datetime":
  352. $date = date('c', strtotime($date));
  353. break;
  354. }
  355. }
  356. else
  357. {
  358. switch ($this->dateFormat)
  359. {
  360. case "carbon":
  361. $date = date('D, d M Y H:i:s O', strtotime($date->toDateTimeString()));
  362. break;
  363. case "timestamp":
  364. $date = date('D, d M Y H:i:s O', $date);
  365. break;
  366. case "datetime":
  367. $date = date('D, d M Y H:i:s O', strtotime($date));
  368. break;
  369. }
  370. }
  371. return $date;
  372. }
  373. /**
  374. * Add namespace
  375. *
  376. * @param string $n
  377. *
  378. * @return void
  379. */
  380. public function addNamespace($n)
  381. {
  382. $this->namespaces[] = $n;
  383. }
  384. /**
  385. * Get all namespaces
  386. *
  387. * @param string $n
  388. *
  389. * @return void
  390. */
  391. public function getNamespaces()
  392. {
  393. return $this->namespaces;
  394. }
  395. /**
  396. * Setter for dateFormat
  397. *
  398. * @param string $format
  399. *
  400. * @return void
  401. */
  402. public function setDateFormat($format="datetime")
  403. {
  404. $this->dateFormat = $format;
  405. }
  406. /**
  407. * Returns $items array
  408. *
  409. * @return array
  410. */
  411. public function getItems()
  412. {
  413. return $this->items;
  414. }
  415. /**
  416. * Adds item to $items array
  417. *
  418. * @param array $item
  419. */
  420. public function setItem($item)
  421. {
  422. $this->items[] = $item;
  423. }
  424. }