Completed
Branch master (a2d832)
by Timo
02:40
created

DataScout::shapeData()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

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