FulltextSearch::_getMatchQuery()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 10
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
    const SUPPORTED_DRIVERS = ['mysql', 'sqlite'];
14
15
    /** @var string */
16
    private $_mode;
17
18
    /** @var string */
19
    private $_databaseDriver;
20
21
    /**
22
     * FulltextSearch constructor.
23
     * @param array $searchableFields
24
     * @param string|null $mode
25
     */
26 5
    public function __construct(array $searchableFields = [], string $mode = null)
27
    {
28 5
        parent::__construct($searchableFields);
29 5
        $this->_mode = $mode;
30 5
        $this->_databaseDriver = \config('database.connections.' . \config('database.default') . '.driver');
31 5
    }
32
33
    /**
34
     * @param string $driver
35
     * @return FulltextSearch
36
     * @throws \RuntimeException
37
     */
38 3
    public function forceDatabaseDriver(string $driver) : FulltextSearch
39
    {
40 3
        if (!\in_array($driver, self::SUPPORTED_DRIVERS, true))
41
        {
42 1
            throw new \RuntimeException($driver . ' is not supported at the moment');
43
        }
44
45 2
        $this->_databaseDriver = $driver;
46
47 2
        return $this;
48
    }
49
50
    /**
51
     * Set the mode for the full text search.
52
     * Available modes:
53
     * "IN NATURAL LANGUAGE MODE", "IN BOOLEAN MODE", "WITH QUERY EXPANSION"
54
     * @see https://dev.mysql.com/doc/refman/5.7/en/fulltext-search.html
55
     *
56
     * @param string $mode
57
     * @return FulltextSearch
58
     */
59 1
    public function setMode(string $mode) : FulltextSearch
60
    {
61 1
        $this->_mode = $mode;
62
63 1
        return $this;
64
    }
65
66
    /**
67
     * @param Builder $queryBuilder
68
     * @param string $value
69
     * @return void
70
     */
71 3
    protected function _searchFields(Builder $queryBuilder, string $value) : void
72
    {
73 3
        $queryBuilder->orWhereRaw($this->_getMatchQuery($value));
74 3
    }
75
76
    /**
77
     * This has to be improved.
78
     * @param string $value
79
     * @return string
80
     */
81 3
    private function _getMatchQuery(string $value) : string
82
    {
83 3
        if ($this->_databaseDriver === 'sqlite')
84
        {
85 1
            $query = ' MATCH \'' . $value . '\'';
86 1
            $matchQuery = '';
87 1
            foreach ($this->_searchableFields as $field)
88
            {
89 1
                $matchQuery .= $field . $query;
90
            }
91
92 1
            return $matchQuery;
93
        }
94
95 2
        return 'MATCH(' . \implode(',', $this->_searchableFields) . ') AGAINST (\'' . $value . '\'' . (!empty($this->_mode) ? (' ' . $this->_mode) : '') . ')';
96
    }
97
}