@@ -6,5 +6,5 @@ |
||
6 | 6 | * @return \Woo\GridView\GridView |
7 | 7 | */ |
8 | 8 | function grid(array $config) { |
9 | - return \Woo\GridView\GridView::make($config); |
|
9 | + return \Woo\GridView\GridView::make($config); |
|
10 | 10 | } |
11 | 11 | \ No newline at end of file |
@@ -6,94 +6,94 @@ |
||
6 | 6 | |
7 | 7 | class GridViewHelper |
8 | 8 | { |
9 | - private function __construct() {} |
|
10 | - |
|
11 | - /** |
|
12 | - * Allows to load config into object properties |
|
13 | - * @param object $object |
|
14 | - * @param array $config |
|
15 | - * @throws GridViewConfigException |
|
16 | - */ |
|
17 | - public static function loadConfig($object, array $config) |
|
18 | - { |
|
19 | - foreach ($config as $key => $value) { |
|
20 | - |
|
21 | - if (property_exists($object, $key)) { |
|
22 | - $object->$key = $value; |
|
23 | - } |
|
24 | - } |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * Allows to test attributes and types in config |
|
29 | - * @param $object |
|
30 | - * @param array $tests |
|
31 | - * @throws GridViewConfigException |
|
32 | - */ |
|
33 | - public static function testConfig($object, array $tests) |
|
34 | - { |
|
35 | - foreach ($tests as $property => $test) { |
|
36 | - |
|
37 | - if (!property_exists($object, $property)) { |
|
38 | - throw new GridViewConfigException( |
|
39 | - 'Unable to test ' . get_class($object) . ': property ' . $property . ' does not exist' |
|
40 | - ); |
|
41 | - } |
|
42 | - |
|
43 | - if (is_scalar($test)) { |
|
44 | - |
|
45 | - $testPassed = true; |
|
46 | - |
|
47 | - switch ($test) { |
|
48 | - case 'int': |
|
49 | - $testPassed = is_numeric($object->$property); |
|
50 | - break; |
|
51 | - |
|
52 | - case 'string': |
|
53 | - $testPassed = is_string($object->$property); |
|
54 | - break; |
|
55 | - |
|
56 | - case 'array': |
|
57 | - $testPassed = is_array($object->$property); |
|
58 | - break; |
|
59 | - |
|
60 | - case 'closure': |
|
61 | - $testPassed = $object->$property instanceof \Closure; |
|
62 | - break; |
|
63 | - |
|
64 | - case 'any': |
|
65 | - break; |
|
66 | - |
|
67 | - default: |
|
68 | - $testPassed = is_subclass_of($object->$property, $test); |
|
69 | - } |
|
70 | - |
|
71 | - if (!$testPassed) { |
|
72 | - throw new GridViewConfigException(' |
|
9 | + private function __construct() {} |
|
10 | + |
|
11 | + /** |
|
12 | + * Allows to load config into object properties |
|
13 | + * @param object $object |
|
14 | + * @param array $config |
|
15 | + * @throws GridViewConfigException |
|
16 | + */ |
|
17 | + public static function loadConfig($object, array $config) |
|
18 | + { |
|
19 | + foreach ($config as $key => $value) { |
|
20 | + |
|
21 | + if (property_exists($object, $key)) { |
|
22 | + $object->$key = $value; |
|
23 | + } |
|
24 | + } |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * Allows to test attributes and types in config |
|
29 | + * @param $object |
|
30 | + * @param array $tests |
|
31 | + * @throws GridViewConfigException |
|
32 | + */ |
|
33 | + public static function testConfig($object, array $tests) |
|
34 | + { |
|
35 | + foreach ($tests as $property => $test) { |
|
36 | + |
|
37 | + if (!property_exists($object, $property)) { |
|
38 | + throw new GridViewConfigException( |
|
39 | + 'Unable to test ' . get_class($object) . ': property ' . $property . ' does not exist' |
|
40 | + ); |
|
41 | + } |
|
42 | + |
|
43 | + if (is_scalar($test)) { |
|
44 | + |
|
45 | + $testPassed = true; |
|
46 | + |
|
47 | + switch ($test) { |
|
48 | + case 'int': |
|
49 | + $testPassed = is_numeric($object->$property); |
|
50 | + break; |
|
51 | + |
|
52 | + case 'string': |
|
53 | + $testPassed = is_string($object->$property); |
|
54 | + break; |
|
55 | + |
|
56 | + case 'array': |
|
57 | + $testPassed = is_array($object->$property); |
|
58 | + break; |
|
59 | + |
|
60 | + case 'closure': |
|
61 | + $testPassed = $object->$property instanceof \Closure; |
|
62 | + break; |
|
63 | + |
|
64 | + case 'any': |
|
65 | + break; |
|
66 | + |
|
67 | + default: |
|
68 | + $testPassed = is_subclass_of($object->$property, $test); |
|
69 | + } |
|
70 | + |
|
71 | + if (!$testPassed) { |
|
72 | + throw new GridViewConfigException(' |
|
73 | 73 | Test ' . $test . ' has failed on ' . get_class($object) . '::' . $property |
74 | - ); |
|
75 | - } |
|
76 | - } |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Allows to convert options array to html string |
|
82 | - * @param array $htmlOptions |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public static function htmlOptionsToString(array $htmlOptions) : string |
|
86 | - { |
|
87 | - if (empty($htmlOptions)) { |
|
88 | - return ''; |
|
89 | - } |
|
90 | - |
|
91 | - $out = []; |
|
92 | - |
|
93 | - foreach ($htmlOptions as $k => $v) { |
|
94 | - $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
95 | - } |
|
96 | - |
|
97 | - return implode(' ', $out); |
|
98 | - } |
|
74 | + ); |
|
75 | + } |
|
76 | + } |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Allows to convert options array to html string |
|
82 | + * @param array $htmlOptions |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public static function htmlOptionsToString(array $htmlOptions) : string |
|
86 | + { |
|
87 | + if (empty($htmlOptions)) { |
|
88 | + return ''; |
|
89 | + } |
|
90 | + |
|
91 | + $out = []; |
|
92 | + |
|
93 | + foreach ($htmlOptions as $k => $v) { |
|
94 | + $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
95 | + } |
|
96 | + |
|
97 | + return implode(' ', $out); |
|
98 | + } |
|
99 | 99 | } |
100 | 100 | \ No newline at end of file |
@@ -7,20 +7,20 @@ |
||
7 | 7 | |
8 | 8 | class DefaultRenderer implements RendererInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * @inheritdoc |
|
12 | - */ |
|
13 | - public function render(GridView $view): string |
|
14 | - { |
|
15 | - $page = intval($_GET['page'] ?? 1); |
|
10 | + /** |
|
11 | + * @inheritdoc |
|
12 | + */ |
|
13 | + public function render(GridView $view): string |
|
14 | + { |
|
15 | + $page = intval($_GET['page'] ?? 1); |
|
16 | 16 | |
17 | - return view('woo_gridview::render-default', [ |
|
18 | - 'columns' => $view->columns, |
|
19 | - 'data' => $view->dataProvider->getData($page, $view->rowsPerPage), |
|
20 | - 'tableHtmlOptions' => GridViewHelper::htmlOptionsToString($view->tableHtmlOptions), |
|
21 | - 'dataProvider' => $view->dataProvider, |
|
22 | - 'perPage' => $view->rowsPerPage, |
|
23 | - 'currentPage' => $page, |
|
24 | - ])->render(); |
|
25 | - } |
|
17 | + return view('woo_gridview::render-default', [ |
|
18 | + 'columns' => $view->columns, |
|
19 | + 'data' => $view->dataProvider->getData($page, $view->rowsPerPage), |
|
20 | + 'tableHtmlOptions' => GridViewHelper::htmlOptionsToString($view->tableHtmlOptions), |
|
21 | + 'dataProvider' => $view->dataProvider, |
|
22 | + 'perPage' => $view->rowsPerPage, |
|
23 | + 'currentPage' => $page, |
|
24 | + ])->render(); |
|
25 | + } |
|
26 | 26 | } |
27 | 27 | \ No newline at end of file |
@@ -6,5 +6,5 @@ |
||
6 | 6 | |
7 | 7 | interface RendererInterface |
8 | 8 | { |
9 | - public function render(GridView $view) : string; |
|
9 | + public function render(GridView $view) : string; |
|
10 | 10 | } |
11 | 11 | \ No newline at end of file |
@@ -6,48 +6,48 @@ |
||
6 | 6 | |
7 | 7 | abstract class BaseColumn |
8 | 8 | { |
9 | - public $title = ''; |
|
10 | - |
|
11 | - public $value = ''; |
|
12 | - |
|
13 | - public $headerHtmlOptions = []; |
|
14 | - |
|
15 | - public $contentHtmlOptions = []; |
|
16 | - |
|
17 | - public function __construct(array $config) |
|
18 | - { |
|
19 | - GridViewHelper::loadConfig($this, $config); |
|
20 | - |
|
21 | - GridViewHelper::testConfig($this, [ |
|
22 | - 'title' => 'string', |
|
23 | - 'value' => 'any', |
|
24 | - 'headerHtmlOptions' => 'array', |
|
25 | - 'contentHtmlOptions' => 'array', |
|
26 | - ]); |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Formatted header html options |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public function headerHtmlOptions() : string |
|
34 | - { |
|
35 | - return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * Formatted content html options |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - public function contentHtmlOptions() : string |
|
43 | - { |
|
44 | - return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Render column value for row |
|
49 | - * @param array|object $row |
|
50 | - * @return string|mixed |
|
51 | - */ |
|
52 | - public abstract function renderValue($row); |
|
9 | + public $title = ''; |
|
10 | + |
|
11 | + public $value = ''; |
|
12 | + |
|
13 | + public $headerHtmlOptions = []; |
|
14 | + |
|
15 | + public $contentHtmlOptions = []; |
|
16 | + |
|
17 | + public function __construct(array $config) |
|
18 | + { |
|
19 | + GridViewHelper::loadConfig($this, $config); |
|
20 | + |
|
21 | + GridViewHelper::testConfig($this, [ |
|
22 | + 'title' => 'string', |
|
23 | + 'value' => 'any', |
|
24 | + 'headerHtmlOptions' => 'array', |
|
25 | + 'contentHtmlOptions' => 'array', |
|
26 | + ]); |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Formatted header html options |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public function headerHtmlOptions() : string |
|
34 | + { |
|
35 | + return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * Formatted content html options |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + public function contentHtmlOptions() : string |
|
43 | + { |
|
44 | + return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Render column value for row |
|
49 | + * @param array|object $row |
|
50 | + * @return string|mixed |
|
51 | + */ |
|
52 | + public abstract function renderValue($row); |
|
53 | 53 | } |
54 | 54 | \ No newline at end of file |
@@ -6,69 +6,69 @@ |
||
6 | 6 | |
7 | 7 | class ActionsColumn extends BaseColumn |
8 | 8 | { |
9 | - /** |
|
10 | - * By default is empty for this column |
|
11 | - * @var string |
|
12 | - */ |
|
13 | - public $title = ''; |
|
9 | + /** |
|
10 | + * By default is empty for this column |
|
11 | + * @var string |
|
12 | + */ |
|
13 | + public $title = ''; |
|
14 | 14 | |
15 | - /** |
|
16 | - * Value contains short codes for actions |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - public $value = '{show} {edit} {delete}'; |
|
15 | + /** |
|
16 | + * Value contains short codes for actions |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + public $value = '{show} {edit} {delete}'; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Additional actions could be added, key is short-code and value is callback |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - public $additionalActions = []; |
|
21 | + /** |
|
22 | + * Additional actions could be added, key is short-code and value is callback |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + public $additionalActions = []; |
|
26 | 26 | |
27 | - /** |
|
28 | - * @var \Closure|null |
|
29 | - */ |
|
30 | - public $actionsUrls; |
|
27 | + /** |
|
28 | + * @var \Closure|null |
|
29 | + */ |
|
30 | + public $actionsUrls; |
|
31 | 31 | |
32 | - public function __construct(array $config) |
|
33 | - { |
|
34 | - parent::__construct($config); |
|
32 | + public function __construct(array $config) |
|
33 | + { |
|
34 | + parent::__construct($config); |
|
35 | 35 | |
36 | - GridViewHelper::testConfig($this, [ |
|
37 | - 'value' => 'string', |
|
38 | - 'additionalActions' => 'array', |
|
39 | - 'actionsUrls' => 'any', |
|
40 | - ]); |
|
41 | - } |
|
36 | + GridViewHelper::testConfig($this, [ |
|
37 | + 'value' => 'string', |
|
38 | + 'additionalActions' => 'array', |
|
39 | + 'actionsUrls' => 'any', |
|
40 | + ]); |
|
41 | + } |
|
42 | 42 | |
43 | - public function basicActions() |
|
44 | - { |
|
45 | - return [ |
|
46 | - 'show' => function($model) { |
|
47 | - return '<a href="' . call_user_func($this->actionsUrls, $model)['show'] . '">Show</a>'; |
|
48 | - }, |
|
49 | - 'edit' => function($model) { |
|
50 | - return '<a href="' . call_user_func($this->actionsUrls, $model)['edit'] . '">Edit</a>'; |
|
51 | - }, |
|
52 | - 'delete' => function($model) { |
|
53 | - return '<a href="' . call_user_func($this->actionsUrls, $model)['delete'] . '">Remove</a>'; |
|
54 | - }, |
|
55 | - ]; |
|
56 | - } |
|
43 | + public function basicActions() |
|
44 | + { |
|
45 | + return [ |
|
46 | + 'show' => function($model) { |
|
47 | + return '<a href="' . call_user_func($this->actionsUrls, $model)['show'] . '">Show</a>'; |
|
48 | + }, |
|
49 | + 'edit' => function($model) { |
|
50 | + return '<a href="' . call_user_func($this->actionsUrls, $model)['edit'] . '">Edit</a>'; |
|
51 | + }, |
|
52 | + 'delete' => function($model) { |
|
53 | + return '<a href="' . call_user_func($this->actionsUrls, $model)['delete'] . '">Remove</a>'; |
|
54 | + }, |
|
55 | + ]; |
|
56 | + } |
|
57 | 57 | |
58 | - public function renderValue($row) |
|
59 | - { |
|
60 | - $result = $this->value; |
|
58 | + public function renderValue($row) |
|
59 | + { |
|
60 | + $result = $this->value; |
|
61 | 61 | |
62 | - $actions = array_merge($this->basicActions(), $this->additionalActions); |
|
62 | + $actions = array_merge($this->basicActions(), $this->additionalActions); |
|
63 | 63 | |
64 | - foreach ($actions as $key => $action) { |
|
65 | - if (strpos($result, '{' . $key . '}') === false) { |
|
66 | - continue; |
|
67 | - } |
|
64 | + foreach ($actions as $key => $action) { |
|
65 | + if (strpos($result, '{' . $key . '}') === false) { |
|
66 | + continue; |
|
67 | + } |
|
68 | 68 | |
69 | - $result = str_replace('{' . $key . '}', $action($row), $result); |
|
70 | - } |
|
69 | + $result = str_replace('{' . $key . '}', $action($row), $result); |
|
70 | + } |
|
71 | 71 | |
72 | - return $result; |
|
73 | - } |
|
72 | + return $result; |
|
73 | + } |
|
74 | 74 | } |
75 | 75 | \ No newline at end of file |
@@ -6,35 +6,35 @@ |
||
6 | 6 | |
7 | 7 | class AttributeColumn extends BaseColumn |
8 | 8 | { |
9 | - public function __construct($config) |
|
10 | - { |
|
11 | - if (is_string($config)) { |
|
12 | - $this->title = ucfirst(str_replace('_', ' ', $config)); |
|
13 | - $this->value = $config; |
|
14 | - } |
|
15 | - |
|
16 | - parent::__construct([]); |
|
17 | - } |
|
18 | - |
|
19 | - /** |
|
20 | - * @inheritdoc |
|
21 | - * @throws ColumnRenderException |
|
22 | - */ |
|
23 | - public function renderValue($row) |
|
24 | - { |
|
25 | - if (is_array($row)) { |
|
26 | - |
|
27 | - if (!isset($row[$this->value])) { |
|
28 | - return null; |
|
29 | - } |
|
30 | - |
|
31 | - return $row[$this->value]; |
|
32 | - } |
|
33 | - |
|
34 | - if (!isset($row->{$this->value})) { |
|
35 | - return null; |
|
36 | - } |
|
37 | - |
|
38 | - return $row->{$this->value}; |
|
39 | - } |
|
9 | + public function __construct($config) |
|
10 | + { |
|
11 | + if (is_string($config)) { |
|
12 | + $this->title = ucfirst(str_replace('_', ' ', $config)); |
|
13 | + $this->value = $config; |
|
14 | + } |
|
15 | + |
|
16 | + parent::__construct([]); |
|
17 | + } |
|
18 | + |
|
19 | + /** |
|
20 | + * @inheritdoc |
|
21 | + * @throws ColumnRenderException |
|
22 | + */ |
|
23 | + public function renderValue($row) |
|
24 | + { |
|
25 | + if (is_array($row)) { |
|
26 | + |
|
27 | + if (!isset($row[$this->value])) { |
|
28 | + return null; |
|
29 | + } |
|
30 | + |
|
31 | + return $row[$this->value]; |
|
32 | + } |
|
33 | + |
|
34 | + if (!isset($row->{$this->value})) { |
|
35 | + return null; |
|
36 | + } |
|
37 | + |
|
38 | + return $row->{$this->value}; |
|
39 | + } |
|
40 | 40 | } |
41 | 41 | \ No newline at end of file |
@@ -6,24 +6,24 @@ |
||
6 | 6 | |
7 | 7 | class RawColumn extends BaseColumn |
8 | 8 | { |
9 | - /** |
|
10 | - * RawColumn constructor. |
|
11 | - * @param array $config |
|
12 | - */ |
|
13 | - public function __construct(array $config) |
|
14 | - { |
|
15 | - parent::__construct($config); |
|
9 | + /** |
|
10 | + * RawColumn constructor. |
|
11 | + * @param array $config |
|
12 | + */ |
|
13 | + public function __construct(array $config) |
|
14 | + { |
|
15 | + parent::__construct($config); |
|
16 | 16 | |
17 | - GridViewHelper::testConfig($this, [ |
|
18 | - 'value' => 'closure', |
|
19 | - ]); |
|
20 | - } |
|
17 | + GridViewHelper::testConfig($this, [ |
|
18 | + 'value' => 'closure', |
|
19 | + ]); |
|
20 | + } |
|
21 | 21 | |
22 | - /** |
|
23 | - * @inheritdoc |
|
24 | - */ |
|
25 | - public function renderValue($row) |
|
26 | - { |
|
27 | - return call_user_func($this->value, $row); |
|
28 | - } |
|
22 | + /** |
|
23 | + * @inheritdoc |
|
24 | + */ |
|
25 | + public function renderValue($row) |
|
26 | + { |
|
27 | + return call_user_func($this->value, $row); |
|
28 | + } |
|
29 | 29 | } |
30 | 30 | \ No newline at end of file |
@@ -4,24 +4,24 @@ |
||
4 | 4 | |
5 | 5 | interface DataProviderInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * Should return total amount of rows |
|
9 | - * @return int |
|
10 | - */ |
|
11 | - public function getCount() : int; |
|
7 | + /** |
|
8 | + * Should return total amount of rows |
|
9 | + * @return int |
|
10 | + */ |
|
11 | + public function getCount() : int; |
|
12 | 12 | |
13 | - /** |
|
14 | - * Should return amount of pages |
|
15 | - * @param int $perPage - amount of records per page |
|
16 | - * @return int |
|
17 | - */ |
|
18 | - public function getTotalPages(int $perPage) : int; |
|
13 | + /** |
|
14 | + * Should return amount of pages |
|
15 | + * @param int $perPage - amount of records per page |
|
16 | + * @return int |
|
17 | + */ |
|
18 | + public function getTotalPages(int $perPage) : int; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Should return a list of data for current page |
|
22 | - * @param int $page |
|
23 | - * @param int $perPage - amount of records per page |
|
24 | - * @return mixed |
|
25 | - */ |
|
26 | - public function getData(int $page, int $perPage); |
|
20 | + /** |
|
21 | + * Should return a list of data for current page |
|
22 | + * @param int $page |
|
23 | + * @param int $perPage - amount of records per page |
|
24 | + * @return mixed |
|
25 | + */ |
|
26 | + public function getData(int $page, int $perPage); |
|
27 | 27 | } |
28 | 28 | \ No newline at end of file |