Search.php 20 KB

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