From 8931bc2e56aef4dd317495de953d339aa8a442d3 Mon Sep 17 00:00:00 2001 From: Tom Lingham Date: Fri, 18 Jul 2014 19:57:13 +1000 Subject: [PATCH] Cleanup and remove appostrophies in search string by default --- .travis.yml | 3 +-- composer.json | 5 +++- src/TomLingham/Searchy/Facades/Searchy.php | 3 +-- .../Matchers/ConsecutiveCharactersMatcher.php | 5 +--- .../Searchy/Matchers/LevenshteinMatcher.php | 9 +------ .../Searchy/Matchers/StudlyCaseMatcher.php | 5 ++-- src/TomLingham/Searchy/SearchBuilder.php | 3 +++ .../Searchy/SearchDrivers/BaseSearchDriver.php | 5 ++-- .../SearchDrivers/LevenshteinSearchDriver.php | 30 +++------------------- src/config/config.php | 2 +- 10 files changed, 19 insertions(+), 51 deletions(-) diff --git a/.travis.yml b/.travis.yml index aa14ee5..9e7e777 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.3 - 5.4 - 5.5 - 5.6 @@ -11,4 +10,4 @@ before_script: - composer self-update - composer install --prefer-source --no-interaction --dev -script: phpunit +script: vendor/bin/phpspec run diff --git a/composer.json b/composer.json index 50d49e2..dad803c 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "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": [ { "name": "Tom Lingham", @@ -11,6 +11,9 @@ "php": ">=5.4.0", "illuminate/support": "4.2.*" }, + "require-dev" :{ + "phpspec/phpspec": "~2.0" + }, "autoload": { "psr-0": { "TomLingham\\Searchy": "src/" diff --git a/src/TomLingham/Searchy/Facades/Searchy.php b/src/TomLingham/Searchy/Facades/Searchy.php index 51b25e8..9413723 100644 --- a/src/TomLingham/Searchy/Facades/Searchy.php +++ b/src/TomLingham/Searchy/Facades/Searchy.php @@ -15,9 +15,8 @@ class Searchy extends Facade */ protected static function getFacadeAccessor() { - if (!static::$app) { + if (!static::$app) static::$app = SearchyServiceProvider::make(); - } return 'searchy'; } diff --git a/src/TomLingham/Searchy/Matchers/ConsecutiveCharactersMatcher.php b/src/TomLingham/Searchy/Matchers/ConsecutiveCharactersMatcher.php index 22a7a02..58d419b 100644 --- a/src/TomLingham/Searchy/Matchers/ConsecutiveCharactersMatcher.php +++ b/src/TomLingham/Searchy/Matchers/ConsecutiveCharactersMatcher.php @@ -34,9 +34,6 @@ class ConsecutiveCharactersMatcher extends BaseMatcher { $searchString = $this->formatSearchString( $rawString ); - $query = "IF($column {$this->operator} '$searchString', ROUND({$this->multiplier} * (CHAR_LENGTH( '$rawString' ) / CHAR_LENGTH( REPLACE($column, ' ', '') ))), 0)"; - - return $query; + return "IF($column {$this->operator} '$searchString', ROUND({$this->multiplier} * (CHAR_LENGTH( '$rawString' ) / CHAR_LENGTH( REPLACE($column, ' ', '') ))), 0)"; } - } \ No newline at end of file diff --git a/src/TomLingham/Searchy/Matchers/LevenshteinMatcher.php b/src/TomLingham/Searchy/Matchers/LevenshteinMatcher.php index 0325bb3..cb832d8 100644 --- a/src/TomLingham/Searchy/Matchers/LevenshteinMatcher.php +++ b/src/TomLingham/Searchy/Matchers/LevenshteinMatcher.php @@ -12,13 +12,6 @@ class LevenshteinMatcher extends BaseMatcher { - private $sensitivity; - - public function setSensitivity( $sensitivity ) - { - $this->sensitivity = $sensitivity; - } - /** * @param $column * @param $searchString @@ -26,7 +19,7 @@ class LevenshteinMatcher extends BaseMatcher */ public function buildQueryString( $column, $searchString ) { - return "levenshtein($column, '$searchString', {$this->sensitivity})"; + return "levenshtein($column, '$searchString')"; } } \ No newline at end of file diff --git a/src/TomLingham/Searchy/Matchers/StudlyCaseMatcher.php b/src/TomLingham/Searchy/Matchers/StudlyCaseMatcher.php index c9908cf..09161a2 100644 --- a/src/TomLingham/Searchy/Matchers/StudlyCaseMatcher.php +++ b/src/TomLingham/Searchy/Matchers/StudlyCaseMatcher.php @@ -11,6 +11,7 @@ class StudlyCaseMatcher extends BaseMatcher { + /** * @var string */ @@ -28,8 +29,6 @@ class StudlyCaseMatcher extends BaseMatcher 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; + return "IF( CHAR_LENGTH( TRIM($column)) = CHAR_LENGTH( REPLACE( TRIM($column), ' ', '')) AND $column {$this->operator} '{$this->formatSearchString($searchString)}', {$this->multiplier}, 0)"; } } \ No newline at end of file diff --git a/src/TomLingham/Searchy/SearchBuilder.php b/src/TomLingham/Searchy/SearchBuilder.php index 608d66b..011b04a 100644 --- a/src/TomLingham/Searchy/SearchBuilder.php +++ b/src/TomLingham/Searchy/SearchBuilder.php @@ -78,6 +78,7 @@ class SearchBuilder { */ private function makeDriver() { + // Check if default driver is being overridden, otherwise // load the default if ( $this->driverName ){ @@ -86,9 +87,11 @@ class SearchBuilder { $driverName = \Config::get('searchy::default'); } + // Gets the details for the selected driver from the configuration file $driverMap = \Config::get("searchy::drivers.$driverName"); + // Create a new instance of the selected drivers 'class' and pass // through table and fields to search return new $driverMap['class']( $this->table, $this->searchFields ); diff --git a/src/TomLingham/Searchy/SearchDrivers/BaseSearchDriver.php b/src/TomLingham/Searchy/SearchDrivers/BaseSearchDriver.php index 474e9f1..6377398 100644 --- a/src/TomLingham/Searchy/SearchDrivers/BaseSearchDriver.php +++ b/src/TomLingham/Searchy/SearchDrivers/BaseSearchDriver.php @@ -66,9 +66,8 @@ abstract class BaseSearchDriver implements SearchDriverInterface { $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 ) { - return preg_replace(\Config::get('searchy::sanitizeRegEx'), '', $searchString); + return preg_replace(\Config::get('searchy::sanitizeRegEx'), '', $searchString ); } } \ No newline at end of file diff --git a/src/TomLingham/Searchy/SearchDrivers/LevenshteinSearchDriver.php b/src/TomLingham/Searchy/SearchDrivers/LevenshteinSearchDriver.php index 50b198e..439a2fa 100644 --- a/src/TomLingham/Searchy/SearchDrivers/LevenshteinSearchDriver.php +++ b/src/TomLingham/Searchy/SearchDrivers/LevenshteinSearchDriver.php @@ -2,35 +2,11 @@ class LevenshteinSearchDriver extends BaseSearchDriver { - - private $sensitivity = 10; - + /** + * @var array + */ protected $matchers = [ '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 ); - - } - } \ No newline at end of file diff --git a/src/config/config.php b/src/config/config.php index 82937cb..b61f16d 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -6,7 +6,7 @@ return [ 'sanitize' => true, - 'sanitizeRegEx' => '/[%]+/i', + 'sanitizeRegEx' => '/[%\']+/i', 'fieldName' => 'relevance',