Completed
Pull Request — master (#51)
by
unknown
46s
created
src/GridViewHelper.php 1 patch
Indentation   +142 added lines, -142 removed lines patch added patch discarded remove patch
@@ -25,146 +25,146 @@
 block discarded – undo
25 25
 
26 26
 class GridViewHelper
27 27
 {
28
-    /**
29
-     * A list of grid aliases
30
-     * @var array
31
-     */
32
-    private static $aliases = [
33
-        'column' => [
34
-            'attribute' => AttributeColumn::class,
35
-            'raw' => CallbackColumn::class,
36
-            'callback' => CallbackColumn::class,
37
-            'actions' => ActionsColumn::class,
38
-            'view' => ViewColumn::class,
39
-        ],
40
-        'formatter' => [
41
-            'email' => EmailFormatter::class,
42
-            'image' => ImageFormatter::class,
43
-            'text' => TextFormatter::class,
44
-            'url' => UrlFormatter::class,
45
-            'raw' => RawFormatter::class,
46
-            'currency' => CurrencyFormatter::class,
47
-            'number' => NumberFormatter::class,
48
-        ],
49
-        'filter' => [
50
-            'text' => TextFilter::class,
51
-            'dropdown' => DropdownFilter::class,
52
-        ],
53
-        'renderer' => [
54
-            'default' => DefaultRenderer::class,
55
-        ],
56
-        'action' => [
57
-            'delete' => DeleteAction::class,
58
-            'update' => EditAction::class,
59
-            'edit' => EditAction::class,
60
-            'show' => ShowAction::class,
61
-            'view' => ShowAction::class,
62
-            'action' => Action::class,
63
-        ]
64
-    ];
65
-
66
-    private function __construct() {}
67
-
68
-    /**
69
-     * Useful in case you want to register a new alias for your project
70
-     * @param string $context
71
-     * @param string $alias
72
-     * @param string $aliasTo
73
-     */
74
-    public static function registerAlias(string $context, string $alias, string $aliasTo)
75
-    {
76
-        self::$aliases[$context][$alias] = $aliasTo;
77
-    }
78
-
79
-    /**
80
-     * Allows to resolve class name by its alias
81
-     * @param string $context
82
-     * @param string $alias
83
-     * @return mixed
84
-     */
85
-    public static function resolveAlias(string $context, string $alias)
86
-    {
87
-        return self::$aliases[$context][$alias] ?? $alias;
88
-    }
89
-
90
-    /**
91
-     * Allows to convert options array to html string
92
-     * @param array $htmlOptions
93
-     * @param array $context - context is variables, which are allowed to use when property value calculated dynamically
94
-     * @return string
95
-     */
96
-    public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string
97
-    {
98
-        if (empty($htmlOptions)) {
99
-            return '';
100
-        }
101
-
102
-        $out = [];
103
-
104
-        foreach ($htmlOptions as $k => $v) {
105
-
106
-            if ($v instanceof \Closure) {
107
-                $v = call_user_func_array($v, $context);
108
-            }
109
-
110
-            $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"';
111
-        }
112
-
113
-        return implode(' ', $out);
114
-    }
115
-
116
-    /**
117
-     * Allows to make column title by it key or attribute name
118
-     * @param string|int $key
119
-     * @return string
120
-     */
121
-    public static function columnTitle($key) : string
122
-    {
123
-        if (is_numeric($key)) {
124
-            return 'Column';
125
-        }
126
-
127
-        return ucwords(
128
-            trim(
129
-                preg_replace_callback(
130
-                    '/([A-Z]|_|\.)/',
131
-                    function($word) {
132
-                        $word = $word[0];
133
-
134
-                        if ($word == '_' || $word == '.') {
135
-                            return ' ';
136
-                        }
137
-
138
-                        return ' ' . strtolower($word);
139
-                    },
140
-                    $key
141
-                )
142
-            )
143
-        );
144
-    }
145
-
146
-    /**
147
-     * Helper for internal purposes
148
-     * @param $id
149
-     * @param $component
150
-     * @return string
151
-     */
152
-    public static function gridIdFormatter($id, $component)
153
-    {
154
-        if ($id == 0) {
155
-            return $component;
156
-        }
157
-
158
-        return 'grid[' . $id . '][' . $component . ']';
159
-    }
160
-
161
-    /**
162
-     * Generates page url with all requested params from request
163
-     * @param $gridId
164
-     * @param $page
165
-     */
166
-    public static function pageUrl($gridId, $page)
167
-    {
168
-        return url()->current() . '?' . Arr::query([\Woo\GridView\GridViewHelper::gridIdFormatter($gridId, 'page') => $page] + request()->query());
169
-    }
28
+	/**
29
+	 * A list of grid aliases
30
+	 * @var array
31
+	 */
32
+	private static $aliases = [
33
+		'column' => [
34
+			'attribute' => AttributeColumn::class,
35
+			'raw' => CallbackColumn::class,
36
+			'callback' => CallbackColumn::class,
37
+			'actions' => ActionsColumn::class,
38
+			'view' => ViewColumn::class,
39
+		],
40
+		'formatter' => [
41
+			'email' => EmailFormatter::class,
42
+			'image' => ImageFormatter::class,
43
+			'text' => TextFormatter::class,
44
+			'url' => UrlFormatter::class,
45
+			'raw' => RawFormatter::class,
46
+			'currency' => CurrencyFormatter::class,
47
+			'number' => NumberFormatter::class,
48
+		],
49
+		'filter' => [
50
+			'text' => TextFilter::class,
51
+			'dropdown' => DropdownFilter::class,
52
+		],
53
+		'renderer' => [
54
+			'default' => DefaultRenderer::class,
55
+		],
56
+		'action' => [
57
+			'delete' => DeleteAction::class,
58
+			'update' => EditAction::class,
59
+			'edit' => EditAction::class,
60
+			'show' => ShowAction::class,
61
+			'view' => ShowAction::class,
62
+			'action' => Action::class,
63
+		]
64
+	];
65
+
66
+	private function __construct() {}
67
+
68
+	/**
69
+	 * Useful in case you want to register a new alias for your project
70
+	 * @param string $context
71
+	 * @param string $alias
72
+	 * @param string $aliasTo
73
+	 */
74
+	public static function registerAlias(string $context, string $alias, string $aliasTo)
75
+	{
76
+		self::$aliases[$context][$alias] = $aliasTo;
77
+	}
78
+
79
+	/**
80
+	 * Allows to resolve class name by its alias
81
+	 * @param string $context
82
+	 * @param string $alias
83
+	 * @return mixed
84
+	 */
85
+	public static function resolveAlias(string $context, string $alias)
86
+	{
87
+		return self::$aliases[$context][$alias] ?? $alias;
88
+	}
89
+
90
+	/**
91
+	 * Allows to convert options array to html string
92
+	 * @param array $htmlOptions
93
+	 * @param array $context - context is variables, which are allowed to use when property value calculated dynamically
94
+	 * @return string
95
+	 */
96
+	public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string
97
+	{
98
+		if (empty($htmlOptions)) {
99
+			return '';
100
+		}
101
+
102
+		$out = [];
103
+
104
+		foreach ($htmlOptions as $k => $v) {
105
+
106
+			if ($v instanceof \Closure) {
107
+				$v = call_user_func_array($v, $context);
108
+			}
109
+
110
+			$out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"';
111
+		}
112
+
113
+		return implode(' ', $out);
114
+	}
115
+
116
+	/**
117
+	 * Allows to make column title by it key or attribute name
118
+	 * @param string|int $key
119
+	 * @return string
120
+	 */
121
+	public static function columnTitle($key) : string
122
+	{
123
+		if (is_numeric($key)) {
124
+			return 'Column';
125
+		}
126
+
127
+		return ucwords(
128
+			trim(
129
+				preg_replace_callback(
130
+					'/([A-Z]|_|\.)/',
131
+					function($word) {
132
+						$word = $word[0];
133
+
134
+						if ($word == '_' || $word == '.') {
135
+							return ' ';
136
+						}
137
+
138
+						return ' ' . strtolower($word);
139
+					},
140
+					$key
141
+				)
142
+			)
143
+		);
144
+	}
145
+
146
+	/**
147
+	 * Helper for internal purposes
148
+	 * @param $id
149
+	 * @param $component
150
+	 * @return string
151
+	 */
152
+	public static function gridIdFormatter($id, $component)
153
+	{
154
+		if ($id == 0) {
155
+			return $component;
156
+		}
157
+
158
+		return 'grid[' . $id . '][' . $component . ']';
159
+	}
160
+
161
+	/**
162
+	 * Generates page url with all requested params from request
163
+	 * @param $gridId
164
+	 * @param $page
165
+	 */
166
+	public static function pageUrl($gridId, $page)
167
+	{
168
+		return url()->current() . '?' . Arr::query([\Woo\GridView\GridViewHelper::gridIdFormatter($gridId, 'page') => $page] + request()->query());
169
+	}
170 170
 }
Please login to merge, or discard this patch.
src/DataProviders/EloquentDataProvider.php 1 patch
Indentation   +101 added lines, -101 removed lines patch added patch discarded remove patch
@@ -9,105 +9,105 @@
 block discarded – undo
9 9
 
10 10
 class EloquentDataProvider extends BaseDataProvider
11 11
 {
12
-    protected $filters = true;
13
-    protected $ordering = true;
14
-
15
-    protected $query;
16
-
17
-    /**
18
-     * EloquentDataProvider constructor.
19
-     * @param Builder $query
20
-     */
21
-    public function __construct(Builder $query)
22
-    {
23
-        $this->query = clone $query;
24
-    }
25
-
26
-    /**
27
-     * Applies filter to a column
28
-     * @param \Closure| $filter
29
-     * @param string $fieldName
30
-     * @param Builder $query
31
-     * @param mixed $value
32
-     * @return void
33
-     */
34
-    private function applyFilter($filter, string $fieldName, Builder $query, $value)
35
-    {
36
-        if (is_callable($filter)) {
37
-           $filter($query, $value);
38
-           return;
39
-        }
40
-
41
-        switch ($filter) {
42
-            case '=':
43
-                $query->where($fieldName, '=', $value);
44
-                break;
45
-
46
-            case 'like':
47
-                $query->where($fieldName, 'LIKE', '%' . $value . '%');
48
-                break;
49
-
50
-            default:
51
-                throw new GridViewConfigException('Unknown filter type: ' . $filter);
52
-        }
53
-    }
54
-
55
-    /**
56
-     * @param GridViewRequest $request
57
-     * @return Builder
58
-     */
59
-    protected function baseQuery(GridViewRequest $request)
60
-    {
61
-        $query = clone $this->query;
62
-
63
-        if ($this->filters !== false) {
64
-            foreach ($request->filters as $field => $value) {
65
-                if ($this->filters === true || in_array($field, $this->filters)) {
66
-                    // Check if the $field is a real column in the table
67
-                    if (Schema::hasColumn($query->from, $field)) {
68
-                        // $field exists in the table, apply a WHERE clause
69
-                        $query->where($field, 'LIKE', '%' . $value . '%');
70
-                    } else {
71
-                        // $field does not exist in the table, it must be an alias, apply a HAVING clause
72
-                        $query->having($field, 'LIKE', '%' . $value . '%');
73
-                    }
74
-                } elseif (!empty($this->filters[$field])) {
75
-                    $this->applyFilter($this->filters[$field], $field, $query, $value);
76
-                }
77
-            }
78
-        }
79
-
80
-        if ($request->sortColumn && ($this->ordering === true || in_array($request->sortColumn, $this->ordering))) {
81
-            $query->reorder($request->sortColumn, $request->sortOrder);
82
-        }
83
-
84
-        return $query;
85
-    }
86
-
87
-    /**
88
-     * @inheritdoc
89
-     */
90
-    public function getCount(GridViewRequest $request) : int
91
-    {
92
-        $query = $this->baseQuery($request);
93
-        $query->reorder(null);
94
-
95
-        return $query->count();
96
-    }
97
-
98
-    /**
99
-     * @inheritdoc
100
-     */
101
-    public function getData(GridViewRequest $request)
102
-    {
103
-        $query = $this->baseQuery($request);
104
-
105
-        if ($request->perPage == 0) {
106
-            return $query->get();
107
-        }
108
-
109
-        return $query->offset(($request->page - 1) * $request->perPage)
110
-            ->limit($request->perPage)
111
-            ->get();
112
-    }
12
+	protected $filters = true;
13
+	protected $ordering = true;
14
+
15
+	protected $query;
16
+
17
+	/**
18
+	 * EloquentDataProvider constructor.
19
+	 * @param Builder $query
20
+	 */
21
+	public function __construct(Builder $query)
22
+	{
23
+		$this->query = clone $query;
24
+	}
25
+
26
+	/**
27
+	 * Applies filter to a column
28
+	 * @param \Closure| $filter
29
+	 * @param string $fieldName
30
+	 * @param Builder $query
31
+	 * @param mixed $value
32
+	 * @return void
33
+	 */
34
+	private function applyFilter($filter, string $fieldName, Builder $query, $value)
35
+	{
36
+		if (is_callable($filter)) {
37
+		   $filter($query, $value);
38
+		   return;
39
+		}
40
+
41
+		switch ($filter) {
42
+			case '=':
43
+				$query->where($fieldName, '=', $value);
44
+				break;
45
+
46
+			case 'like':
47
+				$query->where($fieldName, 'LIKE', '%' . $value . '%');
48
+				break;
49
+
50
+			default:
51
+				throw new GridViewConfigException('Unknown filter type: ' . $filter);
52
+		}
53
+	}
54
+
55
+	/**
56
+	 * @param GridViewRequest $request
57
+	 * @return Builder
58
+	 */
59
+	protected function baseQuery(GridViewRequest $request)
60
+	{
61
+		$query = clone $this->query;
62
+
63
+		if ($this->filters !== false) {
64
+			foreach ($request->filters as $field => $value) {
65
+				if ($this->filters === true || in_array($field, $this->filters)) {
66
+					// Check if the $field is a real column in the table
67
+					if (Schema::hasColumn($query->from, $field)) {
68
+						// $field exists in the table, apply a WHERE clause
69
+						$query->where($field, 'LIKE', '%' . $value . '%');
70
+					} else {
71
+						// $field does not exist in the table, it must be an alias, apply a HAVING clause
72
+						$query->having($field, 'LIKE', '%' . $value . '%');
73
+					}
74
+				} elseif (!empty($this->filters[$field])) {
75
+					$this->applyFilter($this->filters[$field], $field, $query, $value);
76
+				}
77
+			}
78
+		}
79
+
80
+		if ($request->sortColumn && ($this->ordering === true || in_array($request->sortColumn, $this->ordering))) {
81
+			$query->reorder($request->sortColumn, $request->sortOrder);
82
+		}
83
+
84
+		return $query;
85
+	}
86
+
87
+	/**
88
+	 * @inheritdoc
89
+	 */
90
+	public function getCount(GridViewRequest $request) : int
91
+	{
92
+		$query = $this->baseQuery($request);
93
+		$query->reorder(null);
94
+
95
+		return $query->count();
96
+	}
97
+
98
+	/**
99
+	 * @inheritdoc
100
+	 */
101
+	public function getData(GridViewRequest $request)
102
+	{
103
+		$query = $this->baseQuery($request);
104
+
105
+		if ($request->perPage == 0) {
106
+			return $query->get();
107
+		}
108
+
109
+		return $query->offset(($request->page - 1) * $request->perPage)
110
+			->limit($request->perPage)
111
+			->get();
112
+	}
113 113
 }
Please login to merge, or discard this patch.