SearchTrait::getSearchCols()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 16
rs 8.8571
cc 5
eloc 12
nc 5
nop 2
1
<?php namespace App\LaravelRestCms;
2
3
trait SearchTrait {
4
5
	public static $labelCol = [];
6
	public static $searchCols = [];
7
8
	/**
9
	 * Adds search methods to the model
10
	 *         
11
	 * @param string $keyword
12
	 * @param string $labelCol
13
	 * @return \App\LaravelRestCms\BaseModel
14
	 */
15
	public function addSearch($keyword = null, $labelCol = null)
16
	{
17
		$searchCols = self::getSearchCols(null, $labelCol);
18
		$keyword = strtolower($keyword);
19
        
20
		if ($searchCols && !is_null($keyword) && trim($keyword) != '') {
21
            
22
			return self::orWhere(function($query) use ($searchCols, $keyword)
23
			{
24
				foreach ($searchCols as $key=>$val) {
25
					$query->orWhere($val, 'like', "%{$keyword}%");
26
				}
27
			});
28
		} else {
29
			return $this;
30
		}
31
	}
32
    
33
	/**
34
	 * Retrieves the columns to search
35
	 * 
36
	 * @param  string $override
37
	 * @param  string $default
38
	 * @return array
39
	 */
40
	public function getSearchCols($override = null, $default = null)
41
	{
42
		if (!is_null($override)) {
43
			$searchCols = $override;
44
		} else if (!is_null(static::$searchCols)) {
45
			$searchCols = static::$searchCols;
46
		} else if (!is_null(static::$labelCol)) {
47
			$searchCols = static::$labelCol;
48
		} else if (!is_null($default)) {
49
			$searchCols = $default;
50
		} else {
51
			return false;
52
		}
53
        
54
		return (array) $searchCols;
55
	}
56
    
57
	/**
58
	 * Eloquent scope of the search
59
	 * 
60
	 * @param  \App\LaravelRestCms\BaseModel $query
61
	 * @param  string $keyword
62
	 * @return \Illuminate\Database\Eloquent\Builder
63
	 */
64
	public function scopeSearch(\App\LaravelRestCms\BaseModel $query, $keyword = null)
65
	{        
66
		$searchCols = $this->getSearchCols(null, static::$labelCol);
67
		$query->select('*', implode(' ', static::$labelCol) . ' as label', 'url');
0 ignored issues
show
Documentation Bug introduced by
The method select does not exist on object<App\LaravelRestCms\BaseModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
68
		$match = '';
69
70
		// make search results use the "and" clause
71
		$keyword[0] = '"' . $keyword[0] . '"';
72
73
		if (is_array($searchCols)) {
74
			foreach ($searchCols as $searchCol) {
75
				$match = "MATCH(" . $searchCol . ") AGAINST (? IN BOOLEAN MODE)";
76
				$query = $query->whereRaw($match, $keyword);
77
			}
78
		}
79
               
80
		return $query->orderByRaw($match . ' desc', $keyword);  
81
	}
82
}