Completed
Push — master ( 69bb37...3ea6ae )
by Denis
01:17
created
src/DataProviders/EloquentDataProvider.php 1 patch
Indentation   +92 added lines, -92 removed lines patch added patch discarded remove patch
@@ -8,96 +8,96 @@
 block discarded – undo
8 8
 
9 9
 class EloquentDataProvider extends BaseDataProvider
10 10
 {
11
-    protected $filters = false;
12
-    protected $ordering = false;
13
-
14
-    protected $query;
15
-
16
-    /**
17
-     * EloquentDataProvider constructor.
18
-     * @param Builder $query
19
-     */
20
-    public function __construct(Builder $query)
21
-    {
22
-        $this->query = clone $query;
23
-    }
24
-
25
-    /**
26
-     * Applies filter to a column
27
-     * @param \Closure| $filter
28
-     * @param string $fieldName
29
-     * @param Builder $query
30
-     * @param mixed $value
31
-     * @return void
32
-     */
33
-    private function applyFilter($filter, string $fieldName, Builder $query, $value)
34
-    {
35
-        if (is_callable($filter)) {
36
-           $filter($query, $value);
37
-           return;
38
-        }
39
-
40
-        switch ($filter) {
41
-            case '=':
42
-                $query->where($fieldName, '=', $value);
43
-                break;
44
-
45
-            case 'like':
46
-                $query->where($fieldName, 'LIKE', '%' . $value . '%');
47
-                break;
48
-
49
-            default:
50
-                throw new GridViewConfigException('Unknown filter type: ' . $filter);
51
-        }
52
-    }
53
-
54
-    /**
55
-     * @param GridViewRequest $request
56
-     * @return Builder
57
-     */
58
-    protected function baseQuery(GridViewRequest $request)
59
-    {
60
-        $query = clone $this->query;
61
-
62
-        if ($this->filters !== false) {
63
-            foreach ($request->filters as $field => $value) {
64
-                if ($this->filters === true || in_array($field, $this->filters)) {
65
-                    $query->where($field, 'LIKE', '%' . $value . '%');
66
-
67
-                } elseif (!empty($this->filters[$field])) {
68
-                    $this->applyFilter($this->filters[$field], $field, $query, $value);
69
-                }
70
-            }
71
-        }
72
-
73
-        if ($request->sortColumn && ($this->ordering === true || in_array($request->sortColumn, $this->ordering))) {
74
-            $query->orderBy($request->sortColumn, $request->sortOrder);
75
-        }
76
-
77
-        return $query;
78
-    }
79
-
80
-    /**
81
-     * @inheritdoc
82
-     */
83
-    public function getCount(GridViewRequest $request) : int
84
-    {
85
-        return $this->baseQuery($request)->count();
86
-    }
87
-
88
-    /**
89
-     * @inheritdoc
90
-     */
91
-    public function getData(GridViewRequest $request)
92
-    {
93
-        $query = $this->baseQuery($request);
94
-
95
-        if ($request->perPage == 0) {
96
-            return $query->get();
97
-        }
98
-
99
-        return $query->offset(($request->page - 1) * $request->perPage)
100
-            ->limit($request->perPage)
101
-            ->get();
102
-    }
11
+	protected $filters = false;
12
+	protected $ordering = false;
13
+
14
+	protected $query;
15
+
16
+	/**
17
+	 * EloquentDataProvider constructor.
18
+	 * @param Builder $query
19
+	 */
20
+	public function __construct(Builder $query)
21
+	{
22
+		$this->query = clone $query;
23
+	}
24
+
25
+	/**
26
+	 * Applies filter to a column
27
+	 * @param \Closure| $filter
28
+	 * @param string $fieldName
29
+	 * @param Builder $query
30
+	 * @param mixed $value
31
+	 * @return void
32
+	 */
33
+	private function applyFilter($filter, string $fieldName, Builder $query, $value)
34
+	{
35
+		if (is_callable($filter)) {
36
+		   $filter($query, $value);
37
+		   return;
38
+		}
39
+
40
+		switch ($filter) {
41
+			case '=':
42
+				$query->where($fieldName, '=', $value);
43
+				break;
44
+
45
+			case 'like':
46
+				$query->where($fieldName, 'LIKE', '%' . $value . '%');
47
+				break;
48
+
49
+			default:
50
+				throw new GridViewConfigException('Unknown filter type: ' . $filter);
51
+		}
52
+	}
53
+
54
+	/**
55
+	 * @param GridViewRequest $request
56
+	 * @return Builder
57
+	 */
58
+	protected function baseQuery(GridViewRequest $request)
59
+	{
60
+		$query = clone $this->query;
61
+
62
+		if ($this->filters !== false) {
63
+			foreach ($request->filters as $field => $value) {
64
+				if ($this->filters === true || in_array($field, $this->filters)) {
65
+					$query->where($field, 'LIKE', '%' . $value . '%');
66
+
67
+				} elseif (!empty($this->filters[$field])) {
68
+					$this->applyFilter($this->filters[$field], $field, $query, $value);
69
+				}
70
+			}
71
+		}
72
+
73
+		if ($request->sortColumn && ($this->ordering === true || in_array($request->sortColumn, $this->ordering))) {
74
+			$query->orderBy($request->sortColumn, $request->sortOrder);
75
+		}
76
+
77
+		return $query;
78
+	}
79
+
80
+	/**
81
+	 * @inheritdoc
82
+	 */
83
+	public function getCount(GridViewRequest $request) : int
84
+	{
85
+		return $this->baseQuery($request)->count();
86
+	}
87
+
88
+	/**
89
+	 * @inheritdoc
90
+	 */
91
+	public function getData(GridViewRequest $request)
92
+	{
93
+		$query = $this->baseQuery($request);
94
+
95
+		if ($request->perPage == 0) {
96
+			return $query->get();
97
+		}
98
+
99
+		return $query->offset(($request->page - 1) * $request->perPage)
100
+			->limit($request->perPage)
101
+			->get();
102
+	}
103 103
 }
Please login to merge, or discard this patch.
src/DataProviders/BaseDataProvider.php 1 patch
Indentation   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -6,58 +6,58 @@
 block discarded – undo
6 6
 
7 7
 abstract class BaseDataProvider
8 8
 {
9
-    /**
10
-     * true means that all request filters are accepted, checks by default comparing (like)
11
-     * false means that filtering is not enabled in this dataprovider
12
-     * array should contain array of fields, available for filtering.
13
-     *      if key not specified, value should be a name of field, otherwise key - field name,
14
-     *      value - comparing type (=, like) or a callable function
15
-     * @var bool|array
16
-     */
17
-    protected $filters = false;
18
-
19
-    /**
20
-     * true means that all request sortings are accepted, checks by default sorting
21
-     * false means that ordering is not enabled in this dataprovider
22
-     * array should contain array of fields, available for filtering.
23
-     *      value should be a string
24
-     * @var bool|array
25
-     */
26
-    protected $ordering = false;
27
-
28
-    /**
29
-     * Should return total amount of rows
30
-     * @param GridViewRequest $request
31
-     * @return int
32
-     */
33
-    abstract public function getCount(GridViewRequest $request) : int;
34
-
35
-    /**
36
-     * Should return a list of data for current page
37
-     * @param GridViewRequest $request
38
-     * @return mixed
39
-     */
40
-    abstract public function getData(GridViewRequest $request);
41
-
42
-    /**
43
-     * Allows to set a list of fields, available for filtering
44
-     * @param array|boolean $filters
45
-     * @return $this
46
-     */
47
-    public function setFilters($filters)
48
-    {
49
-        $this->filters = $filters;
50
-        return $this;
51
-    }
52
-
53
-    /**
54
-     * Allows to set a list of ordering fields
55
-     * @param array|boolean $ordering
56
-     * @return $this
57
-     */
58
-    public function setOrdering($ordering)
59
-    {
60
-        $this->ordering = $ordering;
61
-        return $this;
62
-    }
9
+	/**
10
+	 * true means that all request filters are accepted, checks by default comparing (like)
11
+	 * false means that filtering is not enabled in this dataprovider
12
+	 * array should contain array of fields, available for filtering.
13
+	 *      if key not specified, value should be a name of field, otherwise key - field name,
14
+	 *      value - comparing type (=, like) or a callable function
15
+	 * @var bool|array
16
+	 */
17
+	protected $filters = false;
18
+
19
+	/**
20
+	 * true means that all request sortings are accepted, checks by default sorting
21
+	 * false means that ordering is not enabled in this dataprovider
22
+	 * array should contain array of fields, available for filtering.
23
+	 *      value should be a string
24
+	 * @var bool|array
25
+	 */
26
+	protected $ordering = false;
27
+
28
+	/**
29
+	 * Should return total amount of rows
30
+	 * @param GridViewRequest $request
31
+	 * @return int
32
+	 */
33
+	abstract public function getCount(GridViewRequest $request) : int;
34
+
35
+	/**
36
+	 * Should return a list of data for current page
37
+	 * @param GridViewRequest $request
38
+	 * @return mixed
39
+	 */
40
+	abstract public function getData(GridViewRequest $request);
41
+
42
+	/**
43
+	 * Allows to set a list of fields, available for filtering
44
+	 * @param array|boolean $filters
45
+	 * @return $this
46
+	 */
47
+	public function setFilters($filters)
48
+	{
49
+		$this->filters = $filters;
50
+		return $this;
51
+	}
52
+
53
+	/**
54
+	 * Allows to set a list of ordering fields
55
+	 * @param array|boolean $ordering
56
+	 * @return $this
57
+	 */
58
+	public function setOrdering($ordering)
59
+	{
60
+		$this->ordering = $ordering;
61
+		return $this;
62
+	}
63 63
 }
Please login to merge, or discard this patch.