forked from mozfr/transvision
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issues mozfr#320: move all our regex logic into a unit tested class
- Loading branch information
1 parent
1e20764
commit 53e1654
Showing
10 changed files
with
385 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php | ||
namespace Transvision; | ||
|
||
/** | ||
* Search class | ||
* | ||
* Allows searching for data in our repositories using a fluent interface. | ||
* Currently, only the regex part (definition of the search) is implemented. | ||
* ex: | ||
* $search = (new Search) | ||
* ->setSearchTerms('Bookmark this page') | ||
* ->setRegexWholeWords(true) | ||
* ->setRegexCase('sensitive') | ||
* ->setRegexPerfectMatch(false); | ||
*/ | ||
class Search | ||
{ | ||
/** | ||
* The trimmed string searched, we keep that one as the canonical reference | ||
* @var string | ||
*/ | ||
public $search_terms; | ||
|
||
/** | ||
* The generated regex string updated dynamically via updateRegex() | ||
* @var string | ||
*/ | ||
public $regex; | ||
|
||
/** | ||
* Case sensibility of the regex | ||
* @var string | ||
*/ | ||
public $regex_case; | ||
|
||
/** | ||
* Consider the space separated string as a single word for search | ||
* @var string | ||
*/ | ||
public $regex_whole_words; | ||
|
||
/** | ||
* Only return strings that match the search perfectly (case excluded) | ||
* @var boolean | ||
*/ | ||
public $regex_perfect_match; | ||
|
||
/** | ||
* The search terms for the regex, those differ from $search_terms as | ||
* they can be changed dynamically via setRegexSearchTerms() | ||
* @var string | ||
*/ | ||
public $regex_search_terms; | ||
|
||
/** | ||
* We set the default values for a search | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->search_terms = ''; | ||
$this->regex = ''; | ||
$this->regex_case = 'i'; | ||
$this->regex_whole_words = ''; | ||
$this->regex_perfect_match = false; | ||
$this->regex_search_terms = ''; | ||
} | ||
|
||
/** | ||
* Store the searched string in $search_terms and in $regex_search_terms | ||
* | ||
* @param string $string String we want to search for | ||
* @return $this | ||
*/ | ||
public function setSearchTerms($string) | ||
{ | ||
$this->search_terms = trim($string); | ||
$this->regex_search_terms = $this->search_terms; | ||
$this->updateRegex(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Allows setting a new searched term for the regex. | ||
* This is mostly useful when you have a multi-words search and need to | ||
* loop through all the words to return results. | ||
* | ||
* @param string $string The string we want to update the regex for | ||
* @return $this | ||
*/ | ||
public function setRegexSearchTerms($string) | ||
{ | ||
$this->regex_search_terms = $string; | ||
$this->updateRegex(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the regex case sensibility. | ||
* | ||
* @param boolean $flag 'sensitive' == '' in a regex | ||
* @return $this | ||
*/ | ||
public function setRegexCase($flag) | ||
{ | ||
$this->regex_case = (boolean) $flag ? '' : 'i'; | ||
$this->updateRegex(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the regex to only return perfect matches for the searched string. | ||
* We cast the value to a boolean because we usually get it from a GET. | ||
* | ||
* @param boolean $flag Set to True for a perfect match | ||
* @return $this | ||
*/ | ||
public function setRegexPerfectMatch($flag) | ||
{ | ||
$this->regex_perfect_match = (boolean) $flag; | ||
$this->updateRegex(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the regex so as that a multi-word search is taken as a single word. | ||
* We cast the value to a boolean because we usually get it from a GET. | ||
* | ||
* @param boolean $flag A string evaluated to True will add \b to the regex | ||
* @return $this | ||
*/ | ||
public function setRegexWholeWords($flag) | ||
{ | ||
$this->regex_whole_words = (boolean) $flag ? '\b' : ''; | ||
$this->updateRegex(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Update the $regex_search_terms value every time a setter to the regex | ||
* is called. | ||
* | ||
* @return $this | ||
*/ | ||
private function updateRegex() | ||
{ | ||
if ($this->regex_perfect_match) { | ||
$search = '^' . $this->regex_search_terms . '$'; | ||
} else { | ||
$search = preg_quote($this->regex_search_terms, '~'); | ||
} | ||
|
||
$this->regex = | ||
'~' | ||
. $this->regex_whole_words | ||
. $search | ||
. $this->regex_whole_words | ||
. '~' | ||
. $this->regex_case | ||
. 'u'; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.