Completed
Push — master ( 49cc4e...8837bb )
by Timo
08:26
created

DataScout   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 142
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A _shapeData() 0 17 3
A _addWhereQueries() 0 7 2
A addQuery() 0 6 1
A buttonText() 0 6 1
A placeholder() 0 6 1
A makeSearchable() 0 6 1
A render() 0 7 1
A _buildSearchUrl() 0 7 1
A _afterInit() 0 8 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 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 4
        {
51
            foreach ($this->_searchQueries as $value)
52 4
            {
53
                $this->_addWhereQueries($query, $value);
54 4
            }
55
        });
56 4
57
        return $this->_queryBuilder;
58
    }
59 4
60
    /**
61
     * @param Builder $query
62 4
     * @param string $value
63
     */
64
    private function _addWhereQueries(Builder $query, string $value) : void
65
    {
66
        foreach ($this->_searchableFields as $field)
67
        {
68
            $query->orWhere($field, 'like', '%' . $value . '%');
69
        }
70
    }
71 3
72
    /**
73 3
     * Add a query programmatically.
74
     *
75 3
     * @param string $value
76
     * @return DataScout
77
     */
78
    public function addQuery(string $value) : DataScout
79
    {
80
        $this->_searchQueries[] = $value;
81
82
        return $this;
83
    }
84 1
85
    /**
86 1
     * Set the text for the search button.
87
     *
88 1
     * @param string $text
89
     * @return DataScout
90
     */
91
    public function buttonText(string $text) : DataScout
92
    {
93
        $this->_buttonText = $text;
94
95
        return $this;
96
    }
97 1
98
    /**
99 1
     * Set the placeholder for the input.
100
     *
101 1
     * @param string $text
102
     * @return DataScout
103
     */
104
    public function placeholder(string $text) : DataScout
105
    {
106
        $this->_placeholder = $text;
107
108
        return $this;
109
    }
110 1
111
    /**
112 1
     * Make the field searchable.
113
     *
114 1
     * @param string $field
115
     * @return DataScout
116
     */
117
    public function makeSearchable(string $field) : DataScout
118
    {
119
        $this->_searchableFields[] = $field;
120 2
121
        return $this;
122 2
    }
123 2
124 2
    /**
125 2
     * @return string
126
     */
127
    public function render() : string
128
    {
129
        return '<form method="get" action="' . $this->_buildSearchUrl() .
130
               '"><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
               $this->_buttonText . '</button></div></div></form>';
133 2
    }
134 2
135
    /**
136 2
     * @return string
137
     */
138
    private function _buildSearchUrl() : string
139 7
    {
140
        $parameters = UrlHelper::queryParameters();
141 7
        $parameters['search'] = \implode(',', $this->_searchQueries);
142 7
143
        return \request()->url() . '?' . \http_build_query($parameters);
144 1
    }
145
146 7
    protected function _afterInit() : void
147
    {
148
        $search = \request()->get('search');
149
        if (!empty($search))
150
        {
151
            $this->_searchQueries += \explode(',', $search);
152
        }
153
    }
154
}