UserQuery.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /** @var bool */
  10. private $deprecated = false;
  11. /** @var string */
  12. private $get = '';
  13. /** @var string */
  14. private $get_name = '';
  15. /** @var string */
  16. private $get_type = '';
  17. /** @var string */
  18. private $name = '';
  19. /** @var string */
  20. private $order = '';
  21. /** @var FreshRSS_BooleanSearch */
  22. private $search;
  23. /** @var int */
  24. private $state = 0;
  25. /** @var string */
  26. private $url = '';
  27. /** @var FreshRSS_FeedDAO|null */
  28. private $feed_dao;
  29. /** @var FreshRSS_CategoryDAO|null */
  30. private $category_dao;
  31. /** @var FreshRSS_TagDAO|null */
  32. private $tag_dao;
  33. /**
  34. * @param array{'get'?:string,'name'?:string,'order'?:string,'search'?:string,'state'?:int,'url'?:string} $query
  35. */
  36. public function __construct(array $query, FreshRSS_FeedDAO $feed_dao = null, FreshRSS_CategoryDAO $category_dao = null, FreshRSS_TagDAO $tag_dao = null) {
  37. $this->category_dao = $category_dao;
  38. $this->feed_dao = $feed_dao;
  39. $this->tag_dao = $tag_dao;
  40. if (isset($query['get'])) {
  41. $this->parseGet($query['get']);
  42. }
  43. if (isset($query['name'])) {
  44. $this->name = trim($query['name']);
  45. }
  46. if (isset($query['order'])) {
  47. $this->order = $query['order'];
  48. }
  49. if (empty($query['url'])) {
  50. if (!empty($query)) {
  51. unset($query['name']);
  52. $this->url = Minz_Url::display(['params' => $query]);
  53. }
  54. } else {
  55. $this->url = $query['url'];
  56. }
  57. if (!isset($query['search'])) {
  58. $query['search'] = '';
  59. }
  60. // linked too deeply with the search object, need to use dependency injection
  61. $this->search = new FreshRSS_BooleanSearch($query['search']);
  62. if (!empty($query['state'])) {
  63. $this->state = intval($query['state']);
  64. }
  65. }
  66. /**
  67. * Convert the current object to an array.
  68. *
  69. * @return array{'get'?:string,'name'?:string,'order'?:string,'search'?:string,'state'?:int,'url'?:string}
  70. */
  71. public function toArray(): array {
  72. return array_filter([
  73. 'get' => $this->get,
  74. 'name' => $this->name,
  75. 'order' => $this->order,
  76. 'search' => $this->search->__toString(),
  77. 'state' => $this->state,
  78. 'url' => $this->url,
  79. ]);
  80. }
  81. /**
  82. * Parse the get parameter in the query string to extract its name and type
  83. */
  84. private function parseGet(string $get): void {
  85. $this->get = $get;
  86. if (preg_match('/(?P<type>[acfst])(_(?P<id>\d+))?/', $get, $matches)) {
  87. $id = intval($matches['id'] ?? '0');
  88. switch ($matches['type']) {
  89. case 'a':
  90. $this->parseAll();
  91. break;
  92. case 'c':
  93. $this->parseCategory($id);
  94. break;
  95. case 'f':
  96. $this->parseFeed($id);
  97. break;
  98. case 's':
  99. $this->parseFavorite();
  100. break;
  101. case 't':
  102. $this->parseTag($id);
  103. break;
  104. }
  105. }
  106. }
  107. /**
  108. * Parse the query string when it is an "all" query
  109. */
  110. private function parseAll(): void {
  111. $this->get_name = 'all';
  112. $this->get_type = 'all';
  113. }
  114. /**
  115. * Parse the query string when it is a "category" query
  116. *
  117. * @throws FreshRSS_DAO_Exception
  118. */
  119. private function parseCategory(int $id): void {
  120. if ($this->category_dao === null) {
  121. $this->category_dao = FreshRSS_Factory::createCategoryDao();
  122. }
  123. $category = $this->category_dao->searchById($id);
  124. if ($category !== null) {
  125. $this->get_name = $category->name();
  126. } else {
  127. $this->deprecated = true;
  128. }
  129. $this->get_type = 'category';
  130. }
  131. /**
  132. * Parse the query string when it is a "feed" query
  133. *
  134. * @throws FreshRSS_DAO_Exception
  135. */
  136. private function parseFeed(int $id): void {
  137. if ($this->feed_dao === null) {
  138. $this->feed_dao = FreshRSS_Factory::createFeedDao();
  139. }
  140. $feed = $this->feed_dao->searchById($id);
  141. if ($feed !== null) {
  142. $this->get_name = $feed->name();
  143. } else {
  144. $this->deprecated = true;
  145. }
  146. $this->get_type = 'feed';
  147. }
  148. /**
  149. * Parse the query string when it is a "tag" query
  150. *
  151. * @throws FreshRSS_DAO_Exception
  152. */
  153. private function parseTag(int $id): void {
  154. if ($this->tag_dao === null) {
  155. $this->tag_dao = FreshRSS_Factory::createTagDao();
  156. }
  157. $tag = $this->tag_dao->searchById($id);
  158. if ($tag !== null) {
  159. $this->get_name = $tag->name();
  160. } else {
  161. $this->deprecated = true;
  162. }
  163. $this->get_type = 'tag';
  164. }
  165. /**
  166. * Parse the query string when it is a "favorite" query
  167. */
  168. private function parseFavorite(): void {
  169. $this->get_name = 'favorite';
  170. $this->get_type = 'favorite';
  171. }
  172. /**
  173. * Check if the current user query is deprecated.
  174. * It is deprecated if the category or the feed used in the query are
  175. * not existing.
  176. */
  177. public function isDeprecated(): bool {
  178. return $this->deprecated;
  179. }
  180. /**
  181. * Check if the user query has parameters.
  182. * If the type is 'all', it is considered equal to no parameters
  183. */
  184. public function hasParameters(): bool {
  185. if ($this->get_type === 'all') {
  186. return false;
  187. }
  188. if ($this->hasSearch()) {
  189. return true;
  190. }
  191. if ($this->state) {
  192. return true;
  193. }
  194. if ($this->order) {
  195. return true;
  196. }
  197. if ($this->get) {
  198. return true;
  199. }
  200. return false;
  201. }
  202. /**
  203. * Check if there is a search in the search object
  204. */
  205. public function hasSearch(): bool {
  206. return $this->search->getRawInput() !== '';
  207. }
  208. public function getGet(): string {
  209. return $this->get;
  210. }
  211. public function getGetName(): string {
  212. return $this->get_name;
  213. }
  214. public function getGetType(): string {
  215. return $this->get_type;
  216. }
  217. public function getName(): string {
  218. return $this->name;
  219. }
  220. public function getOrder(): string {
  221. return $this->order;
  222. }
  223. public function getSearch(): FreshRSS_BooleanSearch {
  224. return $this->search;
  225. }
  226. public function getState(): int {
  227. return $this->state;
  228. }
  229. public function getUrl(): string {
  230. return $this->url;
  231. }
  232. }