From f30ded2f863ed4a33ae8194d6cf00eaa877c9b6a Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Wed, 11 Feb 2015 20:41:00 -0500 Subject: Extract the search code from the context I figured that the code for the search could be extracted from the context to have separation of concern. It supports multiple keywords. It suports also multiple tag keywords. --- app/Models/Search.php | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 app/Models/Search.php (limited to 'app/Models/Search.php') diff --git a/app/Models/Search.php b/app/Models/Search.php new file mode 100644 index 000000000..0475bc685 --- /dev/null +++ b/app/Models/Search.php @@ -0,0 +1,189 @@ +raw_input = $input; + $input = $this->parseIntitleSearch($input); + $input = $this->parseAuthorSearch($input); + $input = $this->parseInurlSearch($input); + $input = $this->parsePubdateSearch($input); + $input = $this->parseDateSearch($input); + $input = $this->parseTagsSeach($input); + $this->search = $input; + } + + public function getRawInput() { + return $this->raw_input; + } + + public function getIntitle() { + return $this->intitle; + } + + public function getMinDate() { + return $this->min_date; + } + + public function getMaxDate() { + return $this->max_date; + } + + public function getMinPubdate() { + return $this->min_pubdate; + } + + public function getMaxPubdate() { + return $this->max_pubdate; + } + + public function getInurl() { + return $this->inurl; + } + + public function getAuthor() { + return $this->author; + } + + public function getTags() { + return $this->tags; + } + + public function getSearch() { + return $this->search; + } + + /** + * Parse the search string to find intitle keyword and the search related + * to it. + * The search is the first word following the keyword. + * + * @param string $input + * @return string + */ + private function parseIntitleSearch($input) { + if (preg_match('/intitle:(?P[\'"])(?P.*)(?P=delim)/U', $input, $matches)) { + $this->intitle = $matches['search']; + $input = str_replace($matches[0], '', $input); + } else if (preg_match('/intitle:(?P\w*)/', $input, $matches)) { + $this->intitle = $matches['search']; + $input = str_replace($matches[0], '', $input); + } + return $this->cleanSearch($input); + } + + /** + * Parse the search string to find author keyword and the search related + * to it. + * The search is the first word following the keyword except when using + * a delimiter. Supported delimiters are single quote (') and double + * quotes ("). + * + * @param string $input + * @return string + */ + private function parseAuthorSearch($input) { + if (preg_match('/author:(?P[\'"])(?P.*)(?P=delim)/U', $input, $matches)) { + $this->author = $matches['search']; + $input = str_replace($matches[0], '', $input); + } else if (preg_match('/author:(?P\w*)/', $input, $matches)) { + $this->author = $matches['search']; + $input = str_replace($matches[0], '', $input); + } + return $this->cleanSearch($input); + } + + /** + * Parse the search string to find inurl keyword and the search related + * to it. + * The search is the first word following the keyword except. + * + * @param string $input + * @return string + */ + private function parseInurlSearch($input) { + if (preg_match('/inurl:(?P[^\s]*)/', $input, $matches)) { + $this->inurl = $matches['search']; + $input = str_replace($matches[0], '', $input); + } + return $this->cleanSearch($input); + } + + /** + * Parse the search string to find date keyword and the search related + * to it. + * The search is the first word following the keyword. + * + * @param string $input + * @return string + */ + private function parseDateSearch($input) { + if (preg_match('/date:(?P[^\s]*)/', $input, $matches)) { + list($this->min_date, $this->max_date) = parseDateInterval($matches['search']); + $input = str_replace($matches[0], '', $input); + } + return $this->cleanSearch($input); + } + + /** + * Parse the search string to find pubdate keyword and the search related + * to it. + * The search is the first word following the keyword. + * + * @param string $input + * @return string + */ + private function parsePubdateSearch($input) { + if (preg_match('/pubdate:(?P[^\s]*)/', $input, $matches)) { + list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($matches['search']); + $input = str_replace($matches[0], '', $input); + } + return $this->cleanSearch($input); + } + + /** + * Parse the search string to find tags keyword (# followed by a word) + * and the search related to it. + * The search is the first word following the #. + * + * @param string $input + * @return string + */ + private function parseTagsSeach($input) { + if (preg_match_all('/#(?P[^\s]+)/', $input, $matches)) { + $this->tags = $matches['search']; + $input = str_replace($matches[0], '', $input); + } + return $this->cleanSearch($input); + } + + private function cleanSearch($input) { + $input = preg_replace('/\s+/', ' ', $input); + return trim($input); + } + +} -- cgit v1.2.3 From 9cee5c1a17947d3f3d10554844b7089e28cf8500 Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Wed, 11 Feb 2015 21:52:10 -0500 Subject: Remove code generated by netbeans --- app/Models/Search.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'app/Models/Search.php') diff --git a/app/Models/Search.php b/app/Models/Search.php index 0475bc685..e64f6cb88 100644 --- a/app/Models/Search.php +++ b/app/Models/Search.php @@ -5,8 +5,6 @@ * * It allows to extract meaningful bits of the search and store them in a * convenient object - * - * @author alexis */ class FreshRSS_Search { -- cgit v1.2.3 From 9f83aa5fe79618be98cb027bb1070f5a11c51723 Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Thu, 12 Feb 2015 21:05:33 -0500 Subject: Refactor the code to make less unnecessaty calls There were multiple calls made to the cleaning method that were unnecessary since it is useful only on the last call. It allows to simplify code by returning values ealier. --- app/Models/Search.php | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'app/Models/Search.php') diff --git a/app/Models/Search.php b/app/Models/Search.php index e64f6cb88..ef8fc883d 100644 --- a/app/Models/Search.php +++ b/app/Models/Search.php @@ -32,7 +32,7 @@ class FreshRSS_Search { $input = $this->parsePubdateSearch($input); $input = $this->parseDateSearch($input); $input = $this->parseTagsSeach($input); - $this->search = $input; + $this->search = $this->cleanSearch($input); } public function getRawInput() { @@ -86,12 +86,13 @@ class FreshRSS_Search { private function parseIntitleSearch($input) { if (preg_match('/intitle:(?P[\'"])(?P.*)(?P=delim)/U', $input, $matches)) { $this->intitle = $matches['search']; - $input = str_replace($matches[0], '', $input); - } else if (preg_match('/intitle:(?P\w*)/', $input, $matches)) { + return str_replace($matches[0], '', $input); + } + if (preg_match('/intitle:(?P\w*)/', $input, $matches)) { $this->intitle = $matches['search']; - $input = str_replace($matches[0], '', $input); + return str_replace($matches[0], '', $input); } - return $this->cleanSearch($input); + return $input; } /** @@ -107,12 +108,13 @@ class FreshRSS_Search { private function parseAuthorSearch($input) { if (preg_match('/author:(?P[\'"])(?P.*)(?P=delim)/U', $input, $matches)) { $this->author = $matches['search']; - $input = str_replace($matches[0], '', $input); - } else if (preg_match('/author:(?P\w*)/', $input, $matches)) { + return str_replace($matches[0], '', $input); + } + if (preg_match('/author:(?P\w*)/', $input, $matches)) { $this->author = $matches['search']; - $input = str_replace($matches[0], '', $input); + return str_replace($matches[0], '', $input); } - return $this->cleanSearch($input); + return $input; } /** @@ -126,9 +128,9 @@ class FreshRSS_Search { private function parseInurlSearch($input) { if (preg_match('/inurl:(?P[^\s]*)/', $input, $matches)) { $this->inurl = $matches['search']; - $input = str_replace($matches[0], '', $input); + return str_replace($matches[0], '', $input); } - return $this->cleanSearch($input); + return $input; } /** @@ -142,9 +144,9 @@ class FreshRSS_Search { private function parseDateSearch($input) { if (preg_match('/date:(?P[^\s]*)/', $input, $matches)) { list($this->min_date, $this->max_date) = parseDateInterval($matches['search']); - $input = str_replace($matches[0], '', $input); + return str_replace($matches[0], '', $input); } - return $this->cleanSearch($input); + return $input; } /** @@ -158,9 +160,9 @@ class FreshRSS_Search { private function parsePubdateSearch($input) { if (preg_match('/pubdate:(?P[^\s]*)/', $input, $matches)) { list($this->min_pubdate, $this->max_pubdate) = parseDateInterval($matches['search']); - $input = str_replace($matches[0], '', $input); + return str_replace($matches[0], '', $input); } - return $this->cleanSearch($input); + return $input; } /** @@ -174,11 +176,17 @@ class FreshRSS_Search { private function parseTagsSeach($input) { if (preg_match_all('/#(?P[^\s]+)/', $input, $matches)) { $this->tags = $matches['search']; - $input = str_replace($matches[0], '', $input); + return str_replace($matches[0], '', $input); } - return $this->cleanSearch($input); + return $input; } + /** + * Remove all unnecessary spaces in the search + * + * @param string $input + * @return string + */ private function cleanSearch($input) { $input = preg_replace('/\s+/', ' ', $input); return trim($input); -- cgit v1.2.3