BooleanSearch.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Contains Boolean search from the search form.
  4. */
  5. class FreshRSS_BooleanSearch {
  6. /** @var string */
  7. private $raw_input = '';
  8. private $searches = array();
  9. public function __construct($input) {
  10. $input = trim($input);
  11. if ($input == '') {
  12. return;
  13. }
  14. $this->raw_input = $input;
  15. $input = preg_replace('/:&quot;(.*?)&quot;/', ':"\1"', $input);
  16. $input = preg_replace('/(?<=[\s!-]|^)&quot;(.*?)&quot;/', '"\1"', $input);
  17. $splits = preg_split('/\b(OR)\b/i', $input, -1, PREG_SPLIT_DELIM_CAPTURE);
  18. $segment = '';
  19. $ns = count($splits);
  20. for ($i = 0; $i < $ns; $i++) {
  21. $segment = $segment . $splits[$i];
  22. if (trim($segment) == '' || strcasecmp($segment, 'OR') === 0) {
  23. $segment = '';
  24. } else {
  25. $quotes = substr_count($segment, '"') + substr_count($segment, '&quot;');
  26. if ($quotes % 2 === 0) {
  27. $segment = trim($segment);
  28. if ($segment != '') {
  29. $this->searches[] = new FreshRSS_Search($segment);
  30. }
  31. $segment = '';
  32. }
  33. }
  34. }
  35. $segment = trim($segment);
  36. if ($segment != '') {
  37. $this->searches[] = new FreshRSS_Search($segment);
  38. }
  39. }
  40. public function searches() {
  41. return $this->searches;
  42. }
  43. public function add($search) {
  44. if ($search instanceof FreshRSS_Search) {
  45. $this->searches[] = $search;
  46. return $search;
  47. }
  48. return null;
  49. }
  50. public function __toString(): string {
  51. return $this->getRawInput();
  52. }
  53. public function getRawInput(): string {
  54. return $this->raw_input;
  55. }
  56. }