Completed
Push — master ( 8837bb...a1d335 )
by Timo
05:59
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\Facades\UrlHelper;
6
use hamburgscleanest\DataTables\Models\DataComponent;
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 array */
16
    private $_searchQueries = [];
17
18
    /** @var array */
19
    private $_searchableFields;
20
21
    /** @var string */
22
    private $_buttonText = 'Search';
23
24
    /** @var string */
25
    private $_placeholder = 'Search..';
26
27
    /**
28
     * DataScout constructor.
29
     * @param array $searchableFields
30
     * @param bool $remember
31
     */
32 7
    public function __construct(array $searchableFields = [], $remember = false)
33
    {
34 7
        $this->_searchableFields = $searchableFields;
35 7
        $this->_rememberKey = 'data-scout';
36 7
        $this->_rememberState = $remember;
37 7
    }
38
39
    /**
40
     * @return Builder
41
     */
42 5
    public function _shapeData() : Builder
43
    {
44 5
        if (\count($this->_searchQueries) === 0)
45
        {
46 1
            return $this->_queryBuilder;
47
        }
48
49 4
        $this->_queryBuilder->where(function($query)
50
        {
51 4
            foreach ($this->_searchQueries as $value)
52
            {
53 4
                $this->_addWhereQueries($query, $value);
54
            }
55 4
        });
56
57 4
        return $this->_queryBuilder;
58
    }
59
60
    /**
61
     * @param Builder $query
62
     * @param string $value
63
     */
64 4
    private function _addWhereQueries(Builder $query, string $value) : void
65
    {
66 4
        foreach ($this->_searchableFields as $field)
67
        {
68 4
            $query->orWhere($field, 'like', '%' . $value . '%');
69
        }
70 4
    }
71
72
    /**
73
     * Add a query programmatically.
74
     *
75
     * @param string $value
76
     * @return DataScout
77
     */
78 3
    public function addQuery(string $value) : DataScout
79
    {
80 3
        $this->_searchQueries[] = $value;
81
82 3
        return $this;
83
    }
84
85
    /**
86
     * Set the text for the search button.
87
     *
88
     * @param string $text
89
     * @return DataScout
90
     */
91 1
    public function buttonText(string $text) : DataScout
92
    {
93 1
        $this->_buttonText = $text;
94
95 1
        return $this;
96
    }
97
98
    /**
99
     * Set the placeholder for the input.
100
     *
101
     * @param string $text
102
     * @return DataScout
103
     */
104 1
    public function placeholder(string $text) : DataScout
105
    {
106 1
        $this->_placeholder = $text;
107
108 1
        return $this;
109
    }
110
111
    /**
112
     * Make the field searchable.
113
     *
114
     * @param string $field
115
     * @return DataScout
116
     */
117 1
    public function makeSearchable(string $field) : DataScout
118
    {
119 1
        $this->_searchableFields[] = $field;
120
121 1
        return $this;
122
    }
123
124
    /**
125
     * @return string
126
     */
127 2
    public function render() : string
128
    {
129 2
        return '<form method="get" action="' . $this->_buildSearchUrl() .
130 2
               '"><div class="row"><div class="col-md-10"><input name="search" class="form-control data-scout-input" placeholder="' .
131 2
               $this->_placeholder . '"/></div><div class="col-md-2"><button type="submit" class="btn btn-primary">' .
132 2
               $this->_buttonText . '</button></div></div></form>';
133
    }
134
135
    /**
136
     * @return string
137
     */
138 2
    private function _buildSearchUrl() : string
139
    {
140 2
        $parameters = UrlHelper::queryParameters();
141 2
        $parameters['search'] = \implode(',', $this->_searchQueries);
142
143 2
        return \request()->url() . '?' . \http_build_query($parameters);
144
    }
145
146 7
    protected function _afterInit() : void
147
    {
148 7
        $search = \request()->get('search');
149 7
        if (!empty($search))
150
        {
151 1
            $this->_searchQueries += \explode(',', $search);
152
        }
153
    }
154
}