Completed
Push — master ( b6b65e...5c1bad )
by Timo
05:41
created

DataScout::buttonText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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