From 187867ea47b2886756846be37ba0fc74cef41df6 Mon Sep 17 00:00:00 2001 From: Davide Bellini Date: Wed, 22 Oct 2014 16:46:59 +0200 Subject: [PATCH 1/2] Coding style fixes Based on http://laravel.com/docs/4.2/contributions#coding-style --- .../Searchable/SearchableTrait.php | 63 +++++++++++++------ 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/src/Nicolaslopezj/Searchable/SearchableTrait.php b/src/Nicolaslopezj/Searchable/SearchableTrait.php index 8365a0f..de4dce2 100755 --- a/src/Nicolaslopezj/Searchable/SearchableTrait.php +++ b/src/Nicolaslopezj/Searchable/SearchableTrait.php @@ -8,15 +8,15 @@ */ trait SearchableTrait { - /** * Makes the search process + * * @param $query * @param $search * @return mixed */ - public function scopeSearch($query, $search) { - + public function scopeSearch($query, $search) + { $query->select($this->getTable() . '.*'); $this->makeJoins($query); @@ -25,14 +25,17 @@ public function scopeSearch($query, $search) { return $query; } - $relevance_count = 0; $words = explode(' ', $search); - $selects = []; - foreach ($this->getColumns() as $column => $relevance) { + $relevance_count = 0; + + foreach ($this->getColumns() as $column => $relevance) + { $relevance_count += $relevance; $queries = $this->getSearchQueriesForColumn($column, $relevance, $words); - foreach ($queries as $select) { + + foreach ($queries as $select) + { $selects[] = $select; } } @@ -47,79 +50,99 @@ public function scopeSearch($query, $search) { /** * Returns the search columns + * * @return array */ - protected function getColumns() { + protected function getColumns() + { return $this->searchable['columns']; } /** * Returns the tables that has to join + * * @return array */ - protected function getJoins() { - if (!array_key_exists('joins', $this->searchable)) { + protected function getJoins() + { + if ( ! array_key_exists('joins', $this->searchable)) + { return []; } + return $this->searchable['joins']; } /** * Adds the join sql to the query + * * @param $query */ - protected function makeJoins(&$query) { - foreach ($this->getJoins() as $table => $keys) { + protected function makeJoins(&$query) + { + foreach ($this->getJoins() as $table => $keys) + { $query->leftJoin($table, $keys[0], '=', $keys[1]); } } /** * Make the query dont repeat the results + * * @param $query */ - protected function makeGroupBy(&$query) { + protected function makeGroupBy(&$query) + { $primary_key = $this->primaryKey; $query->groupBy($primary_key); } /** * Puts all the select clauses to the main query + * * @param $query * @param $selects */ - protected function addSelectsToQuery(&$query, $selects) { + protected function addSelectsToQuery(&$query, $selects) + { $selects = new Expression(join(' + ', $selects) . ' as relevance'); $query->addSelect($selects); } /** * Adds relevance filter to the query + * * @param $query * @param $relevance_count */ - protected function filterQueryWithRelevace(&$query, $relevance_count) { + protected function filterQueryWithRelevace(&$query, $relevance_count) + { $query->havingRaw('relevance > ' . $relevance_count); $query->orderBy('relevance', 'desc'); } /** * Returns the search queries for the specified column + * * @param $column * @param $relevance * @param $words * @return array */ - protected function getSearchQueriesForColumn($column, $relevance, $words) { + protected function getSearchQueriesForColumn($column, $relevance, $words) + { $queries = []; + $queries[] = $this->getSearchQuery($column, $relevance, $words, '=', 15); $queries[] = $this->getSearchQuery($column, $relevance, $words, 'LIKE', 5, '', '%'); $queries[] = $this->getSearchQuery($column, $relevance, $words, 'LIKE', 1, '%', '%'); + return $queries; } /** * Returns the sql string for the parameters + * * @param $column * @param $relevance * @param $words @@ -129,9 +152,12 @@ protected function getSearchQueriesForColumn($column, $relevance, $words) { * @param string $post_word * @return string */ - protected function getSearchQuery($column, $relevance, $words, $compare, $relevance_multiplier, $pre_word = '', $post_word = '') { + protected function getSearchQuery($column, $relevance, $words, $compare, $relevance_multiplier, $pre_word = '', $post_word = '') + { $fields = []; - foreach ($words as $word) { + + foreach ($words as $word) + { $fields[] = $column . " " . $compare . " '" . $pre_word . $word . $post_word . "'"; } @@ -139,5 +165,4 @@ protected function getSearchQuery($column, $relevance, $words, $compare, $releva return 'if(' . $fields . ', ' . $relevance * $relevance_multiplier . ', 0)'; } - } \ No newline at end of file From 0f3972da6aa2034037aaa060883a7d5d4e796d00 Mon Sep 17 00:00:00 2001 From: Davide Bellini Date: Wed, 22 Oct 2014 16:51:18 +0200 Subject: [PATCH 2/2] Small improvements --- .../Searchable/SearchableTrait.php | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/Nicolaslopezj/Searchable/SearchableTrait.php b/src/Nicolaslopezj/Searchable/SearchableTrait.php index de4dce2..60aee0f 100755 --- a/src/Nicolaslopezj/Searchable/SearchableTrait.php +++ b/src/Nicolaslopezj/Searchable/SearchableTrait.php @@ -32,12 +32,9 @@ public function scopeSearch($query, $search) foreach ($this->getColumns() as $column => $relevance) { $relevance_count += $relevance; - $queries = $this->getSearchQueriesForColumn($column, $relevance, $words); - foreach ($queries as $select) - { - $selects[] = $select; - } + $queries = $this->getSearchQueriesForColumn($column, $relevance, $words); + $selects = array_merge($queries); } $this->addSelectsToQuery($query, $selects); @@ -65,12 +62,7 @@ protected function getColumns() */ protected function getJoins() { - if ( ! array_key_exists('joins', $this->searchable)) - { - return []; - } - - return $this->searchable['joins']; + return array_get($this->searchable, 'joins', []); } /** @@ -93,8 +85,7 @@ protected function makeJoins(&$query) */ protected function makeGroupBy(&$query) { - $primary_key = $this->primaryKey; - $query->groupBy($primary_key); + $query->groupBy($this->primaryKey); } /** @@ -105,7 +96,7 @@ protected function makeGroupBy(&$query) */ protected function addSelectsToQuery(&$query, $selects) { - $selects = new Expression(join(' + ', $selects) . ' as relevance'); + $selects = new Expression(implode(' + ', $selects) . ' as relevance'); $query->addSelect($selects); } @@ -161,7 +152,7 @@ protected function getSearchQuery($column, $relevance, $words, $compare, $releva $fields[] = $column . " " . $compare . " '" . $pre_word . $word . $post_word . "'"; } - $fields = join(' || ', $fields); + $fields = implode(' || ', $fields); return 'if(' . $fields . ', ' . $relevance * $relevance_multiplier . ', 0)'; }