First release beta

This commit is contained in:
Tom Lingham
2014-07-14 10:35:19 +10:00
parent 4c0890f936
commit 44ace92f93
20 changed files with 683 additions and 1 deletions

View File

@ -0,0 +1,32 @@
<?php namespace TomLingham\Searchy\Matchers;
/**
* Matches strings for Acronym 'like' matches but does NOT return Studly Case Matches
*
* for example, a search for 'fb' would match; 'foo bar' or 'Fred Brown' but not 'FreeBeer'.
*
* Class AcronymMatcher
* @package TomLingham\Searchy\Matchers
*/
class AcronymMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE';
/**
* @var int
*/
protected $multiplier = 42;
/**
* @param $searchString
* @return mixed|string
*/
public function formatSearchString( $searchString ) {
return implode( '% ', str_split(strtoupper( $searchString ))) . '%';
}
}

View File

@ -0,0 +1,33 @@
<?php namespace TomLingham\Searchy\Matchers;
use TomLingham\Searchy\Interfaces\MatcherInterface;
/**
* @property mixed multiplier
* @property mixed operator
*/
abstract class BaseMatcher implements MatcherInterface
{
protected $multiplier;
public function __construct( $multiplier ){
$this->multiplier = $multiplier;
}
/**
* The default process for building the Query string
*
* @param $column
* @param $searchString
* @return mixed|string
*/
public function buildQueryString( $column, $searchString ){
if ( method_exists($this, 'formatSearchString') )
$searchString = $this->formatSearchString( $searchString );
return "IF($column {$this->operator} '$searchString', {$this->multiplier}, 0)";
}
}

View File

@ -0,0 +1,46 @@
<?php namespace TomLingham\Searchy\Matchers;
/**
* Matches strings that include all the characters in the search relatively position within the string.
* It also calculates the percentage of characters in the string that are matched and applies the multiplier accordingly.
*
* For Example, a search for 'fba' would match; 'Foo Bar' or 'Afraid of bats'
*
* Class ConsecutiveCharactersMatcher
* @package TomLingham\Searchy\Matchers
*/
class ConsecutiveCharactersMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE';
/**
* @var int
*/
protected $multiplier = 40;
/**
* @param $searchString
* @return string
*/
public function formatSearchString( $searchString ) {
return '%'.implode('%', str_split( $searchString )).'%';
}
/**
* @param $column
* @param $rawString
* @return mixed|string
*/
public function buildQueryString( $column, $rawString ){
$searchString = $this->formatSearchString( $rawString );
$query = "IF($column {$this->operator} '$searchString', ROUND({$this->multiplier} * (CHAR_LENGTH( '$rawString' ) / CHAR_LENGTH( REPLACE($column, ' ', '') ))), 0)";
return $query;
}
}

View File

@ -0,0 +1,24 @@
<?php namespace TomLingham\Searchy\Matchers;
/**
* Matches an exact string and applies a high multiplier to bring any exact matches to the top
* When sanitize is on, if the expression strips some of the characters from the search query
* then this may not be able to match against a string despite entering in an exact match.
*
* Class ExactMatcher
* @package TomLingham\Searchy\Matchers
*/
class ExactMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = '=';
/**
* @var int
*/
protected $multiplier = 100;
}

View File

@ -0,0 +1,32 @@
<?php namespace TomLingham\Searchy\Matchers;
/**
* Matches against any occurrences of a string within a string and is case-insensitive.
*
* For example, a search for 'smi' would match; 'John Smith' or 'Smiley Face'
*
* Class InStringMatcher
* @package TomLingham\Searchy\Matchers
*/
class InStringMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE';
/**
* @var int
*/
protected $multiplier = 30;
/**
* @param $searchString
* @return string
*/
public function formatSearchString( $searchString ){
return "%$searchString%";
}
}

View File

@ -0,0 +1,33 @@
<?php namespace TomLingham\Searchy\Matchers;
/**
* Matches strings for Acronym 'like' matches but does NOT return Studly Case Matches
*
* for example, a search for 'fb' would match; 'foo bar' or 'Fred Brown' but not 'FreeBeer'.
*
* Class AcronymMatcher
* @package TomLingham\Searchy\Matchers
*/
class LevenshteinMatcher extends BaseMatcher
{
private $sensitivity;
public function setSensitivity( $sensitivity )
{
$this->sensitivity = $sensitivity;
}
/**
* @param $column
* @param $searchString
* @return mixed|string
*/
public function buildQueryString( $column, $searchString ){
return "levenshtein($column, '$searchString', {$this->sensitivity})";
}
}

View File

@ -0,0 +1,32 @@
<?php namespace TomLingham\Searchy\Matchers;
/**
* Matches Strings that begin with the search string.
*
* For example, a search for 'hel' would match; 'Hello World' or 'helping hand'
*
* Class StartOfStringMatcher
* @package TomLingham\Searchy\Matchers
*/
class StartOfStringMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE';
/**
* @var int
*/
protected $multiplier = 50;
/**
* @param $searchString
* @return string
*/
public function formatSearchString( $searchString ) {
return "$searchString%";
}
}

View File

@ -0,0 +1,32 @@
<?php namespace TomLingham\Searchy\Matchers;
/**
* Matches the start of each word against each word in a search
*
* For example, a search for 'jo ta' would match; 'John Taylor' or 'Joshua B. Takashi'
*
* Class StartOfWordsMatcher
* @package TomLingham\Searchy\Matchers
*/
class StartOfWordsMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE';
/**
* @var int
*/
protected $multiplier = 35;
/**
* @param $searchString
* @return string
*/
public function formatSearchString( $searchString ) {
return implode('% ', explode(' ', $searchString)) . '%';
}
}

View File

@ -0,0 +1,39 @@
<?php namespace TomLingham\Searchy\Matchers;
/**
* Matches Studly Case strings using the first letters of the words only
*
* For example a search for 'hp' would match; 'HtmlServiceProvider' or 'HashParser' but not 'hasProvider'
*
* Class StudlyCaseMatcher
* @package TomLingham\Searchy\Matchers
*/
class StudlyCaseMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE BINARY';
/**
* @var int
*/
protected $multiplier = 32;
/**
* @param $searchString
* @return string
*/
public function formatSearchString( $searchString ) {
return implode( '%', str_split(strtoupper( $searchString ))) . '%';
}
public function buildQueryString( $column, $searchString ){
$query = "IF( CHAR_LENGTH( TRIM($column)) = CHAR_LENGTH( REPLACE( TRIM($column), ' ', '')) AND $column {$this->operator} '{$this->formatSearchString($searchString)}', {$this->multiplier}, 0)";
return $query;
}
}

View File

@ -0,0 +1,34 @@
<?php namespace TomLingham\Searchy\Matchers;
/**
* Matches a string based on how many times the search string appears inside the string
* it then applies the multiplier for each occurrence.
*
* For example, a search for 'tha' would match; 'I hope that that cat has caught that mouse' (3 x multiplier) or 'Thanks, it was great!' (1 x multiplier)
*
* Class TimesInStringMatcher
* @package TomLingham\Searchy\Matchers
*/
class TimesInStringMatcher extends BaseMatcher
{
/**
* @var int
*/
protected $multiplier = 8;
/**
* @param $column
* @param $searchString
* @return mixed|string
*/
public function buildQueryString( $column, $searchString ){
$query = "{$this->multiplier} * ROUND ((
CHAR_LENGTH($column) - CHAR_LENGTH( REPLACE ( LOWER($column), lower('$searchString'), ''))
) / LENGTH('$searchString'))";
return $query;
}
}