Cleanup and remove appostrophies in search string by default

This commit is contained in:
Tom Lingham
2014-07-18 19:57:13 +10:00
parent c9f28197c0
commit 8931bc2e56
10 changed files with 19 additions and 51 deletions

View File

@ -1,7 +1,6 @@
language: php language: php
php: php:
- 5.3
- 5.4 - 5.4
- 5.5 - 5.5
- 5.6 - 5.6
@ -11,4 +10,4 @@ before_script:
- composer self-update - composer self-update
- composer install --prefer-source --no-interaction --dev - composer install --prefer-source --no-interaction --dev
script: phpunit script: vendor/bin/phpspec run

View File

@ -1,6 +1,6 @@
{ {
"name": "tom-lingham/searchy", "name": "tom-lingham/searchy",
"description": "", "description": "Laravel 4 Searchy makes user driven searching easy with fuzzy search, basic string matching and more to come!",
"authors": [ "authors": [
{ {
"name": "Tom Lingham", "name": "Tom Lingham",
@ -11,6 +11,9 @@
"php": ">=5.4.0", "php": ">=5.4.0",
"illuminate/support": "4.2.*" "illuminate/support": "4.2.*"
}, },
"require-dev" :{
"phpspec/phpspec": "~2.0"
},
"autoload": { "autoload": {
"psr-0": { "psr-0": {
"TomLingham\\Searchy": "src/" "TomLingham\\Searchy": "src/"

View File

@ -15,9 +15,8 @@ class Searchy extends Facade
*/ */
protected static function getFacadeAccessor() protected static function getFacadeAccessor()
{ {
if (!static::$app) { if (!static::$app)
static::$app = SearchyServiceProvider::make(); static::$app = SearchyServiceProvider::make();
}
return 'searchy'; return 'searchy';
} }

View File

@ -34,9 +34,6 @@ class ConsecutiveCharactersMatcher extends BaseMatcher
{ {
$searchString = $this->formatSearchString( $rawString ); $searchString = $this->formatSearchString( $rawString );
$query = "IF($column {$this->operator} '$searchString', ROUND({$this->multiplier} * (CHAR_LENGTH( '$rawString' ) / CHAR_LENGTH( REPLACE($column, ' ', '') ))), 0)"; return "IF($column {$this->operator} '$searchString', ROUND({$this->multiplier} * (CHAR_LENGTH( '$rawString' ) / CHAR_LENGTH( REPLACE($column, ' ', '') ))), 0)";
return $query;
} }
} }

View File

@ -12,13 +12,6 @@
class LevenshteinMatcher extends BaseMatcher class LevenshteinMatcher extends BaseMatcher
{ {
private $sensitivity;
public function setSensitivity( $sensitivity )
{
$this->sensitivity = $sensitivity;
}
/** /**
* @param $column * @param $column
* @param $searchString * @param $searchString
@ -26,7 +19,7 @@ class LevenshteinMatcher extends BaseMatcher
*/ */
public function buildQueryString( $column, $searchString ) public function buildQueryString( $column, $searchString )
{ {
return "levenshtein($column, '$searchString', {$this->sensitivity})"; return "levenshtein($column, '$searchString')";
} }
} }

View File

@ -11,6 +11,7 @@
class StudlyCaseMatcher extends BaseMatcher class StudlyCaseMatcher extends BaseMatcher
{ {
/** /**
* @var string * @var string
*/ */
@ -28,8 +29,6 @@ class StudlyCaseMatcher extends BaseMatcher
public function buildQueryString( $column, $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 "IF( CHAR_LENGTH( TRIM($column)) = CHAR_LENGTH( REPLACE( TRIM($column), ' ', '')) AND $column {$this->operator} '{$this->formatSearchString($searchString)}', {$this->multiplier}, 0)";
return $query;
} }
} }

View File

@ -78,6 +78,7 @@ class SearchBuilder {
*/ */
private function makeDriver() private function makeDriver()
{ {
// Check if default driver is being overridden, otherwise // Check if default driver is being overridden, otherwise
// load the default // load the default
if ( $this->driverName ){ if ( $this->driverName ){
@ -86,9 +87,11 @@ class SearchBuilder {
$driverName = \Config::get('searchy::default'); $driverName = \Config::get('searchy::default');
} }
// Gets the details for the selected driver from the configuration file // Gets the details for the selected driver from the configuration file
$driverMap = \Config::get("searchy::drivers.$driverName"); $driverMap = \Config::get("searchy::drivers.$driverName");
// Create a new instance of the selected drivers 'class' and pass // Create a new instance of the selected drivers 'class' and pass
// through table and fields to search // through table and fields to search
return new $driverMap['class']( $this->table, $this->searchFields ); return new $driverMap['class']( $this->table, $this->searchFields );

View File

@ -66,9 +66,8 @@ abstract class BaseSearchDriver implements SearchDriverInterface {
$query[] = $this->buildSelectCriteria( $searchField ); $query[] = $this->buildSelectCriteria( $searchField );
} }
$query = \DB::raw(implode(' + ', $query) . ' AS ' . \Config::get('searchy::fieldName')); return \DB::raw(implode(' + ', $query) . ' AS ' . \Config::get('searchy::fieldName'));
return $query;
} }
/** /**
@ -109,7 +108,7 @@ abstract class BaseSearchDriver implements SearchDriverInterface {
*/ */
private function sanitize( $searchString ) private function sanitize( $searchString )
{ {
return preg_replace(\Config::get('searchy::sanitizeRegEx'), '', $searchString); return preg_replace(\Config::get('searchy::sanitizeRegEx'), '', $searchString );
} }
} }

View File

@ -2,35 +2,11 @@
class LevenshteinSearchDriver extends BaseSearchDriver { class LevenshteinSearchDriver extends BaseSearchDriver {
/**
private $sensitivity = 10; * @var array
*/
protected $matchers = [ protected $matchers = [
'TomLingham\Searchy\Matchers\LevenshteinMatcher' => 100 'TomLingham\Searchy\Matchers\LevenshteinMatcher' => 100
]; ];
public function setSensitivity( $sensitivity ){
$this->sensitivity = $sensitivity;
return $this;
}
/**
* @param $column
* @param $matcherClass
* @param $multiplier
* @return mixed
*/
protected function makeMatcher( $column, $matcherClass, $multiplier )
{
$matcher = new $matcherClass( $multiplier );
$matcher->setSensitivity( $this->sensitivity );
return $matcher->buildQueryString( $column, $this->searchString );
}
} }

View File

@ -6,7 +6,7 @@ return [
'sanitize' => true, 'sanitize' => true,
'sanitizeRegEx' => '/[%]+/i', 'sanitizeRegEx' => '/[%\']+/i',
'fieldName' => 'relevance', 'fieldName' => 'relevance',