summaryrefslogtreecommitdiff
path: root/app/Models/BooleanSearch.php
blob: 88eeea73cbf126af9d23df22a9fab256bc562a4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

/**
 * Contains Boolean search from the search form.
 */
class FreshRSS_BooleanSearch {

	private $raw_input = '';
	private $searches = array();

	public function __construct($input) {
		$input = trim($input);
		if ($input == '') {
			return;
		}
		$this->raw_input = $input;

		$input = preg_replace('/:&quot;(.*?)&quot;/', ':"\1"', $input);
		$splits = preg_split('/\b(OR)\b/i', $input, -1, PREG_SPLIT_DELIM_CAPTURE);

		$segment = '';
		$ns = count($splits);
		for ($i = 0; $i < $ns; $i++) {
			$segment = $segment . $splits[$i];
			if (trim($segment) == '' || strcasecmp($segment, 'OR') === 0) {
				$segment = '';
			} else {
				$quotes = substr_count($segment, '"') + substr_count($segment, '&quot;');
				if ($quotes % 2 === 0) {
					$segment = trim($segment);
					if ($segment != '') {
						$this->searches[] = new FreshRSS_Search($segment);
					}
					$segment = '';
				}
			}
		}
		$segment = trim($segment);
		if ($segment != '') {
			$this->searches[] = new FreshRSS_Search($segment);
		}
	}

	public function searches() {
		return $this->searches;
	}

	public function add($search) {
		if ($search instanceof FreshRSS_Search) {
			$this->searches[] = $search;
			return $search;
		}
		return null;
	}

	public function __toString() {
		return $this->getRawInput();
	}

	public function getRawInput() {
		return $this->raw_input;
	}
}