DataScout::_afterInit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace hamburgscleanest\DataTables\Models\DataComponents;
4
5
use hamburgscleanest\DataTables\Models\DataComponent;
6
use hamburgscleanest\DataTables\Models\DataComponents\Search\DataSearch;
7
use Illuminate\Database\Eloquent\Builder;
8
9
/**
10
 * Class DataScout
11
 * @package hamburgscleanest\DataTables\Models\DataComponents
12
 */
13
class DataScout extends DataComponent {
14
15
    /** @var string */
16
    private $_placeholder = 'Search..';
17
18
    /** @var DataSearch */
19
    private $_dataSearch;
20
21
    /**
22
     * DataScout constructor.
23
     * @param DataSearch $dataSearch
24
     * @param bool $remember
25
     */
26 9
    public function __construct(DataSearch $dataSearch, $remember = false)
27
    {
28 9
        $this->_dataSearch = $dataSearch;
29 9
        $this->_rememberKey = 'data-scout';
30 9
        $this->_rememberState = $remember;
31 9
    }
32
33
    /**
34
     * How should the dataset be searched?
35
     * Define the search algorithm, e.g. SimpleSearch or FulltextSearch
36
     *
37
     * @param DataSearch $dataSearch
38
     * @return DataScout
39
     */
40 1
    public function setSearch(DataSearch $dataSearch) : DataScout
41
    {
42 1
        $this->_dataSearch = $dataSearch;
43
44 1
        return $this;
45
    }
46
47
    /**
48
     * Add a query programmatically.
49
     *
50
     * @param string $query
51
     * @return DataScout
52
     */
53 6
    public function addQuery(string $query) : DataScout
54
    {
55 6
        $this->_dataSearch->addQuery($query);
56
57 6
        return $this;
58
    }
59
60
    /**
61
     * Set the placeholder for the input.
62
     *
63
     * @param string $text
64
     * @return DataScout
65
     */
66 1
    public function placeholder(string $text) : DataScout
67
    {
68 1
        $this->_placeholder = $text;
69
70 1
        return $this;
71
    }
72
73
    /**
74
     * Make the field searchable.
75
     *
76
     * @param string $field
77
     * @return DataScout
78
     */
79 1
    public function makeSearchable(string $field) : DataScout
80
    {
81 1
        $this->_dataSearch->addField($field);
82
83 1
        return $this;
84
    }
85
86
    /**
87
     * @return string
88
     */
89 1
    public function render() : string
90
    {
91 1
        return '<input name="' . $this->getName() . '-search" class="form-control datascout-input" placeholder="' . $this->_placeholder . '"/>';
92
    }
93
94
    /**
95
     * @return string
96
     */
97 1
    public function getSearchUrl() : string
98
    {
99 1
        return $this->_dataSearch->getSearchUrl();
100
    }
101
102
    /**
103
     * @return Builder
104
     */
105 7
    protected function _shapeData() : Builder
106
    {
107 7
        if ($this->_dataSearch->queryCount() === 0)
108
        {
109 1
            return $this->_dataTable->query();
110
        }
111
112 6
        return $this->_dataSearch->searchData($this->_dataTable->query());
113
    }
114
115 8
    protected function _afterInit() : void
116
    {
117 8
        $search = \request()->get('search');
118 8
        if (!empty($search))
119
        {
120 1
            $this->_dataSearch->addQueries(\explode(',', $search));
121
        }
122
    }
123
}