UserQuery.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Contains the description of a user query
  4. *
  5. * It allows to extract the meaningful bits of the query to be manipulated in an
  6. * easy way.
  7. */
  8. class FreshRSS_UserQuery {
  9. private $deprecated = false;
  10. private $get;
  11. private $get_name;
  12. private $get_type;
  13. private $name;
  14. private $order;
  15. private $search;
  16. private $state;
  17. private $url;
  18. private $feed_dao;
  19. private $category_dao;
  20. /**
  21. * @param array $query
  22. * @param FreshRSS_Searchable $feed_dao
  23. * @param FreshRSS_Searchable $category_dao
  24. */
  25. public function __construct($query, FreshRSS_Searchable $feed_dao = null, FreshRSS_Searchable $category_dao = null) {
  26. $this->category_dao = $category_dao;
  27. $this->feed_dao = $feed_dao;
  28. if (isset($query['get'])) {
  29. $this->parseGet($query['get']);
  30. }
  31. if (isset($query['name'])) {
  32. $this->name = $query['name'];
  33. }
  34. if (isset($query['order'])) {
  35. $this->order = $query['order'];
  36. }
  37. if (!isset($query['search'])) {
  38. $query['search'] = '';
  39. }
  40. // linked to deeply with the search object, need to use dependency injection
  41. $this->search = new FreshRSS_Search($query['search']);
  42. if (isset($query['state'])) {
  43. $this->state = $query['state'];
  44. }
  45. if (isset($query['url'])) {
  46. $this->url = $query['url'];
  47. }
  48. }
  49. /**
  50. * Convert the current object to an array.
  51. *
  52. * @return array
  53. */
  54. public function toArray() {
  55. return array_filter(array(
  56. 'get' => $this->get,
  57. 'name' => $this->name,
  58. 'order' => $this->order,
  59. 'search' => $this->search->__toString(),
  60. 'state' => $this->state,
  61. 'url' => $this->url,
  62. ));
  63. }
  64. /**
  65. * Parse the get parameter in the query string to extract its name and
  66. * type
  67. *
  68. * @param string $get
  69. */
  70. private function parseGet($get) {
  71. $this->get = $get;
  72. if (preg_match('/(?P<type>[acfs])(_(?P<id>\d+))?/', $get, $matches)) {
  73. switch ($matches['type']) {
  74. case 'a':
  75. $this->parseAll();
  76. break;
  77. case 'c':
  78. $this->parseCategory($matches['id']);
  79. break;
  80. case 'f':
  81. $this->parseFeed($matches['id']);
  82. break;
  83. case 's':
  84. $this->parseFavorite();
  85. break;
  86. }
  87. }
  88. }
  89. /**
  90. * Parse the query string when it is an "all" query
  91. */
  92. private function parseAll() {
  93. $this->get_name = 'all';
  94. $this->get_type = 'all';
  95. }
  96. /**
  97. * Parse the query string when it is a "category" query
  98. *
  99. * @param integer $id
  100. * @throws FreshRSS_DAO_Exception
  101. */
  102. private function parseCategory($id) {
  103. if (is_null($this->category_dao)) {
  104. throw new FreshRSS_DAO_Exception('Category DAO is not loaded in UserQuery');
  105. }
  106. $category = $this->category_dao->searchById($id);
  107. if ($category) {
  108. $this->get_name = $category->name();
  109. } else {
  110. $this->deprecated = true;
  111. }
  112. $this->get_type = 'category';
  113. }
  114. /**
  115. * Parse the query string when it is a "feed" query
  116. *
  117. * @param integer $id
  118. * @throws FreshRSS_DAO_Exception
  119. */
  120. private function parseFeed($id) {
  121. if (is_null($this->feed_dao)) {
  122. throw new FreshRSS_DAO_Exception('Feed DAO is not loaded in UserQuery');
  123. }
  124. $feed = $this->feed_dao->searchById($id);
  125. if ($feed) {
  126. $this->get_name = $feed->name();
  127. } else {
  128. $this->deprecated = true;
  129. }
  130. $this->get_type = 'feed';
  131. }
  132. /**
  133. * Parse the query string when it is a "favorite" query
  134. */
  135. private function parseFavorite() {
  136. $this->get_name = 'favorite';
  137. $this->get_type = 'favorite';
  138. }
  139. /**
  140. * Check if the current user query is deprecated.
  141. * It is deprecated if the category or the feed used in the query are
  142. * not existing.
  143. *
  144. * @return boolean
  145. */
  146. public function isDeprecated() {
  147. return $this->deprecated;
  148. }
  149. /**
  150. * Check if the user query has parameters.
  151. * If the type is 'all', it is considered equal to no parameters
  152. *
  153. * @return boolean
  154. */
  155. public function hasParameters() {
  156. if ($this->get_type === 'all') {
  157. return false;
  158. }
  159. if ($this->hasSearch()) {
  160. return true;
  161. }
  162. if ($this->state) {
  163. return true;
  164. }
  165. if ($this->order) {
  166. return true;
  167. }
  168. if ($this->get) {
  169. return true;
  170. }
  171. return false;
  172. }
  173. /**
  174. * Check if there is a search in the search object
  175. *
  176. * @return boolean
  177. */
  178. public function hasSearch() {
  179. return $this->search->getRawInput() != "";
  180. }
  181. public function getGet() {
  182. return $this->get;
  183. }
  184. public function getGetName() {
  185. return $this->get_name;
  186. }
  187. public function getGetType() {
  188. return $this->get_type;
  189. }
  190. public function getName() {
  191. return $this->name;
  192. }
  193. public function getOrder() {
  194. return $this->order;
  195. }
  196. public function getSearch() {
  197. return $this->search;
  198. }
  199. public function getState() {
  200. return $this->state;
  201. }
  202. public function getUrl() {
  203. return $this->url;
  204. }
  205. }