1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hamburgscleanest\DataTables\Models\DataComponents\Search; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class FulltextSearch |
9
|
|
|
* @package hamburgscleanest\DataTables\Models\DataComponents\Search |
10
|
|
|
*/ |
11
|
|
|
class FulltextSearch extends DataSearch { |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
private $_mode; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $_databaseDriver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* FulltextSearch constructor. |
21
|
|
|
* @param array $searchableFields |
22
|
|
|
* @param string|null $mode |
23
|
|
|
*/ |
24
|
3 |
|
public function __construct(array $searchableFields = [], string $mode = null) |
25
|
|
|
{ |
26
|
3 |
|
parent::__construct($searchableFields); |
27
|
3 |
|
$this->_mode = $mode; |
28
|
3 |
|
$this->_databaseDriver = \config('database.connections.' . \config('database.default') . '.driver'); |
29
|
3 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set the mode for the full text search. |
33
|
|
|
* Available modes: |
34
|
|
|
* "IN NATURAL LANGUAGE MODE", "IN BOOLEAN MODE", "WITH QUERY EXPANSION" |
35
|
|
|
* @see https://dev.mysql.com/doc/refman/5.7/en/fulltext-search.html |
36
|
|
|
* |
37
|
|
|
* @param string $mode |
38
|
|
|
* @return DataSearch |
|
|
|
|
39
|
|
|
*/ |
40
|
1 |
|
public function setMode(string $mode) : DataSearch |
41
|
|
|
{ |
42
|
1 |
|
$this->_mode = $mode; |
43
|
|
|
|
44
|
1 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Builder $queryBuilder |
49
|
|
|
* @param string $value |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
2 |
|
protected function _searchFields(Builder $queryBuilder, string $value) : void |
53
|
|
|
{ |
54
|
2 |
|
$queryBuilder->orWhereRaw($this->_getMatchQuery($value)); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* This has to be improved. |
59
|
|
|
* @param string $value |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
2 |
|
private function _getMatchQuery(string $value) : string |
63
|
|
|
{ |
64
|
2 |
|
if ($this->_databaseDriver === 'sqlite') |
65
|
|
|
{ |
66
|
2 |
|
$query = ' MATCH \'' . $value . '\''; |
67
|
2 |
|
$matchQuery = ''; |
68
|
2 |
|
foreach ($this->_searchableFields as $field) |
69
|
|
|
{ |
70
|
2 |
|
$matchQuery .= $field . $query; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
return $matchQuery; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return 'MATCH(' . \implode(',', $this->_searchableFields) . ') AGAINST (\'' . $value . '\'' . (!empty($this->_mode) ? $this->_mode : '') . ')'; |
77
|
|
|
} |
78
|
|
|
} |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.