SearchFilter::transform()   A
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 4
b 0
f 0
nc 18
nop 1
dl 0
loc 27
ccs 12
cts 12
cp 1
crap 6
rs 9.2222
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu\Filters;
4
5
use JeroenNoten\LaravelAdminLte\Helpers\NavbarItemHelper;
6
use JeroenNoten\LaravelAdminLte\Helpers\SidebarItemHelper;
7
8
class SearchFilter implements FilterInterface
9
{
10
    /**
11
     * The default HTML name attribute to be used on the search input.
12
     *
13
     * @var string
14
     */
15
    protected $defName = 'adminlteSearch';
16
17
    /**
18
     * The default HTML method attribute to be used on the search input.
19
     *
20
     * @var string
21
     */
22
    protected $defMethod = 'get';
23
24
    /**
25
     * Transforms a menu item. Makes the proper configuration for a search bar
26
     * item.
27
     *
28
     * @param  array  $item  A menu item
29
     * @return array
30
     */
31 54
    public function transform($item)
32
    {
33
        // Menu items that aren't a search bar should be ignored.
34
35 54
        $isSearchBar = NavbarItemHelper::isSearch($item)
36 54
            || SidebarItemHelper::isSearch($item);
37
38 54
        if (! $isSearchBar) {
39 52
            return $item;
40
        }
41
42
        // Setup the search bar method attribute.
43
44 3
        $isValidMethod = isset($item['method'])
45 3
            && in_array(strtolower($item['method']), ['post', 'get']);
46
47 3
        if (! $isValidMethod) {
48 3
            $item['method'] = $this->defMethod;
49
        }
50
51
        // Setup the search bar input name attribute.
52
53 3
        if (empty($item['input_name'])) {
54 3
            $item['input_name'] = $this->defName;
55
        }
56
57 3
        return $item;
58
    }
59
}
60