Switch to PSR-4, fix CS and update service provider

This commit is contained in:
James Brooks
2015-12-22 09:34:30 +00:00
parent d961509341
commit c2ec458b2c
40 changed files with 753 additions and 723 deletions

30
src/Matchers/AcronymMatcher.php Executable file
View File

@ -0,0 +1,30 @@
<?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
*/
class AcronymMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE';
/**
* @param $searchString
*
* @return mixed|string
*/
public function formatSearchString($searchString)
{
$searchString = preg_replace('/[^0-9a-zA-Z]/', '', $searchString);
return implode('% ', str_split(strtoupper($searchString))).'%';
}
}

36
src/Matchers/BaseMatcher.php Executable file
View File

@ -0,0 +1,36 @@
<?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,44 @@
<?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
*/
class ConsecutiveCharactersMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE';
/**
* @param $searchString
*
* @return string
*/
public function formatSearchString($searchString)
{
$searchString = preg_replace('/[^0-9a-zA-Z]/', '', $searchString);
return '%'.implode('%', str_split($searchString)).'%';
}
/**
* @param $column
* @param $rawString
*
* @return mixed|string
*/
public function buildQueryString($column, $rawString)
{
$searchString = $this->formatSearchString($rawString);
return "IF(REPLACE($column, '\.', '') {$this->operator} '$searchString', ROUND({$this->multiplier} * (CHAR_LENGTH( '$rawString' ) / CHAR_LENGTH( REPLACE($column, ' ', '') ))), 0)";
}
}

18
src/Matchers/ExactMatcher.php Executable file
View File

@ -0,0 +1,18 @@
<?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
*/
class ExactMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = '=';
}

View File

@ -0,0 +1,28 @@
<?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
*/
class InStringMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE';
/**
* @param $searchString
*
* @return string
*/
public function formatSearchString($searchString)
{
return "%$searchString%";
}
}

View File

@ -0,0 +1,24 @@
<?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
*/
class LevenshteinMatcher extends BaseMatcher
{
/**
* @param $column
* @param $searchString
*
* @return mixed|string
*/
public function buildQueryString($column, $searchString)
{
return "levenshtein($column, '$searchString')";
}
}

View File

@ -0,0 +1,28 @@
<?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
*/
class StartOfStringMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE';
/**
* @param $searchString
*
* @return string
*/
public function formatSearchString($searchString)
{
return "$searchString%";
}
}

View File

@ -0,0 +1,28 @@
<?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
*/
class StartOfWordsMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE';
/**
* @param $searchString
*
* @return string
*/
public function formatSearchString($searchString)
{
return implode('% ', explode(' ', $searchString)).'%';
}
}

View File

@ -0,0 +1,35 @@
<?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
*/
class StudlyCaseMatcher extends BaseMatcher
{
/**
* @var string
*/
protected $operator = 'LIKE BINARY';
/**
* @param $searchString
*
* @return string
*/
public function formatSearchString($searchString)
{
$searchString = preg_replace('/[^0-9a-zA-Z]/', '', $searchString);
return implode('%', str_split(strtoupper($searchString))).'%';
}
public function buildQueryString($column, $searchString)
{
return "IF( CHAR_LENGTH( TRIM($column)) = CHAR_LENGTH( REPLACE( TRIM($column), ' ', '')) AND $column {$this->operator} '{$this->formatSearchString($searchString)}', {$this->multiplier}, 0)";
}
}

View File

@ -0,0 +1,29 @@
<?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
*/
class TimesInStringMatcher extends BaseMatcher
{
/**
* @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;
}
}