Completed
Pull Request — master (#8)
by Timo
05:30
created

DataScout::_afterInit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
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 6
    public function __construct(array $searchableFields = [], $remember = false)
33
    {
34 6
        $this->_searchableFields = $searchableFields;
35 6
        $this->_rememberKey = 'data-scout';
36 6
        $this->_rememberState = $remember;
37 6
    }
38
39
    /**
40
     * @return Builder
41
     */
42 4
    public function _shapeData(): Builder
43
    {
44 4
        if (\count($this->_searchQueries) === 0)
45
        {
46
            return $this->_queryBuilder;
47
        }
48
49 4
        $this->_queryBuilder->where(function($query)
50
        {
51 4
            foreach ($this->_searchQueries as $value)
52
            {
53 4
                foreach ($this->_searchableFields as $field)
54
                {
55 4
                    $query->orWhere($field, 'like', '%' . $value . '%');
56
                }
57
            }
58 4
        });
59
60 4
        return $this->_queryBuilder;
61
    }
62
63
    /**
64
     * Add a query programmatically.
65
     *
66
     * @param string $value
67
     * @return DataScout
68
     */
69 3
    public function addQuery(string $value): DataScout
70
    {
71 3
        $this->_searchQueries[] = $value;
72
73 3
        return $this;
74
    }
75
76
    /**
77
     * Set the text for the search button.
78
     *
79
     * @param string $text
80
     * @return DataScout
81
     */
82 1
    public function buttonText(string $text): DataScout
83
    {
84 1
        $this->_buttonText = $text;
85
86 1
        return $this;
87
    }
88
89
    /**
90
     * Set the placeholder for the input.
91
     *
92
     * @param string $text
93
     * @return DataScout
94
     */
95 1
    public function placeholder(string $text): DataScout
96
    {
97 1
        $this->_placeholder = $text;
98
99 1
        return $this;
100
    }
101
102
    /**
103
     * Make the field searchable.
104
     *
105
     * @param string $field
106
     * @return DataScout
107
     */
108 1
    public function makeSearchable(string $field): DataScout
109
    {
110 1
        $this->_searchableFields[] = $field;
111
112 1
        return $this;
113
    }
114
115
    /**
116
     * @return string
117
     */
118 2
    public function render(): string
119
    {
120 2
        return '<form method="get" action="' . $this->_buildSearchUrl() .
121 2
                '"><div class="row"><div class="col-md-10"><input name="search" class="form-control data-scout-input" placeholder="' .
122 2
                $this->_placeholder . '"/></div><div class="col-md-2"><button type="submit" class="btn btn-primary">' .
123 2
                $this->_buttonText . '</button></div></div></form>';
124
    }
125
126
    /**
127
     * @return string
128
     */
129 2
    private function _buildSearchUrl()
130
    {
131 2
        $parameters = UrlHelper::parameterizeQuery($this->_request->getQueryString());
132 2
        $parameters['search'] = \implode(',', $this->_searchQueries);
133
134 2
        return $this->_request->url() . '?' . \http_build_query($parameters);
135
    }
136
137 6
    protected function _afterInit()
138
    {
139 6
        $search = $this->_request->get('search');
140 6
        if (!empty($search))
141
        {
142 1
            $this->_searchQueries += \explode(',', $search);
143
        }
144
    }
145
}