AbstractFeed.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ UdE+MOk0fNVzuT+ELYHRzTd/YUtDMbEhFzp7goBrq+M=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.6
  11. */
  12. /**
  13. * abstract feed class
  14. */
  15. namespace Cube\Feed;
  16. abstract class AbstractFeed
  17. {
  18. /**
  19. *
  20. * feed entries
  21. *
  22. * @var array
  23. */
  24. protected $_entries = array();
  25. /**
  26. *
  27. * channels
  28. *
  29. * @var array
  30. */
  31. protected $_channels = array();
  32. /**
  33. * @param array $entries
  34. *
  35. * @return $this
  36. */
  37. public function setEntries($entries)
  38. {
  39. foreach ((array)$entries as $entry) {
  40. $this->addEntry($entry);
  41. }
  42. return $this;
  43. }
  44. /**
  45. *
  46. * add entry to entries array
  47. *
  48. * @param \Cube\Feed\Entry $entry
  49. *
  50. * @return $this
  51. */
  52. public function addEntry(Entry $entry)
  53. {
  54. $this->_entries[] = $entry;
  55. return $this;
  56. }
  57. /**
  58. * @return array
  59. */
  60. public function getEntries()
  61. {
  62. return $this->_entries;
  63. }
  64. /**
  65. *
  66. * clear entries array
  67. *
  68. * @return $this
  69. */
  70. public function clearEntries()
  71. {
  72. $this->_entries = array();
  73. return $this;
  74. }
  75. /**
  76. *
  77. * set channels
  78. *
  79. * @param array $channels
  80. *
  81. * @return $this
  82. */
  83. public function setChannels($channels)
  84. {
  85. foreach ((array)$channels as $key => $value) {
  86. $this->addChannel($key, $value);
  87. }
  88. return $this;
  89. }
  90. /**
  91. *
  92. * add channel
  93. *
  94. * @param string $key
  95. * @param string $value
  96. *
  97. * @return $this
  98. */
  99. public function addChannel($key, $value)
  100. {
  101. $this->_channels[$key] = $this->_formatString($value);
  102. return $this;
  103. }
  104. /**
  105. *
  106. * get channels
  107. *
  108. * @return array
  109. */
  110. public function getChannels()
  111. {
  112. return $this->_channels;
  113. }
  114. /**
  115. *
  116. * render one channel/element
  117. *
  118. * @param array $array
  119. *
  120. * @return null|string
  121. */
  122. protected function _renderArray($array)
  123. {
  124. $output = '';
  125. foreach ($array as $key => $value) {
  126. if (is_array($value)) {
  127. $output .= PHP_EOL . "<$key>" . $this->_renderArray($value) . "</$key>" . PHP_EOL;
  128. }
  129. else {
  130. $output .= PHP_EOL . "<$key>" . $this->_formatString($value) . "</$key>" . PHP_EOL;
  131. }
  132. }
  133. return $output;
  134. }
  135. /**
  136. *
  137. * format string
  138. *
  139. * @param mixed $input
  140. *
  141. * @return string
  142. */
  143. protected function _formatString($input)
  144. {
  145. if (is_array($input)) {
  146. return $input;
  147. }
  148. return strip_tags(
  149. str_ireplace(
  150. array('&'), array(' '), $input));
  151. // return strip_tags(
  152. // str_ireplace(
  153. // array('&amp;', '&#039;', '&quot;', '&lt;', '&gt;', '&nbsp;'), array('&', "'", '"', '<', '>', ' '), $string));
  154. }
  155. /**
  156. *
  157. * generate feed
  158. *
  159. * @return mixed
  160. */
  161. abstract function generateFeed();
  162. }