UserQuery.php 5.4 KB

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