@@ -19,7 +19,7 @@ |
||
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 |
@@ -19,17 +19,17 @@ |
||
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() |
@@ -7,5 +7,5 @@ |
||
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 |
@@ -7,88 +7,88 @@ |
||
7 | 7 | |
8 | 8 | trait Configurable |
9 | 9 | { |
10 | - /** |
|
11 | - * Allows to load config into object properties |
|
12 | - * @param array $config |
|
13 | - * @throws GridViewConfigException |
|
14 | - */ |
|
15 | - public function loadConfig(array $config) |
|
16 | - { |
|
17 | - foreach ($config as $key => $value) { |
|
18 | - |
|
19 | - if (property_exists($this, $key)) { |
|
20 | - $this->$key = $value; |
|
21 | - } |
|
22 | - } |
|
23 | - |
|
24 | - $this->testConfig(); |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * Should specify tests |
|
29 | - * @return array |
|
30 | - */ |
|
31 | - abstract protected function configTests() : array; |
|
32 | - |
|
33 | - /** |
|
34 | - * Allows to test attributes and types in config |
|
35 | - * @throws GridViewConfigException |
|
36 | - */ |
|
37 | - protected function testConfig() |
|
38 | - { |
|
39 | - foreach ($this->configTests() as $property => $tests) { |
|
40 | - |
|
41 | - if (!property_exists($this, $property)) { |
|
42 | - throw new GridViewConfigException( |
|
43 | - 'Unable to test ' . get_class($this) . ': property ' . $property . ' does not exist' |
|
44 | - ); |
|
45 | - } |
|
46 | - |
|
47 | - $testPassed = true; |
|
48 | - $testMessage = 'Validation failed'; |
|
49 | - |
|
50 | - foreach (explode('|', $tests) as $test) { |
|
51 | - |
|
52 | - switch ($test) { |
|
53 | - case 'int': |
|
54 | - $testPassed = is_numeric($this->$property); |
|
55 | - $testMessage = 'Property should be numeric'; |
|
56 | - break; |
|
57 | - |
|
58 | - case 'string': |
|
59 | - $testPassed = is_string($this->$property); |
|
60 | - $testMessage = 'Property should be a string'; |
|
61 | - break; |
|
62 | - |
|
63 | - case 'array': |
|
64 | - $testPassed = is_array($this->$property); |
|
65 | - $testMessage = 'Property should be an array'; |
|
66 | - break; |
|
67 | - |
|
68 | - case 'closure': |
|
69 | - $testPassed = $this->$property instanceof Closure; |
|
70 | - $testMessage = 'Property should be a valid callback (Closure instance)'; |
|
71 | - break; |
|
72 | - |
|
73 | - case 'boolean': |
|
74 | - $testPassed = is_bool($this->$property); |
|
75 | - $testMessage = 'Property should be boolean'; |
|
76 | - break; |
|
77 | - |
|
78 | - case 'any': |
|
79 | - break; |
|
80 | - |
|
81 | - default: |
|
82 | - $testPassed = $testPassed || is_a($this->$property, $test) || is_subclass_of($this->$property, $test); |
|
83 | - $testMessage = 'Property should be ' . $test . ' instance/class reference, check ' . $property; |
|
84 | - } |
|
85 | - } |
|
86 | - |
|
87 | - if (!$testPassed) { |
|
88 | - throw new GridViewConfigException( |
|
89 | - 'Tests ' . $tests . ' has failed on ' . get_class($this) . '::' . $property . ': ' . $testMessage |
|
90 | - ); |
|
91 | - } |
|
92 | - } |
|
93 | - } |
|
10 | + /** |
|
11 | + * Allows to load config into object properties |
|
12 | + * @param array $config |
|
13 | + * @throws GridViewConfigException |
|
14 | + */ |
|
15 | + public function loadConfig(array $config) |
|
16 | + { |
|
17 | + foreach ($config as $key => $value) { |
|
18 | + |
|
19 | + if (property_exists($this, $key)) { |
|
20 | + $this->$key = $value; |
|
21 | + } |
|
22 | + } |
|
23 | + |
|
24 | + $this->testConfig(); |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * Should specify tests |
|
29 | + * @return array |
|
30 | + */ |
|
31 | + abstract protected function configTests() : array; |
|
32 | + |
|
33 | + /** |
|
34 | + * Allows to test attributes and types in config |
|
35 | + * @throws GridViewConfigException |
|
36 | + */ |
|
37 | + protected function testConfig() |
|
38 | + { |
|
39 | + foreach ($this->configTests() as $property => $tests) { |
|
40 | + |
|
41 | + if (!property_exists($this, $property)) { |
|
42 | + throw new GridViewConfigException( |
|
43 | + 'Unable to test ' . get_class($this) . ': property ' . $property . ' does not exist' |
|
44 | + ); |
|
45 | + } |
|
46 | + |
|
47 | + $testPassed = true; |
|
48 | + $testMessage = 'Validation failed'; |
|
49 | + |
|
50 | + foreach (explode('|', $tests) as $test) { |
|
51 | + |
|
52 | + switch ($test) { |
|
53 | + case 'int': |
|
54 | + $testPassed = is_numeric($this->$property); |
|
55 | + $testMessage = 'Property should be numeric'; |
|
56 | + break; |
|
57 | + |
|
58 | + case 'string': |
|
59 | + $testPassed = is_string($this->$property); |
|
60 | + $testMessage = 'Property should be a string'; |
|
61 | + break; |
|
62 | + |
|
63 | + case 'array': |
|
64 | + $testPassed = is_array($this->$property); |
|
65 | + $testMessage = 'Property should be an array'; |
|
66 | + break; |
|
67 | + |
|
68 | + case 'closure': |
|
69 | + $testPassed = $this->$property instanceof Closure; |
|
70 | + $testMessage = 'Property should be a valid callback (Closure instance)'; |
|
71 | + break; |
|
72 | + |
|
73 | + case 'boolean': |
|
74 | + $testPassed = is_bool($this->$property); |
|
75 | + $testMessage = 'Property should be boolean'; |
|
76 | + break; |
|
77 | + |
|
78 | + case 'any': |
|
79 | + break; |
|
80 | + |
|
81 | + default: |
|
82 | + $testPassed = $testPassed || is_a($this->$property, $test) || is_subclass_of($this->$property, $test); |
|
83 | + $testMessage = 'Property should be ' . $test . ' instance/class reference, check ' . $property; |
|
84 | + } |
|
85 | + } |
|
86 | + |
|
87 | + if (!$testPassed) { |
|
88 | + throw new GridViewConfigException( |
|
89 | + 'Tests ' . $tests . ' has failed on ' . get_class($this) . '::' . $property . ': ' . $testMessage |
|
90 | + ); |
|
91 | + } |
|
92 | + } |
|
93 | + } |
|
94 | 94 | } |
95 | 95 | \ No newline at end of file |
@@ -4,8 +4,8 @@ |
||
4 | 4 | |
5 | 5 | class BooleanFormatter implements IFormatter |
6 | 6 | { |
7 | - public function format($value): string |
|
8 | - { |
|
9 | - return $value ? 'Yes' : 'No'; |
|
10 | - } |
|
7 | + public function format($value): string |
|
8 | + { |
|
9 | + return $value ? 'Yes' : 'No'; |
|
10 | + } |
|
11 | 11 | } |
12 | 12 | \ No newline at end of file |
@@ -4,8 +4,8 @@ |
||
4 | 4 | |
5 | 5 | class RawFormatter implements IFormatter |
6 | 6 | { |
7 | - public function format($value): string |
|
8 | - { |
|
9 | - return $value; |
|
10 | - } |
|
7 | + public function format($value): string |
|
8 | + { |
|
9 | + return $value; |
|
10 | + } |
|
11 | 11 | } |
12 | 12 | \ No newline at end of file |
@@ -4,31 +4,31 @@ |
||
4 | 4 | |
5 | 5 | class CallbackColumn extends BaseColumn |
6 | 6 | { |
7 | - /** |
|
8 | - * @var string |
|
9 | - */ |
|
10 | - public $formatters = ['raw']; |
|
7 | + /** |
|
8 | + * @var string |
|
9 | + */ |
|
10 | + public $formatters = ['raw']; |
|
11 | 11 | |
12 | - /** |
|
13 | - * @var bool |
|
14 | - */ |
|
15 | - public $sortable = false; |
|
12 | + /** |
|
13 | + * @var bool |
|
14 | + */ |
|
15 | + public $sortable = false; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @return array |
|
19 | - */ |
|
20 | - protected function configTests(): array |
|
21 | - { |
|
22 | - return array_merge(parent::configTests(), [ |
|
23 | - 'value' => 'closure', |
|
24 | - ]); |
|
25 | - } |
|
17 | + /** |
|
18 | + * @return array |
|
19 | + */ |
|
20 | + protected function configTests(): array |
|
21 | + { |
|
22 | + return array_merge(parent::configTests(), [ |
|
23 | + 'value' => 'closure', |
|
24 | + ]); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * @inheritdoc |
|
29 | - */ |
|
30 | - public function _renderValue($row) |
|
31 | - { |
|
32 | - return call_user_func($this->value, $row); |
|
33 | - } |
|
27 | + /** |
|
28 | + * @inheritdoc |
|
29 | + */ |
|
30 | + public function _renderValue($row) |
|
31 | + { |
|
32 | + return call_user_func($this->value, $row); |
|
33 | + } |
|
34 | 34 | } |
35 | 35 | \ No newline at end of file |
@@ -23,144 +23,144 @@ |
||
23 | 23 | |
24 | 24 | class GridViewHelper |
25 | 25 | { |
26 | - /** |
|
27 | - * A list of grid aliases |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - private static $aliases = [ |
|
31 | - 'column' => [ |
|
32 | - 'attribute' => AttributeColumn::class, |
|
33 | - 'raw' => CallbackColumn::class, |
|
34 | - 'callback' => CallbackColumn::class, |
|
35 | - 'actions' => ActionsColumn::class, |
|
36 | - 'view' => ViewColumn::class, |
|
37 | - ], |
|
38 | - 'formatter' => [ |
|
39 | - 'email' => EmailFormatter::class, |
|
40 | - 'image' => ImageFormatter::class, |
|
41 | - 'text' => TextFormatter::class, |
|
42 | - 'url' => UrlFormatter::class, |
|
43 | - 'raw' => RawFormatter::class, |
|
44 | - ], |
|
45 | - 'filter' => [ |
|
46 | - 'text' => TextFilter::class, |
|
47 | - 'dropdown' => DropdownFilter::class, |
|
48 | - ], |
|
49 | - 'renderer' => [ |
|
50 | - 'default' => DefaultRenderer::class, |
|
51 | - ], |
|
52 | - 'action' => [ |
|
53 | - 'delete' => DeleteAction::class, |
|
54 | - 'update' => EditAction::class, |
|
55 | - 'edit' => EditAction::class, |
|
56 | - 'show' => ShowAction::class, |
|
57 | - 'view' => ShowAction::class, |
|
58 | - 'action' => Action::class, |
|
59 | - ] |
|
60 | - ]; |
|
61 | - |
|
62 | - private function __construct() {} |
|
63 | - |
|
64 | - /** |
|
65 | - * Useful in case you want to register a new alias for your project |
|
66 | - * @param string $context |
|
67 | - * @param string $alias |
|
68 | - * @param string $aliasTo |
|
69 | - */ |
|
70 | - public static function registerAlias(string $context, string $alias, string $aliasTo) |
|
71 | - { |
|
72 | - self::$aliases[$context][$alias] = $aliasTo; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Allows to resolve class name by its alias |
|
77 | - * @param string $context |
|
78 | - * @param string $alias |
|
79 | - * @return mixed |
|
80 | - */ |
|
81 | - public static function resolveAlias(string $context, string $alias) |
|
82 | - { |
|
83 | - return self::$aliases[$context][$alias] ?? $alias; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Allows to convert options array to html string |
|
88 | - * @param array $htmlOptions |
|
89 | - * @param array $context - context is variables, which are allowed to use when property value calculated dynamically |
|
90 | - * @return string |
|
91 | - */ |
|
92 | - public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string |
|
93 | - { |
|
94 | - if (empty($htmlOptions)) { |
|
95 | - return ''; |
|
96 | - } |
|
97 | - |
|
98 | - $out = []; |
|
99 | - |
|
100 | - foreach ($htmlOptions as $k => $v) { |
|
101 | - |
|
102 | - if ($v instanceof \Closure) { |
|
103 | - $v = call_user_func_array($v, $context); |
|
104 | - } |
|
105 | - |
|
106 | - $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
107 | - } |
|
108 | - |
|
109 | - return implode(' ', $out); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Allows to make column title by it key or attribute name |
|
114 | - * @param string|int $key |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public static function columnTitle($key) : string |
|
118 | - { |
|
119 | - if (is_numeric($key)) { |
|
120 | - return 'Column'; |
|
121 | - } |
|
122 | - |
|
123 | - return ucwords( |
|
124 | - trim( |
|
125 | - preg_replace_callback( |
|
126 | - '/([A-Z]|_|\.)/', |
|
127 | - function($word) { |
|
128 | - $word = $word[0]; |
|
129 | - |
|
130 | - if ($word == '_' || $word == '.') { |
|
131 | - return ' '; |
|
132 | - } |
|
133 | - |
|
134 | - return ' ' . strtolower($word); |
|
135 | - }, |
|
136 | - $key |
|
137 | - ) |
|
138 | - ) |
|
139 | - ); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Helper for internal purposes |
|
144 | - * @param $id |
|
145 | - * @param $component |
|
146 | - * @return string |
|
147 | - */ |
|
148 | - public static function gridIdFormatter($id, $component) |
|
149 | - { |
|
150 | - if ($id == 0) { |
|
151 | - return $component; |
|
152 | - } |
|
153 | - |
|
154 | - return 'grid[' . $id . '][' . $component . ']'; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Generates page url with all requested params from request |
|
159 | - * @param $gridId |
|
160 | - * @param $page |
|
161 | - */ |
|
162 | - public static function pageUrl($gridId, $page) |
|
163 | - { |
|
164 | - return url()->current() . '?' . Arr::query([\Woo\GridView\GridViewHelper::gridIdFormatter($gridId, 'page') => $page] + request()->query()); |
|
165 | - } |
|
26 | + /** |
|
27 | + * A list of grid aliases |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + private static $aliases = [ |
|
31 | + 'column' => [ |
|
32 | + 'attribute' => AttributeColumn::class, |
|
33 | + 'raw' => CallbackColumn::class, |
|
34 | + 'callback' => CallbackColumn::class, |
|
35 | + 'actions' => ActionsColumn::class, |
|
36 | + 'view' => ViewColumn::class, |
|
37 | + ], |
|
38 | + 'formatter' => [ |
|
39 | + 'email' => EmailFormatter::class, |
|
40 | + 'image' => ImageFormatter::class, |
|
41 | + 'text' => TextFormatter::class, |
|
42 | + 'url' => UrlFormatter::class, |
|
43 | + 'raw' => RawFormatter::class, |
|
44 | + ], |
|
45 | + 'filter' => [ |
|
46 | + 'text' => TextFilter::class, |
|
47 | + 'dropdown' => DropdownFilter::class, |
|
48 | + ], |
|
49 | + 'renderer' => [ |
|
50 | + 'default' => DefaultRenderer::class, |
|
51 | + ], |
|
52 | + 'action' => [ |
|
53 | + 'delete' => DeleteAction::class, |
|
54 | + 'update' => EditAction::class, |
|
55 | + 'edit' => EditAction::class, |
|
56 | + 'show' => ShowAction::class, |
|
57 | + 'view' => ShowAction::class, |
|
58 | + 'action' => Action::class, |
|
59 | + ] |
|
60 | + ]; |
|
61 | + |
|
62 | + private function __construct() {} |
|
63 | + |
|
64 | + /** |
|
65 | + * Useful in case you want to register a new alias for your project |
|
66 | + * @param string $context |
|
67 | + * @param string $alias |
|
68 | + * @param string $aliasTo |
|
69 | + */ |
|
70 | + public static function registerAlias(string $context, string $alias, string $aliasTo) |
|
71 | + { |
|
72 | + self::$aliases[$context][$alias] = $aliasTo; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Allows to resolve class name by its alias |
|
77 | + * @param string $context |
|
78 | + * @param string $alias |
|
79 | + * @return mixed |
|
80 | + */ |
|
81 | + public static function resolveAlias(string $context, string $alias) |
|
82 | + { |
|
83 | + return self::$aliases[$context][$alias] ?? $alias; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Allows to convert options array to html string |
|
88 | + * @param array $htmlOptions |
|
89 | + * @param array $context - context is variables, which are allowed to use when property value calculated dynamically |
|
90 | + * @return string |
|
91 | + */ |
|
92 | + public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string |
|
93 | + { |
|
94 | + if (empty($htmlOptions)) { |
|
95 | + return ''; |
|
96 | + } |
|
97 | + |
|
98 | + $out = []; |
|
99 | + |
|
100 | + foreach ($htmlOptions as $k => $v) { |
|
101 | + |
|
102 | + if ($v instanceof \Closure) { |
|
103 | + $v = call_user_func_array($v, $context); |
|
104 | + } |
|
105 | + |
|
106 | + $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
107 | + } |
|
108 | + |
|
109 | + return implode(' ', $out); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Allows to make column title by it key or attribute name |
|
114 | + * @param string|int $key |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public static function columnTitle($key) : string |
|
118 | + { |
|
119 | + if (is_numeric($key)) { |
|
120 | + return 'Column'; |
|
121 | + } |
|
122 | + |
|
123 | + return ucwords( |
|
124 | + trim( |
|
125 | + preg_replace_callback( |
|
126 | + '/([A-Z]|_|\.)/', |
|
127 | + function($word) { |
|
128 | + $word = $word[0]; |
|
129 | + |
|
130 | + if ($word == '_' || $word == '.') { |
|
131 | + return ' '; |
|
132 | + } |
|
133 | + |
|
134 | + return ' ' . strtolower($word); |
|
135 | + }, |
|
136 | + $key |
|
137 | + ) |
|
138 | + ) |
|
139 | + ); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Helper for internal purposes |
|
144 | + * @param $id |
|
145 | + * @param $component |
|
146 | + * @return string |
|
147 | + */ |
|
148 | + public static function gridIdFormatter($id, $component) |
|
149 | + { |
|
150 | + if ($id == 0) { |
|
151 | + return $component; |
|
152 | + } |
|
153 | + |
|
154 | + return 'grid[' . $id . '][' . $component . ']'; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Generates page url with all requested params from request |
|
159 | + * @param $gridId |
|
160 | + * @param $page |
|
161 | + */ |
|
162 | + public static function pageUrl($gridId, $page) |
|
163 | + { |
|
164 | + return url()->current() . '?' . Arr::query([\Woo\GridView\GridViewHelper::gridIdFormatter($gridId, 'page') => $page] + request()->query()); |
|
165 | + } |
|
166 | 166 | } |
@@ -7,58 +7,58 @@ |
||
7 | 7 | |
8 | 8 | class EloquentDataProvider extends BaseDataProvider |
9 | 9 | { |
10 | - protected $query; |
|
10 | + protected $query; |
|
11 | 11 | |
12 | - /** |
|
13 | - * EloquentDataProvider constructor. |
|
14 | - * @param Builder $query |
|
15 | - */ |
|
16 | - public function __construct(Builder $query) |
|
17 | - { |
|
18 | - $this->query = clone $query; |
|
19 | - } |
|
12 | + /** |
|
13 | + * EloquentDataProvider constructor. |
|
14 | + * @param Builder $query |
|
15 | + */ |
|
16 | + public function __construct(Builder $query) |
|
17 | + { |
|
18 | + $this->query = clone $query; |
|
19 | + } |
|
20 | 20 | |
21 | 21 | |
22 | - /** |
|
23 | - * @param GridViewRequest $request |
|
24 | - * @return Builder |
|
25 | - */ |
|
26 | - protected function baseQuery(GridViewRequest $request) |
|
27 | - { |
|
28 | - $query = clone $this->query; |
|
22 | + /** |
|
23 | + * @param GridViewRequest $request |
|
24 | + * @return Builder |
|
25 | + */ |
|
26 | + protected function baseQuery(GridViewRequest $request) |
|
27 | + { |
|
28 | + $query = clone $this->query; |
|
29 | 29 | |
30 | - foreach ($request->filters as $field => $value) { |
|
31 | - $query->where($field, 'LIKE', '%' . $value . '%'); |
|
32 | - } |
|
30 | + foreach ($request->filters as $field => $value) { |
|
31 | + $query->where($field, 'LIKE', '%' . $value . '%'); |
|
32 | + } |
|
33 | 33 | |
34 | - if ($request->sortColumn) { |
|
35 | - $query->orderBy($request->sortColumn, $request->sortOrder); |
|
36 | - } |
|
34 | + if ($request->sortColumn) { |
|
35 | + $query->orderBy($request->sortColumn, $request->sortOrder); |
|
36 | + } |
|
37 | 37 | |
38 | - return $query; |
|
39 | - } |
|
38 | + return $query; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * @inheritdoc |
|
43 | - */ |
|
44 | - public function getCount(GridViewRequest $request) : int |
|
45 | - { |
|
46 | - return $this->baseQuery($request)->count(); |
|
47 | - } |
|
41 | + /** |
|
42 | + * @inheritdoc |
|
43 | + */ |
|
44 | + public function getCount(GridViewRequest $request) : int |
|
45 | + { |
|
46 | + return $this->baseQuery($request)->count(); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @inheritdoc |
|
51 | - */ |
|
52 | - public function getData(GridViewRequest $request) |
|
53 | - { |
|
54 | - $query = $this->baseQuery($request); |
|
49 | + /** |
|
50 | + * @inheritdoc |
|
51 | + */ |
|
52 | + public function getData(GridViewRequest $request) |
|
53 | + { |
|
54 | + $query = $this->baseQuery($request); |
|
55 | 55 | |
56 | - if ($request->perPage == 0) { |
|
57 | - return $query->get(); |
|
58 | - } |
|
56 | + if ($request->perPage == 0) { |
|
57 | + return $query->get(); |
|
58 | + } |
|
59 | 59 | |
60 | - return $query->offset(($request->page - 1) * $request->perPage) |
|
61 | - ->limit($request->perPage) |
|
62 | - ->get(); |
|
63 | - } |
|
60 | + return $query->offset(($request->page - 1) * $request->perPage) |
|
61 | + ->limit($request->perPage) |
|
62 | + ->get(); |
|
63 | + } |
|
64 | 64 | } |
@@ -9,166 +9,166 @@ |
||
9 | 9 | |
10 | 10 | abstract class BaseColumn |
11 | 11 | { |
12 | - use Configurable; |
|
13 | - |
|
14 | - /** |
|
15 | - * Column title |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - public $title = ''; |
|
19 | - |
|
20 | - /** |
|
21 | - * Column value. Could be an attribute, |
|
22 | - * @var string|mixed |
|
23 | - */ |
|
24 | - public $value = ''; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var BaseFilter |
|
28 | - */ |
|
29 | - public $filter; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var boolean|string |
|
33 | - */ |
|
34 | - public $sortable = true; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - public $headerHtmlOptions = []; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var array |
|
43 | - */ |
|
44 | - public $contentHtmlOptions = []; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var array - allowed: raw, url, email, text, image |
|
48 | - */ |
|
49 | - public $formatters = ['text']; |
|
50 | - |
|
51 | - /** |
|
52 | - * Value when column is empty |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - public $emptyValue = '-'; |
|
56 | - |
|
57 | - /** |
|
58 | - * BaseColumn constructor. |
|
59 | - * @param array $config |
|
60 | - * @throws \Woo\GridView\Exceptions\GridViewConfigException |
|
61 | - */ |
|
62 | - public function __construct(array $config) |
|
63 | - { |
|
64 | - $this->loadConfig($config); |
|
65 | - |
|
66 | - $this->buildFilter(); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Allows to get sortable column's name |
|
71 | - */ |
|
72 | - public function getSortableName() |
|
73 | - { |
|
74 | - if ($this->sortable === false) { |
|
75 | - return false; |
|
76 | - } |
|
77 | - |
|
78 | - if (!is_bool($this->sortable)) { |
|
79 | - return $this->sortable; |
|
80 | - } |
|
81 | - |
|
82 | - if (is_scalar($this->value)) { |
|
83 | - return $this->value; |
|
84 | - } |
|
85 | - |
|
86 | - return false; |
|
87 | - } |
|
88 | - |
|
89 | - protected function buildFilter() |
|
90 | - { |
|
91 | - if (is_null($this->filter) || is_object($this->filter)) { |
|
92 | - return; |
|
93 | - } |
|
94 | - |
|
95 | - if (is_string($this->filter)) { |
|
96 | - $this->filter = [ |
|
97 | - 'class' => $this->filter, |
|
98 | - 'name' => $this->value, |
|
99 | - ]; |
|
100 | - } |
|
101 | - |
|
102 | - if (empty($this->filter['class'])) { |
|
103 | - $this->filter['class'] = TextFilter::class; |
|
104 | - } |
|
105 | - |
|
106 | - if (empty($this->filter['name'])) { |
|
107 | - $this->filter['name'] = $this->value; |
|
108 | - } |
|
109 | - |
|
110 | - $className = GridViewHelper::resolveAlias('filter', $this->filter['class']); |
|
111 | - $this->filter = new $className($this->filter); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @return array |
|
116 | - */ |
|
117 | - protected function configTests(): array |
|
118 | - { |
|
119 | - return [ |
|
120 | - 'title' => 'string', |
|
121 | - 'value' => 'any', |
|
122 | - 'headerHtmlOptions' => 'array', |
|
123 | - 'contentHtmlOptions' => 'array', |
|
124 | - 'formatters' => 'array', |
|
125 | - 'emptyValue' => 'string', |
|
126 | - 'sortable' => 'any', |
|
127 | - 'filter' => BaseFilter::class . '|null', |
|
128 | - ]; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Formatted header html options |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - public function compileHeaderHtmlOptions() : string |
|
136 | - { |
|
137 | - return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Formatted content html options |
|
142 | - * @param array $context |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function compileContentHtmlOptions(array $context) : string |
|
146 | - { |
|
147 | - return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Render column value for row |
|
152 | - * @param array|object $row |
|
153 | - * @return string|mixed |
|
154 | - */ |
|
155 | - protected abstract function _renderValue($row); |
|
156 | - |
|
157 | - /** |
|
158 | - * Renders column content |
|
159 | - * @param $row |
|
160 | - * @return string |
|
161 | - */ |
|
162 | - public function renderValue($row) |
|
163 | - { |
|
164 | - $value = $this->_renderValue($row); |
|
165 | - |
|
166 | - foreach ($this->formatters as $formatter) { |
|
167 | - $className = GridViewHelper::resolveAlias('formatter', $formatter); |
|
168 | - $value = (new $className)->format($value); |
|
169 | - } |
|
170 | - |
|
171 | - return $value; |
|
172 | - } |
|
12 | + use Configurable; |
|
13 | + |
|
14 | + /** |
|
15 | + * Column title |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + public $title = ''; |
|
19 | + |
|
20 | + /** |
|
21 | + * Column value. Could be an attribute, |
|
22 | + * @var string|mixed |
|
23 | + */ |
|
24 | + public $value = ''; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var BaseFilter |
|
28 | + */ |
|
29 | + public $filter; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var boolean|string |
|
33 | + */ |
|
34 | + public $sortable = true; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + public $headerHtmlOptions = []; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var array |
|
43 | + */ |
|
44 | + public $contentHtmlOptions = []; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var array - allowed: raw, url, email, text, image |
|
48 | + */ |
|
49 | + public $formatters = ['text']; |
|
50 | + |
|
51 | + /** |
|
52 | + * Value when column is empty |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + public $emptyValue = '-'; |
|
56 | + |
|
57 | + /** |
|
58 | + * BaseColumn constructor. |
|
59 | + * @param array $config |
|
60 | + * @throws \Woo\GridView\Exceptions\GridViewConfigException |
|
61 | + */ |
|
62 | + public function __construct(array $config) |
|
63 | + { |
|
64 | + $this->loadConfig($config); |
|
65 | + |
|
66 | + $this->buildFilter(); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Allows to get sortable column's name |
|
71 | + */ |
|
72 | + public function getSortableName() |
|
73 | + { |
|
74 | + if ($this->sortable === false) { |
|
75 | + return false; |
|
76 | + } |
|
77 | + |
|
78 | + if (!is_bool($this->sortable)) { |
|
79 | + return $this->sortable; |
|
80 | + } |
|
81 | + |
|
82 | + if (is_scalar($this->value)) { |
|
83 | + return $this->value; |
|
84 | + } |
|
85 | + |
|
86 | + return false; |
|
87 | + } |
|
88 | + |
|
89 | + protected function buildFilter() |
|
90 | + { |
|
91 | + if (is_null($this->filter) || is_object($this->filter)) { |
|
92 | + return; |
|
93 | + } |
|
94 | + |
|
95 | + if (is_string($this->filter)) { |
|
96 | + $this->filter = [ |
|
97 | + 'class' => $this->filter, |
|
98 | + 'name' => $this->value, |
|
99 | + ]; |
|
100 | + } |
|
101 | + |
|
102 | + if (empty($this->filter['class'])) { |
|
103 | + $this->filter['class'] = TextFilter::class; |
|
104 | + } |
|
105 | + |
|
106 | + if (empty($this->filter['name'])) { |
|
107 | + $this->filter['name'] = $this->value; |
|
108 | + } |
|
109 | + |
|
110 | + $className = GridViewHelper::resolveAlias('filter', $this->filter['class']); |
|
111 | + $this->filter = new $className($this->filter); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @return array |
|
116 | + */ |
|
117 | + protected function configTests(): array |
|
118 | + { |
|
119 | + return [ |
|
120 | + 'title' => 'string', |
|
121 | + 'value' => 'any', |
|
122 | + 'headerHtmlOptions' => 'array', |
|
123 | + 'contentHtmlOptions' => 'array', |
|
124 | + 'formatters' => 'array', |
|
125 | + 'emptyValue' => 'string', |
|
126 | + 'sortable' => 'any', |
|
127 | + 'filter' => BaseFilter::class . '|null', |
|
128 | + ]; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Formatted header html options |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + public function compileHeaderHtmlOptions() : string |
|
136 | + { |
|
137 | + return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Formatted content html options |
|
142 | + * @param array $context |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function compileContentHtmlOptions(array $context) : string |
|
146 | + { |
|
147 | + return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Render column value for row |
|
152 | + * @param array|object $row |
|
153 | + * @return string|mixed |
|
154 | + */ |
|
155 | + protected abstract function _renderValue($row); |
|
156 | + |
|
157 | + /** |
|
158 | + * Renders column content |
|
159 | + * @param $row |
|
160 | + * @return string |
|
161 | + */ |
|
162 | + public function renderValue($row) |
|
163 | + { |
|
164 | + $value = $this->_renderValue($row); |
|
165 | + |
|
166 | + foreach ($this->formatters as $formatter) { |
|
167 | + $className = GridViewHelper::resolveAlias('formatter', $formatter); |
|
168 | + $value = (new $className)->format($value); |
|
169 | + } |
|
170 | + |
|
171 | + return $value; |
|
172 | + } |
|
173 | 173 | |
174 | 174 | } |