Completed
Push — master ( 0182dc...017ca5 )
by Denis
02:13
created
src/GridViewRequest.php 1 patch
Indentation   +73 added lines, -73 removed lines patch added patch discarded remove patch
@@ -7,77 +7,77 @@
 block discarded – undo
7 7
 
8 8
 class GridViewRequest
9 9
 {
10
-    use Configurable;
11
-
12
-    public $page;
13
-
14
-    public $sortColumn;
15
-
16
-    public $sortOrder;
17
-
18
-    public $perPage;
19
-
20
-    public $filters = [];
21
-
22
-    private function __construct($config)
23
-    {
24
-        $this->loadConfig($config);
25
-    }
26
-
27
-    private static function gridField(int $gridId, string $field)
28
-    {
29
-        return $gridId == 0 ? $field : 'grid.' . $gridId . '.' . $field;
30
-    }
31
-
32
-    /**
33
-     * Allows to parse request information and making request instance
34
-     * @param int $gridId
35
-     * @return GridViewRequest
36
-     */
37
-    public static function parse(int $gridId)
38
-    {
39
-        $request = Request::instance();
40
-
41
-        $page = $request->input(self::gridField($gridId, 'page'), 1);
42
-        $sortColumn = $request->input(self::gridField($gridId, 'sort'), '');
43
-        $sortOrder = $request->input(self::gridField($gridId, 'order'), 'DESC');
44
-        $filters = $request->input(self::gridField($gridId, 'filters'), []);
45
-
46
-        if ($page <= 0) {
47
-            $page = 1;
48
-        }
49
-
50
-        if (!is_string($sortColumn)) {
51
-            $sortColumn = '';
52
-        }
53
-
54
-        if (!in_array($sortOrder, ['ASC', 'DESC'])) {
55
-            $sortOrder = 'DESC';
56
-        }
57
-
58
-        if (!is_array($filters)) {
59
-            $filters = [];
60
-        }
61
-
62
-        return new GridViewRequest([
63
-            'page' => $page,
64
-            'sortColumn' => $sortColumn,
65
-            'sortOrder' => $sortOrder,
66
-            'filters' => $filters,
67
-        ]);
68
-    }
69
-
70
-    public function getFilterValue(string $name)
71
-    {
72
-        return $this->filters[$name] ?? '';
73
-    }
74
-
75
-    /**
76
-     * Should specify tests
77
-     * @return array
78
-     */
79
-    protected function configTests(): array
80
-    {
81
-        return [];
82
-    }
10
+	use Configurable;
11
+
12
+	public $page;
13
+
14
+	public $sortColumn;
15
+
16
+	public $sortOrder;
17
+
18
+	public $perPage;
19
+
20
+	public $filters = [];
21
+
22
+	private function __construct($config)
23
+	{
24
+		$this->loadConfig($config);
25
+	}
26
+
27
+	private static function gridField(int $gridId, string $field)
28
+	{
29
+		return $gridId == 0 ? $field : 'grid.' . $gridId . '.' . $field;
30
+	}
31
+
32
+	/**
33
+	 * Allows to parse request information and making request instance
34
+	 * @param int $gridId
35
+	 * @return GridViewRequest
36
+	 */
37
+	public static function parse(int $gridId)
38
+	{
39
+		$request = Request::instance();
40
+
41
+		$page = $request->input(self::gridField($gridId, 'page'), 1);
42
+		$sortColumn = $request->input(self::gridField($gridId, 'sort'), '');
43
+		$sortOrder = $request->input(self::gridField($gridId, 'order'), 'DESC');
44
+		$filters = $request->input(self::gridField($gridId, 'filters'), []);
45
+
46
+		if ($page <= 0) {
47
+			$page = 1;
48
+		}
49
+
50
+		if (!is_string($sortColumn)) {
51
+			$sortColumn = '';
52
+		}
53
+
54
+		if (!in_array($sortOrder, ['ASC', 'DESC'])) {
55
+			$sortOrder = 'DESC';
56
+		}
57
+
58
+		if (!is_array($filters)) {
59
+			$filters = [];
60
+		}
61
+
62
+		return new GridViewRequest([
63
+			'page' => $page,
64
+			'sortColumn' => $sortColumn,
65
+			'sortOrder' => $sortOrder,
66
+			'filters' => $filters,
67
+		]);
68
+	}
69
+
70
+	public function getFilterValue(string $name)
71
+	{
72
+		return $this->filters[$name] ?? '';
73
+	}
74
+
75
+	/**
76
+	 * Should specify tests
77
+	 * @return array
78
+	 */
79
+	protected function configTests(): array
80
+	{
81
+		return [];
82
+	}
83 83
 }
84 84
\ No newline at end of file
Please login to merge, or discard this patch.
src/DataProviders/EloquentDataProvider.php 1 patch
Indentation   +53 added lines, -53 removed lines patch added patch discarded remove patch
@@ -7,57 +7,57 @@
 block discarded – undo
7 7
 
8 8
 class EloquentDataProvider extends BaseDataProvider
9 9
 {
10
-    protected $query;
11
-
12
-    /**
13
-     * EloquentDataProvider constructor.
14
-     * @param Builder $query
15
-     */
16
-    public function __construct(Builder $query)
17
-    {
18
-        $this->query = clone $query;
19
-    }
20
-
21
-    /**
22
-     * @param GridViewRequest $request
23
-     * @return Builder
24
-     */
25
-    protected function baseQuery(GridViewRequest $request)
26
-    {
27
-        $query = clone $this->query;
28
-
29
-        foreach ($request->filters as $field => $value) {
30
-            $query->where($field, 'LIKE', '%' . $value . '%');
31
-        }
32
-
33
-        if ($request->sortColumn) {
34
-            $query->orderBy($request->sortColumn, $request->sortOrder);
35
-        }
36
-
37
-        return $query;
38
-    }
39
-
40
-    /**
41
-     * @inheritdoc
42
-     */
43
-    public function getCount(GridViewRequest $request) : int
44
-    {
45
-        return $this->baseQuery($request)->count();
46
-    }
47
-
48
-    /**
49
-     * @inheritdoc
50
-     */
51
-    public function getData(GridViewRequest $request)
52
-    {
53
-        $query = $this->baseQuery($request);
54
-
55
-        if ($request->perPage == 0) {
56
-            return $query->get();
57
-        }
58
-
59
-        return $query->offset(($request->page - 1) * $request->perPage)
60
-            ->limit($request->perPage)
61
-            ->get();
62
-    }
10
+	protected $query;
11
+
12
+	/**
13
+	 * EloquentDataProvider constructor.
14
+	 * @param Builder $query
15
+	 */
16
+	public function __construct(Builder $query)
17
+	{
18
+		$this->query = clone $query;
19
+	}
20
+
21
+	/**
22
+	 * @param GridViewRequest $request
23
+	 * @return Builder
24
+	 */
25
+	protected function baseQuery(GridViewRequest $request)
26
+	{
27
+		$query = clone $this->query;
28
+
29
+		foreach ($request->filters as $field => $value) {
30
+			$query->where($field, 'LIKE', '%' . $value . '%');
31
+		}
32
+
33
+		if ($request->sortColumn) {
34
+			$query->orderBy($request->sortColumn, $request->sortOrder);
35
+		}
36
+
37
+		return $query;
38
+	}
39
+
40
+	/**
41
+	 * @inheritdoc
42
+	 */
43
+	public function getCount(GridViewRequest $request) : int
44
+	{
45
+		return $this->baseQuery($request)->count();
46
+	}
47
+
48
+	/**
49
+	 * @inheritdoc
50
+	 */
51
+	public function getData(GridViewRequest $request)
52
+	{
53
+		$query = $this->baseQuery($request);
54
+
55
+		if ($request->perPage == 0) {
56
+			return $query->get();
57
+		}
58
+
59
+		return $query->offset(($request->page - 1) * $request->perPage)
60
+			->limit($request->perPage)
61
+			->get();
62
+	}
63 63
 }
64 64
\ No newline at end of file
Please login to merge, or discard this patch.
src/DataProviders/BaseDataProvider.php 1 patch
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -6,17 +6,17 @@
 block discarded – undo
6 6
 
7 7
 abstract class BaseDataProvider
8 8
 {
9
-    /**
10
-     * Should return total amount of rows
11
-     * @param GridViewRequest $request
12
-     * @return int
13
-     */
14
-    abstract public function getCount(GridViewRequest $request) : int;
9
+	/**
10
+	 * Should return total amount of rows
11
+	 * @param GridViewRequest $request
12
+	 * @return int
13
+	 */
14
+	abstract public function getCount(GridViewRequest $request) : int;
15 15
 
16
-    /**
17
-     * Should return a list of data for current page
18
-     * @param GridViewRequest $request
19
-     * @return mixed
20
-     */
21
-    abstract public function getData(GridViewRequest $request);
16
+	/**
17
+	 * Should return a list of data for current page
18
+	 * @param GridViewRequest $request
19
+	 * @return mixed
20
+	 */
21
+	abstract public function getData(GridViewRequest $request);
22 22
 }
23 23
\ No newline at end of file
Please login to merge, or discard this patch.
src/DataProviders/ArrayDataProvider.php 2 patches
Indentation   +58 added lines, -58 removed lines patch added patch discarded remove patch
@@ -6,74 +6,74 @@
 block discarded – undo
6 6
 
7 7
 class ArrayDataProvider extends BaseDataProvider
8 8
 {
9
-    /**
10
-     * @var array
11
-     */
12
-    private $data;
9
+	/**
10
+	 * @var array
11
+	 */
12
+	private $data;
13 13
 
14
-    public function __construct(array $data)
15
-    {
16
-        $this->data = $data;
17
-    }
14
+	public function __construct(array $data)
15
+	{
16
+		$this->data = $data;
17
+	}
18 18
 
19
-    /**
20
-     * @param GridViewRequest $request
21
-     * @return array
22
-     */
23
-    protected function processData(GridViewRequest $request) : array
24
-    {
25
-        if (empty($this->data)) {
26
-            return [];
27
-        }
19
+	/**
20
+	 * @param GridViewRequest $request
21
+	 * @return array
22
+	 */
23
+	protected function processData(GridViewRequest $request) : array
24
+	{
25
+		if (empty($this->data)) {
26
+			return [];
27
+		}
28 28
 
29
-        $tmp = collect($this->data);
29
+		$tmp = collect($this->data);
30 30
 
31
-        if (!empty($request->filters)) {
32
-            $tmp = $tmp->filter(function($item) use ($request) {
33
-                foreach ($request->filters as $filterKey => $filterValue) {
31
+		if (!empty($request->filters)) {
32
+			$tmp = $tmp->filter(function($item) use ($request) {
33
+				foreach ($request->filters as $filterKey => $filterValue) {
34 34
 
35
-                    if (!isset($item[$filterKey])) {
36
-                        return false;
37
-                    }
35
+					if (!isset($item[$filterKey])) {
36
+						return false;
37
+					}
38 38
 
39
-                    if (strpos($item[$filterKey], $filterValue) === false) {
40
-                        return false;
41
-                    }
42
-                }
39
+					if (strpos($item[$filterKey], $filterValue) === false) {
40
+						return false;
41
+					}
42
+				}
43 43
 
44
-                return true;
45
-            });
46
-        }
44
+				return true;
45
+			});
46
+		}
47 47
 
48
-        if (!empty($request->sortColumn)) {
49
-            $tmp = $tmp->sortBy(
50
-                $request->sortColumn,
51
-                SORT_REGULAR,
52
-                $request->sortOrder == 'DESC'
53
-            );
54
-        }
48
+		if (!empty($request->sortColumn)) {
49
+			$tmp = $tmp->sortBy(
50
+				$request->sortColumn,
51
+				SORT_REGULAR,
52
+				$request->sortOrder == 'DESC'
53
+			);
54
+		}
55 55
 
56
-        return $tmp->values()->all();
57
-    }
56
+		return $tmp->values()->all();
57
+	}
58 58
 
59
-    /**
60
-     * @inheritdoc
61
-     */
62
-    public function getCount(GridViewRequest $request) : int
63
-    {
64
-        return count($this->processData($request));
65
-    }
59
+	/**
60
+	 * @inheritdoc
61
+	 */
62
+	public function getCount(GridViewRequest $request) : int
63
+	{
64
+		return count($this->processData($request));
65
+	}
66 66
 
67
-    /**
68
-     * @inheritdoc
69
-     */
70
-    public function getData(GridViewRequest $request)
71
-    {
72
-        $tmp = $this->processData($request);
67
+	/**
68
+	 * @inheritdoc
69
+	 */
70
+	public function getData(GridViewRequest $request)
71
+	{
72
+		$tmp = $this->processData($request);
73 73
 
74
-        return array_splice(
75
-            $tmp,
76
-            ($request->page -1) * $request->perPage, $request->perPage
77
-        );
78
-    }
74
+		return array_splice(
75
+			$tmp,
76
+			($request->page -1) * $request->perPage, $request->perPage
77
+		);
78
+	}
79 79
 }
80 80
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -73,7 +73,7 @@
 block discarded – undo
73 73
 
74 74
         return array_splice(
75 75
             $tmp,
76
-            ($request->page -1) * $request->perPage, $request->perPage
76
+            ($request->page - 1) * $request->perPage, $request->perPage
77 77
         );
78 78
     }
79 79
 }
80 80
\ No newline at end of file
Please login to merge, or discard this patch.
src/GridViewServiceProvider.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -19,7 +19,7 @@
 block discarded – undo
19 19
 
20 20
 		require_once __DIR__ . '/functions.php';
21 21
 
22
-        \Blade::directive('grid', function ($expression) {
22
+        \Blade::directive('grid', function($expression) {
23 23
             return "<?php echo grid($expression) ?>";
24 24
         });
25 25
 
Please login to merge, or discard this patch.
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -19,17 +19,17 @@
 block discarded – undo
19 19
 
20 20
 		require_once __DIR__ . '/functions.php';
21 21
 
22
-        \Blade::directive('grid', function ($expression) {
23
-            return "<?php echo grid($expression) ?>";
24
-        });
22
+		\Blade::directive('grid', function ($expression) {
23
+			return "<?php echo grid($expression) ?>";
24
+		});
25 25
 
26
-        $this->publishes([
27
-            __DIR__ . '/../public' => 'public/vendor/grid-view',
28
-        ], 'public');
26
+		$this->publishes([
27
+			__DIR__ . '/../public' => 'public/vendor/grid-view',
28
+		], 'public');
29 29
 
30
-        if (!File::isDirectory(public_path('vendor/grid-view'))) {
31
-            Artisan::call('vendor:publish', ['--tag' => 'public', '--force' => '']);
32
-        }
30
+		if (!File::isDirectory(public_path('vendor/grid-view'))) {
31
+			Artisan::call('vendor:publish', ['--tag' => 'public', '--force' => '']);
32
+		}
33 33
 	}
34 34
 
35 35
 	public function register()
Please login to merge, or discard this patch.
src/GridView.php 1 patch
Indentation   +216 added lines, -216 removed lines patch added patch discarded remove patch
@@ -15,220 +15,220 @@
 block discarded – undo
15 15
 
16 16
 class GridView
17 17
 {
18
-    use Configurable;
19
-
20
-    /**
21
-     * Counter for ids
22
-     * @var int
23
-     */
24
-    private static $counter = 0;
25
-
26
-    /**
27
-     * Grid id (used for request handling, for
28
-     * @var int
29
-     */
30
-    private $id;
31
-
32
-    /**
33
-     * DataProvider provides gridview with the data for representation
34
-     * @var BaseDataProvider
35
-     */
36
-    public $dataProvider;
37
-
38
-    /**
39
-     * Columns config. You may specify array or GridColumn instance
40
-     * @var BaseColumn[]
41
-     */
42
-    public $columns = [];
43
-
44
-    /**
45
-     * Common options for all columns, will be appended to all columns configs
46
-     * @var array
47
-     */
48
-    public $columnOptions = [
49
-        'class' => AttributeColumn::class,
50
-    ];
51
-
52
-    /**
53
-     * Renders the final UI
54
-     * @var string|BaseRenderer
55
-     */
56
-    public $renderer = DefaultRenderer::class;
57
-
58
-    /**
59
-     * Allows to pass some options into renderer/customize rendered behavior
60
-     * @var array
61
-     */
62
-    public $rendererOptions = [];
63
-
64
-    /**
65
-     * Controls amount of data per page
66
-     * @var int
67
-     */
68
-    public $rowsPerPage = 25;
69
-
70
-    /**
71
-     * Allows to tune the <table> tag with html options
72
-     * @var array
73
-     */
74
-    public $tableHtmlOptions = [
75
-        'class' => 'table table-bordered gridview-table',
76
-    ];
77
-
78
-    /**
79
-     * Indicate if filters will be shown or not
80
-     * @var bool
81
-     */
82
-    public $showFilters = true;
83
-
84
-    /**
85
-     * @var Paginator
86
-     */
87
-    protected $pagination;
88
-
89
-    /**
90
-     * @var GridViewRequest
91
-     */
92
-    protected $request;
93
-
94
-    /**
95
-     * GridView constructor.
96
-     * @param array $config
97
-     * @throws Exceptions\GridViewConfigException
98
-     */
99
-    public function __construct(array $config)
100
-    {
101
-        $this->id = self::$counter++;
102
-
103
-        $this->loadConfig($config);
104
-
105
-        /**
106
-         * Making renderer
107
-         */
108
-        if (!is_object($this->renderer)) {
109
-            $className = GridViewHelper::resolveAlias('renderer', $this->renderer);
110
-            $this->renderer = new $className(array_merge(
111
-                $this->rendererOptions, [
112
-                    'gridView' => $this,
113
-                ]
114
-            ));
115
-        }
116
-
117
-        /**
118
-         * Build columns from config
119
-         */
120
-        $this->buildColumns();
121
-
122
-        $this->request = GridViewRequest::parse($this->id);
123
-        $this->request->perPage = $this->rowsPerPage;
124
-
125
-        $this->pagination = new LengthAwarePaginator(
126
-            $this->dataProvider->getData($this->request),
127
-            $this->dataProvider->getCount($this->request),
128
-            $this->rowsPerPage,
129
-            $this->request->page
130
-        );
131
-    }
132
-
133
-    /**
134
-     * @return array
135
-     */
136
-    protected function configTests(): array
137
-    {
138
-        return [
139
-            'dataProvider' => BaseDataProvider::class,
140
-            'columns' => 'array',
141
-            'renderer' => BaseRenderer::class,
142
-            'rowsPerPage' => 'int',
143
-            'tableHtmlOptions' => 'array',
144
-            'showFilters' => 'boolean',
145
-        ];
146
-    }
147
-
148
-    /**
149
-     * Build columns into objects
150
-     */
151
-    protected function buildColumns()
152
-    {
153
-        foreach ($this->columns as $key => &$columnOptions) {
154
-
155
-            /**
156
-             * In case of when column is already build
157
-             */
158
-            if (is_object($columnOptions)) {
159
-                continue;
160
-            }
161
-
162
-            /**
163
-             * When only attribute name/value passed
164
-             */
165
-            if (is_string($columnOptions)) {
166
-                $columnOptions = [
167
-                    'value' => $columnOptions,
168
-                ];
169
-            }
170
-
171
-            if ($columnOptions instanceof Closure) {
172
-                $columnOptions = [
173
-                    'class' => CallbackColumn::class,
174
-                    'value' => $columnOptions,
175
-                    'title' => GridViewHelper::columnTitle($key),
176
-                ];
177
-            }
178
-
179
-            /**
180
-             * Inline column declaration detector
181
-             */
182
-            if (is_string($columnOptions['value']) && strpos($columnOptions['value'], 'view:') === 0) {
183
-                $columnOptions['class'] = 'view';
184
-                $columnOptions['value'] = str_replace('view:', '', $columnOptions['value']);
185
-            }
186
-
187
-            $columnOptions = array_merge($this->columnOptions, $columnOptions);
188
-
189
-            $className = GridViewHelper::resolveAlias('column', $columnOptions['class']);
190
-            $columnOptions = new $className($columnOptions);
191
-        }
192
-    }
193
-
194
-    /**
195
-     * Draws widget and return html code
196
-     * @return string
197
-     */
198
-    public function render()
199
-    {
200
-        return $this->renderer->render($this);
201
-    }
202
-
203
-    /**
204
-     * @return LengthAwarePaginator|Paginator
205
-     */
206
-    public function getPagination()
207
-    {
208
-        return $this->pagination;
209
-    }
210
-
211
-    /**
212
-     * @return GridViewRequest
213
-     */
214
-    public function getRequest()
215
-    {
216
-        return $this->request;
217
-    }
218
-
219
-    /**
220
-     * @return int
221
-     */
222
-    public function getId() : int
223
-    {
224
-        return $this->id;
225
-    }
226
-
227
-    /**
228
-     * @return string
229
-     */
230
-    public function compileTableHtmlOptions() : string
231
-    {
232
-        return GridViewHelper::htmlOptionsToString($this->tableHtmlOptions);
233
-    }
18
+	use Configurable;
19
+
20
+	/**
21
+	 * Counter for ids
22
+	 * @var int
23
+	 */
24
+	private static $counter = 0;
25
+
26
+	/**
27
+	 * Grid id (used for request handling, for
28
+	 * @var int
29
+	 */
30
+	private $id;
31
+
32
+	/**
33
+	 * DataProvider provides gridview with the data for representation
34
+	 * @var BaseDataProvider
35
+	 */
36
+	public $dataProvider;
37
+
38
+	/**
39
+	 * Columns config. You may specify array or GridColumn instance
40
+	 * @var BaseColumn[]
41
+	 */
42
+	public $columns = [];
43
+
44
+	/**
45
+	 * Common options for all columns, will be appended to all columns configs
46
+	 * @var array
47
+	 */
48
+	public $columnOptions = [
49
+		'class' => AttributeColumn::class,
50
+	];
51
+
52
+	/**
53
+	 * Renders the final UI
54
+	 * @var string|BaseRenderer
55
+	 */
56
+	public $renderer = DefaultRenderer::class;
57
+
58
+	/**
59
+	 * Allows to pass some options into renderer/customize rendered behavior
60
+	 * @var array
61
+	 */
62
+	public $rendererOptions = [];
63
+
64
+	/**
65
+	 * Controls amount of data per page
66
+	 * @var int
67
+	 */
68
+	public $rowsPerPage = 25;
69
+
70
+	/**
71
+	 * Allows to tune the <table> tag with html options
72
+	 * @var array
73
+	 */
74
+	public $tableHtmlOptions = [
75
+		'class' => 'table table-bordered gridview-table',
76
+	];
77
+
78
+	/**
79
+	 * Indicate if filters will be shown or not
80
+	 * @var bool
81
+	 */
82
+	public $showFilters = true;
83
+
84
+	/**
85
+	 * @var Paginator
86
+	 */
87
+	protected $pagination;
88
+
89
+	/**
90
+	 * @var GridViewRequest
91
+	 */
92
+	protected $request;
93
+
94
+	/**
95
+	 * GridView constructor.
96
+	 * @param array $config
97
+	 * @throws Exceptions\GridViewConfigException
98
+	 */
99
+	public function __construct(array $config)
100
+	{
101
+		$this->id = self::$counter++;
102
+
103
+		$this->loadConfig($config);
104
+
105
+		/**
106
+		 * Making renderer
107
+		 */
108
+		if (!is_object($this->renderer)) {
109
+			$className = GridViewHelper::resolveAlias('renderer', $this->renderer);
110
+			$this->renderer = new $className(array_merge(
111
+				$this->rendererOptions, [
112
+					'gridView' => $this,
113
+				]
114
+			));
115
+		}
116
+
117
+		/**
118
+		 * Build columns from config
119
+		 */
120
+		$this->buildColumns();
121
+
122
+		$this->request = GridViewRequest::parse($this->id);
123
+		$this->request->perPage = $this->rowsPerPage;
124
+
125
+		$this->pagination = new LengthAwarePaginator(
126
+			$this->dataProvider->getData($this->request),
127
+			$this->dataProvider->getCount($this->request),
128
+			$this->rowsPerPage,
129
+			$this->request->page
130
+		);
131
+	}
132
+
133
+	/**
134
+	 * @return array
135
+	 */
136
+	protected function configTests(): array
137
+	{
138
+		return [
139
+			'dataProvider' => BaseDataProvider::class,
140
+			'columns' => 'array',
141
+			'renderer' => BaseRenderer::class,
142
+			'rowsPerPage' => 'int',
143
+			'tableHtmlOptions' => 'array',
144
+			'showFilters' => 'boolean',
145
+		];
146
+	}
147
+
148
+	/**
149
+	 * Build columns into objects
150
+	 */
151
+	protected function buildColumns()
152
+	{
153
+		foreach ($this->columns as $key => &$columnOptions) {
154
+
155
+			/**
156
+			 * In case of when column is already build
157
+			 */
158
+			if (is_object($columnOptions)) {
159
+				continue;
160
+			}
161
+
162
+			/**
163
+			 * When only attribute name/value passed
164
+			 */
165
+			if (is_string($columnOptions)) {
166
+				$columnOptions = [
167
+					'value' => $columnOptions,
168
+				];
169
+			}
170
+
171
+			if ($columnOptions instanceof Closure) {
172
+				$columnOptions = [
173
+					'class' => CallbackColumn::class,
174
+					'value' => $columnOptions,
175
+					'title' => GridViewHelper::columnTitle($key),
176
+				];
177
+			}
178
+
179
+			/**
180
+			 * Inline column declaration detector
181
+			 */
182
+			if (is_string($columnOptions['value']) && strpos($columnOptions['value'], 'view:') === 0) {
183
+				$columnOptions['class'] = 'view';
184
+				$columnOptions['value'] = str_replace('view:', '', $columnOptions['value']);
185
+			}
186
+
187
+			$columnOptions = array_merge($this->columnOptions, $columnOptions);
188
+
189
+			$className = GridViewHelper::resolveAlias('column', $columnOptions['class']);
190
+			$columnOptions = new $className($columnOptions);
191
+		}
192
+	}
193
+
194
+	/**
195
+	 * Draws widget and return html code
196
+	 * @return string
197
+	 */
198
+	public function render()
199
+	{
200
+		return $this->renderer->render($this);
201
+	}
202
+
203
+	/**
204
+	 * @return LengthAwarePaginator|Paginator
205
+	 */
206
+	public function getPagination()
207
+	{
208
+		return $this->pagination;
209
+	}
210
+
211
+	/**
212
+	 * @return GridViewRequest
213
+	 */
214
+	public function getRequest()
215
+	{
216
+		return $this->request;
217
+	}
218
+
219
+	/**
220
+	 * @return int
221
+	 */
222
+	public function getId() : int
223
+	{
224
+		return $this->id;
225
+	}
226
+
227
+	/**
228
+	 * @return string
229
+	 */
230
+	public function compileTableHtmlOptions() : string
231
+	{
232
+		return GridViewHelper::htmlOptionsToString($this->tableHtmlOptions);
233
+	}
234 234
 }
235 235
\ No newline at end of file
Please login to merge, or discard this patch.
src/functions.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -7,5 +7,5 @@
 block discarded – undo
7 7
  * @throws \Woo\GridView\Exceptions\GridViewConfigException
8 8
  */
9 9
 function grid(array $config) {
10
-    return (new \Woo\GridView\GridView($config))->render();
10
+	return (new \Woo\GridView\GridView($config))->render();
11 11
 }
12 12
\ No newline at end of file
Please login to merge, or discard this patch.
src/GridViewHelper.php 1 patch
Indentation   +129 added lines, -129 removed lines patch added patch discarded remove patch
@@ -21,133 +21,133 @@
 block discarded – undo
21 21
 
22 22
 class GridViewHelper
23 23
 {
24
-    /**
25
-     * A list of grid aliases
26
-     * @var array
27
-     */
28
-    private static $aliases = [
29
-        'column' => [
30
-            'attribute' => AttributeColumn::class,
31
-            'raw' => CallbackColumn::class,
32
-            'callback' => CallbackColumn::class,
33
-            'actions' => ActionsColumn::class,
34
-            'view' => ViewColumn::class,
35
-        ],
36
-        'formatter' => [
37
-            'email' => EmailFormatter::class,
38
-            'image' => ImageFormatter::class,
39
-            'text' => TextFormatter::class,
40
-            'url' => UrlFormatter::class,
41
-        ],
42
-        'filter' => [
43
-            'text' => TextFilter::class,
44
-            'dropdown' => DropdownFilter::class,
45
-        ],
46
-        'renderer' => [
47
-            'default' => DefaultRenderer::class,
48
-        ],
49
-        'action' => [
50
-            'delete' => DeleteAction::class,
51
-            'update' => EditAction::class,
52
-            'edit' => EditAction::class,
53
-            'show' => ShowAction::class,
54
-            'view' => ShowAction::class,
55
-            'action' => Action::class,
56
-        ]
57
-    ];
58
-
59
-    private function __construct() {}
60
-
61
-    /**
62
-     * Useful in case you want to register a new alias for your project
63
-     * @param string $context
64
-     * @param string $alias
65
-     * @param string $aliasTo
66
-     */
67
-    public static function registerAlias(string $context, string $alias, string $aliasTo)
68
-    {
69
-        self::$aliases[$context][$alias] = $aliasTo;
70
-    }
71
-
72
-    /**
73
-     * Allows to resolve class name by its alias
74
-     * @param string $context
75
-     * @param string $alias
76
-     * @return mixed
77
-     */
78
-    public static function resolveAlias(string $context, string $alias)
79
-    {
80
-        return self::$aliases[$context][$alias] ?? $alias;
81
-    }
82
-
83
-    /**
84
-     * Allows to convert options array to html string
85
-     * @param array $htmlOptions
86
-     * @param array $context - context is variables, which are allowed to use when property value calculated dynamically
87
-     * @return string
88
-     */
89
-    public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string
90
-    {
91
-        if (empty($htmlOptions)) {
92
-            return '';
93
-        }
94
-
95
-        $out = [];
96
-
97
-        foreach ($htmlOptions as $k => $v) {
98
-
99
-            if ($v instanceof \Closure) {
100
-                $v = call_user_func_array($v, $context);
101
-            }
102
-
103
-            $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"';
104
-        }
105
-
106
-        return implode(' ', $out);
107
-    }
108
-
109
-    /**
110
-     * Allows to make column title by it key or attribute name
111
-     * @param string|int $key
112
-     * @return string
113
-     */
114
-    public static function columnTitle($key) : string
115
-    {
116
-        if (is_numeric($key)) {
117
-            return 'Column';
118
-        }
119
-
120
-        return ucwords(
121
-            trim(
122
-                preg_replace_callback(
123
-                    '/([A-Z]|_|\.)/',
124
-                    function($word) {
125
-                        $word = $word[0];
126
-
127
-                        if ($word == '_' || $word == '.') {
128
-                            return ' ';
129
-                        }
130
-
131
-                        return ' ' . strtolower($word);
132
-                    },
133
-                    $key
134
-                )
135
-            )
136
-        );
137
-    }
138
-
139
-    /**
140
-     * Helper for internal purposes
141
-     * @param $id
142
-     * @param $component
143
-     * @return string
144
-     */
145
-    public static function gridIdFormatter($id, $component)
146
-    {
147
-        if ($id == 0) {
148
-            return $component;
149
-        }
150
-
151
-        return 'grid[' . $id . '][' . $component . ']';
152
-    }
24
+	/**
25
+	 * A list of grid aliases
26
+	 * @var array
27
+	 */
28
+	private static $aliases = [
29
+		'column' => [
30
+			'attribute' => AttributeColumn::class,
31
+			'raw' => CallbackColumn::class,
32
+			'callback' => CallbackColumn::class,
33
+			'actions' => ActionsColumn::class,
34
+			'view' => ViewColumn::class,
35
+		],
36
+		'formatter' => [
37
+			'email' => EmailFormatter::class,
38
+			'image' => ImageFormatter::class,
39
+			'text' => TextFormatter::class,
40
+			'url' => UrlFormatter::class,
41
+		],
42
+		'filter' => [
43
+			'text' => TextFilter::class,
44
+			'dropdown' => DropdownFilter::class,
45
+		],
46
+		'renderer' => [
47
+			'default' => DefaultRenderer::class,
48
+		],
49
+		'action' => [
50
+			'delete' => DeleteAction::class,
51
+			'update' => EditAction::class,
52
+			'edit' => EditAction::class,
53
+			'show' => ShowAction::class,
54
+			'view' => ShowAction::class,
55
+			'action' => Action::class,
56
+		]
57
+	];
58
+
59
+	private function __construct() {}
60
+
61
+	/**
62
+	 * Useful in case you want to register a new alias for your project
63
+	 * @param string $context
64
+	 * @param string $alias
65
+	 * @param string $aliasTo
66
+	 */
67
+	public static function registerAlias(string $context, string $alias, string $aliasTo)
68
+	{
69
+		self::$aliases[$context][$alias] = $aliasTo;
70
+	}
71
+
72
+	/**
73
+	 * Allows to resolve class name by its alias
74
+	 * @param string $context
75
+	 * @param string $alias
76
+	 * @return mixed
77
+	 */
78
+	public static function resolveAlias(string $context, string $alias)
79
+	{
80
+		return self::$aliases[$context][$alias] ?? $alias;
81
+	}
82
+
83
+	/**
84
+	 * Allows to convert options array to html string
85
+	 * @param array $htmlOptions
86
+	 * @param array $context - context is variables, which are allowed to use when property value calculated dynamically
87
+	 * @return string
88
+	 */
89
+	public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string
90
+	{
91
+		if (empty($htmlOptions)) {
92
+			return '';
93
+		}
94
+
95
+		$out = [];
96
+
97
+		foreach ($htmlOptions as $k => $v) {
98
+
99
+			if ($v instanceof \Closure) {
100
+				$v = call_user_func_array($v, $context);
101
+			}
102
+
103
+			$out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"';
104
+		}
105
+
106
+		return implode(' ', $out);
107
+	}
108
+
109
+	/**
110
+	 * Allows to make column title by it key or attribute name
111
+	 * @param string|int $key
112
+	 * @return string
113
+	 */
114
+	public static function columnTitle($key) : string
115
+	{
116
+		if (is_numeric($key)) {
117
+			return 'Column';
118
+		}
119
+
120
+		return ucwords(
121
+			trim(
122
+				preg_replace_callback(
123
+					'/([A-Z]|_|\.)/',
124
+					function($word) {
125
+						$word = $word[0];
126
+
127
+						if ($word == '_' || $word == '.') {
128
+							return ' ';
129
+						}
130
+
131
+						return ' ' . strtolower($word);
132
+					},
133
+					$key
134
+				)
135
+			)
136
+		);
137
+	}
138
+
139
+	/**
140
+	 * Helper for internal purposes
141
+	 * @param $id
142
+	 * @param $component
143
+	 * @return string
144
+	 */
145
+	public static function gridIdFormatter($id, $component)
146
+	{
147
+		if ($id == 0) {
148
+			return $component;
149
+		}
150
+
151
+		return 'grid[' . $id . '][' . $component . ']';
152
+	}
153 153
 }
154 154
\ No newline at end of file
Please login to merge, or discard this patch.