@@ -21,133 +21,133 @@ |
||
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 |