FEATURE: add parent class for all search engines

This commit is contained in:
Michal Kváček
2016-03-15 13:57:40 +01:00
parent 4d19d4388a
commit c4e65e18c4
2 changed files with 33 additions and 8 deletions
+7 -8
View File
@@ -1,10 +1,9 @@
<?php
namespace App\Handlers\Search;
use App\Handlers\Contracts\SearchHandler;
use App\Models\Content as ContentModel;
class Content implements SearchHandler
class Content extends Search
{
/**
* Search for given patttern/term
@@ -15,14 +14,14 @@ class Content implements SearchHandler
*/
public function search($term)
{
$token = '%' . $term . '%';
$token = $this->tokenize($term);
return ContentModel::where('content', 'LIKE', $token)
->orWhere('meta_description', 'LIKE', $token)
->orWhere('meta_keywords', 'LIKE', $token)
->orWhere('title', 'LIKE', $token)
->orWhere('url', 'LIKE', $token)
->get();
->orWhere('meta_description', 'LIKE', $token)
->orWhere('meta_keywords', 'LIKE', $token)
->orWhere('title', 'LIKE', $token)
->orWhere('url', 'LIKE', $token)
->get();
}
public function getId()
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace App\Handlers\Search;
use App\Handlers\Contracts\SearchHandler;
use Symfony\Component\Process\Exception\InvalidArgumentException;
abstract class Search implements SearchHandler
{
/**
* Tokenize string for usage in LIKE clausules
*
* @param $term
*
* @return string
*/
protected function tokenize($term)
{
$term = trim($term);
if (empty($term)) {
throw new InvalidArgumentException(trans('search.emptyString'));
}
return '%' . str_replace(' ', '%', $term) . '%';
}
}