Completed
Push — master ( 436c57...ad3aa0 )
by Denis
14s
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   +95 added lines, -95 removed lines patch added patch discarded remove patch
@@ -8,99 +8,99 @@
 block discarded – undo
8 8
 
9 9
 class EloquentDataProvider extends BaseDataProvider
10 10
 {
11
-    protected $filters = true;
12
-    protected $ordering = true;
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->reorder($request->sortColumn, $request->sortOrder);
75
-        }
76
-
77
-        return $query;
78
-    }
79
-
80
-    /**
81
-     * @inheritdoc
82
-     */
83
-    public function getCount(GridViewRequest $request) : int
84
-    {
85
-        $query = $this->baseQuery($request);
86
-        $query->reorder(null);
87
-
88
-        return $query->count();
89
-    }
90
-
91
-    /**
92
-     * @inheritdoc
93
-     */
94
-    public function getData(GridViewRequest $request)
95
-    {
96
-        $query = $this->baseQuery($request);
97
-
98
-        if ($request->perPage == 0) {
99
-            return $query->get();
100
-        }
101
-
102
-        return $query->offset(($request->page - 1) * $request->perPage)
103
-            ->limit($request->perPage)
104
-            ->get();
105
-    }
11
+	protected $filters = true;
12
+	protected $ordering = true;
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->reorder($request->sortColumn, $request->sortOrder);
75
+		}
76
+
77
+		return $query;
78
+	}
79
+
80
+	/**
81
+	 * @inheritdoc
82
+	 */
83
+	public function getCount(GridViewRequest $request) : int
84
+	{
85
+		$query = $this->baseQuery($request);
86
+		$query->reorder(null);
87
+
88
+		return $query->count();
89
+	}
90
+
91
+	/**
92
+	 * @inheritdoc
93
+	 */
94
+	public function getData(GridViewRequest $request)
95
+	{
96
+		$query = $this->baseQuery($request);
97
+
98
+		if ($request->perPage == 0) {
99
+			return $query->get();
100
+		}
101
+
102
+		return $query->offset(($request->page - 1) * $request->perPage)
103
+			->limit($request->perPage)
104
+			->get();
105
+	}
106 106
 }
Please login to merge, or discard this patch.