Search.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. <?php
  2. declare(strict_types=1);
  3. require_once(LIB_PATH . '/lib_date.php');
  4. /**
  5. * Contains a search from the search form.
  6. *
  7. * It allows to extract meaningful bits of the search and store them in a
  8. * convenient object
  9. */
  10. class FreshRSS_Search {
  11. /**
  12. * This contains the user input string
  13. */
  14. private string $raw_input = '';
  15. // The following properties are extracted from the raw input
  16. /** @var array<string>|null */
  17. private ?array $entry_ids = null;
  18. /** @var array<int>|null */
  19. private ?array $feed_ids = null;
  20. /** @var array<int>|'*'|null */
  21. private $label_ids = null;
  22. /** @var array<string>|null */
  23. private ?array $label_names = null;
  24. /** @var array<string>|null */
  25. private ?array $intitle = null;
  26. /** @var int|false|null */
  27. private $min_date = null;
  28. /** @var int|false|null */
  29. private $max_date = null;
  30. /** @var int|false|null */
  31. private $min_pubdate = null;
  32. /** @var int|false|null */
  33. private $max_pubdate = null;
  34. /** @var array<string>|null */
  35. private ?array $inurl = null;
  36. /** @var array<string>|null */
  37. private ?array $author = null;
  38. /** @var array<string>|null */
  39. private ?array $tags = null;
  40. /** @var array<string>|null */
  41. private ?array $search = null;
  42. /** @var array<string>|null */
  43. private ?array $not_entry_ids = null;
  44. /** @var array<int>|null */
  45. private ?array $not_feed_ids = null;
  46. /** @var array<int>|'*'|null */
  47. private $not_label_ids = null;
  48. /** @var array<string>|null */
  49. private ?array $not_label_names = null;
  50. /** @var array<string>|null */
  51. private ?array $not_intitle = null;
  52. /** @var int|false|null */
  53. private $not_min_date = null;
  54. /** @var int|false|null */
  55. private $not_max_date = null;
  56. /** @var int|false|null */
  57. private $not_min_pubdate = null;
  58. /** @var int|false|null */
  59. private $not_max_pubdate = null;
  60. /** @var array<string>|null */
  61. private ?array $not_inurl = null;
  62. /** @var array<string>|null */
  63. private ?array $not_author = null;
  64. /** @var array<string>|null */
  65. private ?array $not_tags = null;
  66. /** @var array<string>|null */
  67. private ?array $not_search = null;
  68. public function __construct(string $input) {
  69. $input = self::cleanSearch($input);
  70. $input = self::unescape($input);
  71. $this->raw_input = $input;
  72. $input = $this->parseNotEntryIds($input);
  73. $input = $this->parseNotFeedIds($input);
  74. $input = $this->parseNotLabelIds($input);
  75. $input = $this->parseNotLabelNames($input);
  76. $input = $this->parseNotPubdateSearch($input);
  77. $input = $this->parseNotDateSearch($input);
  78. $input = $this->parseNotIntitleSearch($input);
  79. $input = $this->parseNotAuthorSearch($input);
  80. $input = $this->parseNotInurlSearch($input);
  81. $input = $this->parseNotTagsSearch($input);
  82. $input = $this->parseEntryIds($input);
  83. $input = $this->parseFeedIds($input);
  84. $input = $this->parseLabelIds($input);
  85. $input = $this->parseLabelNames($input);
  86. $input = $this->parsePubdateSearch($input);
  87. $input = $this->parseDateSearch($input);
  88. $input = $this->parseIntitleSearch($input);
  89. $input = $this->parseAuthorSearch($input);
  90. $input = $this->parseInurlSearch($input);
  91. $input = $this->parseTagsSearch($input);
  92. $input = $this->parseQuotedSearch($input);
  93. $input = $this->parseNotSearch($input);
  94. $this->parseSearch($input);
  95. }
  96. #[\Override]
  97. public function __toString(): string {
  98. return $this->getRawInput();
  99. }
  100. public function getRawInput(): string {
  101. return $this->raw_input;
  102. }
  103. /** @return array<string>|null */
  104. public function getEntryIds(): ?array {
  105. return $this->entry_ids;
  106. }
  107. /** @return array<string>|null */
  108. public function getNotEntryIds(): ?array {
  109. return $this->not_entry_ids;
  110. }
  111. /** @return array<int>|null */
  112. public function getFeedIds(): ?array {
  113. return $this->feed_ids;
  114. }
  115. /** @return array<int>|null */
  116. public function getNotFeedIds(): ?array {
  117. return $this->not_feed_ids;
  118. }
  119. /** @return array<int>|'*'|null */
  120. public function getLabelIds() {
  121. return $this->label_ids;
  122. }
  123. /** @return array<int>|'*'|null */
  124. public function getNotLabelIds() {
  125. return $this->not_label_ids;
  126. }
  127. /** @return array<string>|null */
  128. public function getLabelNames(): ?array {
  129. return $this->label_names;
  130. }
  131. /** @return array<string>|null */
  132. public function getNotLabelNames(): ?array {
  133. return $this->not_label_names;
  134. }
  135. /** @return array<string>|null */
  136. public function getIntitle(): ?array {
  137. return $this->intitle;
  138. }
  139. /** @return array<string>|null */
  140. public function getNotIntitle(): ?array {
  141. return $this->not_intitle;
  142. }
  143. public function getMinDate(): ?int {
  144. return $this->min_date ?: null;
  145. }
  146. public function getNotMinDate(): ?int {
  147. return $this->not_min_date ?: null;
  148. }
  149. public function setMinDate(int $value): void {
  150. $this->min_date = $value;
  151. }
  152. public function getMaxDate(): ?int {
  153. return $this->max_date ?: null;
  154. }
  155. public function getNotMaxDate(): ?int {
  156. return $this->not_max_date ?: null;
  157. }
  158. public function setMaxDate(int $value): void {
  159. $this->max_date = $value;
  160. }
  161. public function getMinPubdate(): ?int {
  162. return $this->min_pubdate ?: null;
  163. }
  164. public function getNotMinPubdate(): ?int {
  165. return $this->not_min_pubdate ?: null;
  166. }
  167. public function getMaxPubdate(): ?int {
  168. return $this->max_pubdate ?: null;
  169. }
  170. public function getNotMaxPubdate(): ?int {
  171. return $this->not_max_pubdate ?: null;
  172. }
  173. /** @return array<string>|null */
  174. public function getInurl(): ?array {
  175. return $this->inurl;
  176. }
  177. /** @return array<string>|null */
  178. public function getNotInurl(): ?array {
  179. return $this->not_inurl;
  180. }
  181. /** @return array<string>|null */
  182. public function getAuthor(): ?array {
  183. return $this->author;
  184. }
  185. /** @return array<string>|null */
  186. public function getNotAuthor(): ?array {
  187. return $this->not_author;
  188. }
  189. /** @return array<string>|null */
  190. public function getTags(): ?array {
  191. return $this->tags;
  192. }
  193. /** @return array<string>|null */
  194. public function getNotTags(): ?array {
  195. return $this->not_tags;
  196. }
  197. /** @return array<string>|null */
  198. public function getSearch(): ?array {
  199. return $this->search;
  200. }
  201. /** @return array<string>|null */
  202. public function getNotSearch(): ?array {
  203. return $this->not_search;
  204. }
  205. /**
  206. * @param array<string>|null $anArray
  207. * @return array<string>
  208. */
  209. private static function removeEmptyValues(?array $anArray): array {
  210. return empty($anArray) ? [] : array_filter($anArray, static function(string $value) {
  211. return $value !== '';
  212. });
  213. }
  214. /**
  215. * @param array<string>|string $value
  216. * @return ($value is array ? array<string> : string)
  217. */
  218. private static function decodeSpaces($value) {
  219. if (is_array($value)) {
  220. for ($i = count($value) - 1; $i >= 0; $i--) {
  221. $value[$i] = self::decodeSpaces($value[$i]);
  222. }
  223. } else {
  224. $value = trim(str_replace('+', ' ', $value));
  225. }
  226. return $value;
  227. }
  228. /**
  229. * Parse the search string to find entry (article) IDs.
  230. */
  231. private function parseEntryIds(string $input): string {
  232. if (preg_match_all('/\be:(?P<search>[0-9,]*)/', $input, $matches)) {
  233. $input = str_replace($matches[0], '', $input);
  234. $ids_lists = $matches['search'];
  235. $this->entry_ids = [];
  236. foreach ($ids_lists as $ids_list) {
  237. $entry_ids = explode(',', $ids_list);
  238. $entry_ids = self::removeEmptyValues($entry_ids);
  239. if (!empty($entry_ids)) {
  240. $this->entry_ids = array_merge($this->entry_ids, $entry_ids);
  241. }
  242. }
  243. }
  244. return $input;
  245. }
  246. private function parseNotEntryIds(string $input): string {
  247. if (preg_match_all('/(?<=\s|^)[!-]e:(?P<search>[0-9,]*)/', $input, $matches)) {
  248. $input = str_replace($matches[0], '', $input);
  249. $ids_lists = $matches['search'];
  250. $this->not_entry_ids = [];
  251. foreach ($ids_lists as $ids_list) {
  252. $entry_ids = explode(',', $ids_list);
  253. $entry_ids = self::removeEmptyValues($entry_ids);
  254. if (!empty($entry_ids)) {
  255. $this->not_entry_ids = array_merge($this->not_entry_ids, $entry_ids);
  256. }
  257. }
  258. }
  259. return $input;
  260. }
  261. private function parseFeedIds(string $input): string {
  262. if (preg_match_all('/\bf:(?P<search>[0-9,]*)/', $input, $matches)) {
  263. $input = str_replace($matches[0], '', $input);
  264. $ids_lists = $matches['search'];
  265. $this->feed_ids = [];
  266. foreach ($ids_lists as $ids_list) {
  267. $feed_ids = explode(',', $ids_list);
  268. $feed_ids = self::removeEmptyValues($feed_ids);
  269. /** @var array<int> $feed_ids */
  270. $feed_ids = array_map('intval', $feed_ids);
  271. if (!empty($feed_ids)) {
  272. $this->feed_ids = array_merge($this->feed_ids, $feed_ids);
  273. }
  274. }
  275. }
  276. return $input;
  277. }
  278. private function parseNotFeedIds(string $input): string {
  279. if (preg_match_all('/(?<=\s|^)[!-]f:(?P<search>[0-9,]*)/', $input, $matches)) {
  280. $input = str_replace($matches[0], '', $input);
  281. $ids_lists = $matches['search'];
  282. $this->not_feed_ids = [];
  283. foreach ($ids_lists as $ids_list) {
  284. $feed_ids = explode(',', $ids_list);
  285. $feed_ids = self::removeEmptyValues($feed_ids);
  286. /** @var array<int> $feed_ids */
  287. $feed_ids = array_map('intval', $feed_ids);
  288. if (!empty($feed_ids)) {
  289. $this->not_feed_ids = array_merge($this->not_feed_ids, $feed_ids);
  290. }
  291. }
  292. }
  293. return $input;
  294. }
  295. /**
  296. * Parse the search string to find tags (labels) IDs.
  297. */
  298. private function parseLabelIds(string $input): string {
  299. if (preg_match_all('/\b[lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
  300. $input = str_replace($matches[0], '', $input);
  301. $ids_lists = $matches['search'];
  302. $this->label_ids = [];
  303. foreach ($ids_lists as $ids_list) {
  304. if ($ids_list === '*') {
  305. $this->label_ids = '*';
  306. break;
  307. }
  308. $label_ids = explode(',', $ids_list);
  309. $label_ids = self::removeEmptyValues($label_ids);
  310. /** @var array<int> $label_ids */
  311. $label_ids = array_map('intval', $label_ids);
  312. if (!empty($label_ids)) {
  313. $this->label_ids = array_merge($this->label_ids, $label_ids);
  314. }
  315. }
  316. }
  317. return $input;
  318. }
  319. private function parseNotLabelIds(string $input): string {
  320. if (preg_match_all('/(?<=\s|^)[!-][lL]:(?P<search>[0-9,]+|[*])/', $input, $matches)) {
  321. $input = str_replace($matches[0], '', $input);
  322. $ids_lists = $matches['search'];
  323. $this->not_label_ids = [];
  324. foreach ($ids_lists as $ids_list) {
  325. if ($ids_list === '*') {
  326. $this->not_label_ids = '*';
  327. break;
  328. }
  329. $label_ids = explode(',', $ids_list);
  330. $label_ids = self::removeEmptyValues($label_ids);
  331. /** @var array<int> $label_ids */
  332. $label_ids = array_map('intval', $label_ids);
  333. if (!empty($label_ids)) {
  334. $this->not_label_ids = array_merge($this->not_label_ids, $label_ids);
  335. }
  336. }
  337. }
  338. return $input;
  339. }
  340. /**
  341. * Parse the search string to find tags (labels) names.
  342. */
  343. private function parseLabelNames(string $input): string {
  344. $names_lists = [];
  345. if (preg_match_all('/\blabels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  346. $names_lists = $matches['search'];
  347. $input = str_replace($matches[0], '', $input);
  348. }
  349. if (preg_match_all('/\blabels?:(?P<search>[^\s"]*)/', $input, $matches)) {
  350. $names_lists = array_merge($names_lists, $matches['search']);
  351. $input = str_replace($matches[0], '', $input);
  352. }
  353. if (!empty($names_lists)) {
  354. $this->label_names = [];
  355. foreach ($names_lists as $names_list) {
  356. $names_array = explode(',', $names_list);
  357. $names_array = self::removeEmptyValues($names_array);
  358. if (!empty($names_array)) {
  359. $this->label_names = array_merge($this->label_names, $names_array);
  360. }
  361. }
  362. }
  363. return $input;
  364. }
  365. /**
  366. * Parse the search string to find tags (labels) names to exclude.
  367. */
  368. private function parseNotLabelNames(string $input): string {
  369. $names_lists = [];
  370. if (preg_match_all('/(?<=\s|^)[!-]labels?:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  371. $names_lists = $matches['search'];
  372. $input = str_replace($matches[0], '', $input);
  373. }
  374. if (preg_match_all('/(?<=\s|^)[!-]labels?:(?P<search>[^\s"]*)/', $input, $matches)) {
  375. $names_lists = array_merge($names_lists, $matches['search']);
  376. $input = str_replace($matches[0], '', $input);
  377. }
  378. if (!empty($names_lists)) {
  379. $this->not_label_names = [];
  380. foreach ($names_lists as $names_list) {
  381. $names_array = explode(',', $names_list);
  382. $names_array = self::removeEmptyValues($names_array);
  383. if (!empty($names_array)) {
  384. $this->not_label_names = array_merge($this->not_label_names, $names_array);
  385. }
  386. }
  387. }
  388. return $input;
  389. }
  390. /**
  391. * Parse the search string to find intitle keyword and the search related to it.
  392. * The search is the first word following the keyword.
  393. */
  394. private function parseIntitleSearch(string $input): string {
  395. if (preg_match_all('/\bintitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  396. $this->intitle = $matches['search'];
  397. $input = str_replace($matches[0], '', $input);
  398. }
  399. if (preg_match_all('/\bintitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  400. $this->intitle = array_merge($this->intitle ?: [], $matches['search']);
  401. $input = str_replace($matches[0], '', $input);
  402. }
  403. $this->intitle = self::removeEmptyValues($this->intitle);
  404. if (empty($this->intitle)) {
  405. $this->intitle = null;
  406. }
  407. return $input;
  408. }
  409. private function parseNotIntitleSearch(string $input): string {
  410. if (preg_match_all('/(?<=\s|^)[!-]intitle:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  411. $this->not_intitle = $matches['search'];
  412. $input = str_replace($matches[0], '', $input);
  413. }
  414. if (preg_match_all('/(?<=\s|^)[!-]intitle:(?P<search>[^\s"]*)/', $input, $matches)) {
  415. $this->not_intitle = array_merge($this->not_intitle ?: [], $matches['search']);
  416. $input = str_replace($matches[0], '', $input);
  417. }
  418. $this->not_intitle = self::removeEmptyValues($this->not_intitle);
  419. if (empty($this->not_intitle)) {
  420. $this->not_intitle = null;
  421. }
  422. return $input;
  423. }
  424. /**
  425. * Parse the search string to find author keyword and the search related to it.
  426. * The search is the first word following the keyword except when using
  427. * a delimiter. Supported delimiters are single quote (') and double quotes (").
  428. */
  429. private function parseAuthorSearch(string $input): string {
  430. if (preg_match_all('/\bauthor:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  431. $this->author = $matches['search'];
  432. $input = str_replace($matches[0], '', $input);
  433. }
  434. if (preg_match_all('/\bauthor:(?P<search>[^\s"]*)/', $input, $matches)) {
  435. $this->author = array_merge($this->author ?: [], $matches['search']);
  436. $input = str_replace($matches[0], '', $input);
  437. }
  438. $this->author = self::removeEmptyValues($this->author);
  439. if (empty($this->author)) {
  440. $this->author = null;
  441. }
  442. return $input;
  443. }
  444. private function parseNotAuthorSearch(string $input): string {
  445. if (preg_match_all('/(?<=\s|^)[!-]author:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  446. $this->not_author = $matches['search'];
  447. $input = str_replace($matches[0], '', $input);
  448. }
  449. if (preg_match_all('/(?<=\s|^)[!-]author:(?P<search>[^\s"]*)/', $input, $matches)) {
  450. $this->not_author = array_merge($this->not_author ?: [], $matches['search']);
  451. $input = str_replace($matches[0], '', $input);
  452. }
  453. $this->not_author = self::removeEmptyValues($this->not_author);
  454. if (empty($this->not_author)) {
  455. $this->not_author = null;
  456. }
  457. return $input;
  458. }
  459. /**
  460. * Parse the search string to find inurl keyword and the search related to it.
  461. * The search is the first word following the keyword.
  462. */
  463. private function parseInurlSearch(string $input): string {
  464. if (preg_match_all('/\binurl:(?P<search>[^\s]*)/', $input, $matches)) {
  465. $this->inurl = $matches['search'];
  466. $input = str_replace($matches[0], '', $input);
  467. $this->inurl = self::removeEmptyValues($this->inurl);
  468. }
  469. return $input;
  470. }
  471. private function parseNotInurlSearch(string $input): string {
  472. if (preg_match_all('/(?<=\s|^)[!-]inurl:(?P<search>[^\s]*)/', $input, $matches)) {
  473. $this->not_inurl = $matches['search'];
  474. $input = str_replace($matches[0], '', $input);
  475. $this->not_inurl = self::removeEmptyValues($this->not_inurl);
  476. }
  477. return $input;
  478. }
  479. /**
  480. * Parse the search string to find date keyword and the search related to it.
  481. * The search is the first word following the keyword.
  482. */
  483. private function parseDateSearch(string $input): string {
  484. if (preg_match_all('/\bdate:(?P<search>[^\s]*)/', $input, $matches)) {
  485. $input = str_replace($matches[0], '', $input);
  486. $dates = self::removeEmptyValues($matches['search']);
  487. if (!empty($dates[0])) {
  488. [$this->min_date, $this->max_date] = parseDateInterval($dates[0]);
  489. }
  490. }
  491. return $input;
  492. }
  493. private function parseNotDateSearch(string $input): string {
  494. if (preg_match_all('/(?<=\s|^)[!-]date:(?P<search>[^\s]*)/', $input, $matches)) {
  495. $input = str_replace($matches[0], '', $input);
  496. $dates = self::removeEmptyValues($matches['search']);
  497. if (!empty($dates[0])) {
  498. [$this->not_min_date, $this->not_max_date] = parseDateInterval($dates[0]);
  499. }
  500. }
  501. return $input;
  502. }
  503. /**
  504. * Parse the search string to find pubdate keyword and the search related to it.
  505. * The search is the first word following the keyword.
  506. */
  507. private function parsePubdateSearch(string $input): string {
  508. if (preg_match_all('/\bpubdate:(?P<search>[^\s]*)/', $input, $matches)) {
  509. $input = str_replace($matches[0], '', $input);
  510. $dates = self::removeEmptyValues($matches['search']);
  511. if (!empty($dates[0])) {
  512. [$this->min_pubdate, $this->max_pubdate] = parseDateInterval($dates[0]);
  513. }
  514. }
  515. return $input;
  516. }
  517. private function parseNotPubdateSearch(string $input): string {
  518. if (preg_match_all('/(?<=\s|^)[!-]pubdate:(?P<search>[^\s]*)/', $input, $matches)) {
  519. $input = str_replace($matches[0], '', $input);
  520. $dates = self::removeEmptyValues($matches['search']);
  521. if (!empty($dates[0])) {
  522. [$this->not_min_pubdate, $this->not_max_pubdate] = parseDateInterval($dates[0]);
  523. }
  524. }
  525. return $input;
  526. }
  527. /**
  528. * Parse the search string to find tags keyword (# followed by a word)
  529. * and the search related to it.
  530. * The search is the first word following the #.
  531. */
  532. private function parseTagsSearch(string $input): string {
  533. if (preg_match_all('/#(?P<search>[^\s]+)/', $input, $matches)) {
  534. $this->tags = $matches['search'];
  535. $input = str_replace($matches[0], '', $input);
  536. $this->tags = self::removeEmptyValues($this->tags);
  537. $this->tags = self::decodeSpaces($this->tags);
  538. }
  539. return $input;
  540. }
  541. private function parseNotTagsSearch(string $input): string {
  542. if (preg_match_all('/(?<=\s|^)[!-]#(?P<search>[^\s]+)/', $input, $matches)) {
  543. $this->not_tags = $matches['search'];
  544. $input = str_replace($matches[0], '', $input);
  545. $this->not_tags = self::removeEmptyValues($this->not_tags);
  546. $this->not_tags = self::decodeSpaces($this->not_tags);
  547. }
  548. return $input;
  549. }
  550. /**
  551. * Parse the search string to find search values.
  552. * Every word is a distinct search value using a delimiter.
  553. * Supported delimiters are single quote (') and double quotes (").
  554. */
  555. private function parseQuotedSearch(string $input): string {
  556. $input = self::cleanSearch($input);
  557. if ($input === '') {
  558. return '';
  559. }
  560. if (preg_match_all('/(?<![!-])(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  561. $this->search = $matches['search'];
  562. //TODO: Replace all those str_replace with PREG_OFFSET_CAPTURE
  563. $input = str_replace($matches[0], '', $input);
  564. }
  565. return $input;
  566. }
  567. /**
  568. * Parse the search string to find search values.
  569. * Every word is a distinct search value.
  570. */
  571. private function parseSearch(string $input): string {
  572. $input = self::cleanSearch($input);
  573. if ($input === '') {
  574. return '';
  575. }
  576. if (is_array($this->search)) {
  577. $this->search = array_merge($this->search, explode(' ', $input));
  578. } else {
  579. $this->search = explode(' ', $input);
  580. }
  581. return $input;
  582. }
  583. private function parseNotSearch(string $input): string {
  584. $input = self::cleanSearch($input);
  585. if ($input === '') {
  586. return '';
  587. }
  588. if (preg_match_all('/(?<=\s|^)[!-](?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matches)) {
  589. $this->not_search = $matches['search'];
  590. $input = str_replace($matches[0], '', $input);
  591. }
  592. $input = self::cleanSearch($input);
  593. if ($input === '') {
  594. return '';
  595. }
  596. if (preg_match_all('/(?<=\s|^)[!-](?P<search>[^\s]+)/', $input, $matches)) {
  597. $this->not_search = array_merge(is_array($this->not_search) ? $this->not_search : [], $matches['search']);
  598. $input = str_replace($matches[0], '', $input);
  599. }
  600. $this->not_search = self::removeEmptyValues($this->not_search);
  601. return $input;
  602. }
  603. /**
  604. * Remove all unnecessary spaces in the search
  605. */
  606. private static function cleanSearch(string $input): string {
  607. $input = preg_replace('/\s+/', ' ', $input);
  608. if (!is_string($input)) {
  609. return '';
  610. }
  611. return trim($input);
  612. }
  613. /** Remove escaping backslashes for parenthesis logic */
  614. private static function unescape(string $input): string {
  615. return str_replace(['\\(', '\\)'], ['(', ')'], $input);
  616. }
  617. }