@@ -22,134 +22,134 @@ |
||
| 22 | 22 | |
| 23 | 23 | class GridViewHelper |
| 24 | 24 | { |
| 25 | - /** |
|
| 26 | - * A list of grid aliases |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - private static $aliases = [ |
|
| 30 | - 'column' => [ |
|
| 31 | - 'attribute' => AttributeColumn::class, |
|
| 32 | - 'raw' => CallbackColumn::class, |
|
| 33 | - 'callback' => CallbackColumn::class, |
|
| 34 | - 'actions' => ActionsColumn::class, |
|
| 35 | - 'view' => ViewColumn::class, |
|
| 36 | - ], |
|
| 37 | - 'formatter' => [ |
|
| 38 | - 'email' => EmailFormatter::class, |
|
| 39 | - 'image' => ImageFormatter::class, |
|
| 40 | - 'text' => TextFormatter::class, |
|
| 41 | - 'url' => UrlFormatter::class, |
|
| 42 | - 'raw' => RawFormatter::class, |
|
| 43 | - ], |
|
| 44 | - 'filter' => [ |
|
| 45 | - 'text' => TextFilter::class, |
|
| 46 | - 'dropdown' => DropdownFilter::class, |
|
| 47 | - ], |
|
| 48 | - 'renderer' => [ |
|
| 49 | - 'default' => DefaultRenderer::class, |
|
| 50 | - ], |
|
| 51 | - 'action' => [ |
|
| 52 | - 'delete' => DeleteAction::class, |
|
| 53 | - 'update' => EditAction::class, |
|
| 54 | - 'edit' => EditAction::class, |
|
| 55 | - 'show' => ShowAction::class, |
|
| 56 | - 'view' => ShowAction::class, |
|
| 57 | - 'action' => Action::class, |
|
| 58 | - ] |
|
| 59 | - ]; |
|
| 60 | - |
|
| 61 | - private function __construct() {} |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Useful in case you want to register a new alias for your project |
|
| 65 | - * @param string $context |
|
| 66 | - * @param string $alias |
|
| 67 | - * @param string $aliasTo |
|
| 68 | - */ |
|
| 69 | - public static function registerAlias(string $context, string $alias, string $aliasTo) |
|
| 70 | - { |
|
| 71 | - self::$aliases[$context][$alias] = $aliasTo; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Allows to resolve class name by its alias |
|
| 76 | - * @param string $context |
|
| 77 | - * @param string $alias |
|
| 78 | - * @return mixed |
|
| 79 | - */ |
|
| 80 | - public static function resolveAlias(string $context, string $alias) |
|
| 81 | - { |
|
| 82 | - return self::$aliases[$context][$alias] ?? $alias; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Allows to convert options array to html string |
|
| 87 | - * @param array $htmlOptions |
|
| 88 | - * @param array $context - context is variables, which are allowed to use when property value calculated dynamically |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string |
|
| 92 | - { |
|
| 93 | - if (empty($htmlOptions)) { |
|
| 94 | - return ''; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $out = []; |
|
| 98 | - |
|
| 99 | - foreach ($htmlOptions as $k => $v) { |
|
| 100 | - |
|
| 101 | - if ($v instanceof \Closure) { |
|
| 102 | - $v = call_user_func_array($v, $context); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - return implode(' ', $out); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Allows to make column title by it key or attribute name |
|
| 113 | - * @param string|int $key |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - public static function columnTitle($key) : string |
|
| 117 | - { |
|
| 118 | - if (is_numeric($key)) { |
|
| 119 | - return 'Column'; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return ucwords( |
|
| 123 | - trim( |
|
| 124 | - preg_replace_callback( |
|
| 125 | - '/([A-Z]|_|\.)/', |
|
| 126 | - function($word) { |
|
| 127 | - $word = $word[0]; |
|
| 128 | - |
|
| 129 | - if ($word == '_' || $word == '.') { |
|
| 130 | - return ' '; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return ' ' . strtolower($word); |
|
| 134 | - }, |
|
| 135 | - $key |
|
| 136 | - ) |
|
| 137 | - ) |
|
| 138 | - ); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Helper for internal purposes |
|
| 143 | - * @param $id |
|
| 144 | - * @param $component |
|
| 145 | - * @return string |
|
| 146 | - */ |
|
| 147 | - public static function gridIdFormatter($id, $component) |
|
| 148 | - { |
|
| 149 | - if ($id == 0) { |
|
| 150 | - return $component; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return 'grid[' . $id . '][' . $component . ']'; |
|
| 154 | - } |
|
| 25 | + /** |
|
| 26 | + * A list of grid aliases |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + private static $aliases = [ |
|
| 30 | + 'column' => [ |
|
| 31 | + 'attribute' => AttributeColumn::class, |
|
| 32 | + 'raw' => CallbackColumn::class, |
|
| 33 | + 'callback' => CallbackColumn::class, |
|
| 34 | + 'actions' => ActionsColumn::class, |
|
| 35 | + 'view' => ViewColumn::class, |
|
| 36 | + ], |
|
| 37 | + 'formatter' => [ |
|
| 38 | + 'email' => EmailFormatter::class, |
|
| 39 | + 'image' => ImageFormatter::class, |
|
| 40 | + 'text' => TextFormatter::class, |
|
| 41 | + 'url' => UrlFormatter::class, |
|
| 42 | + 'raw' => RawFormatter::class, |
|
| 43 | + ], |
|
| 44 | + 'filter' => [ |
|
| 45 | + 'text' => TextFilter::class, |
|
| 46 | + 'dropdown' => DropdownFilter::class, |
|
| 47 | + ], |
|
| 48 | + 'renderer' => [ |
|
| 49 | + 'default' => DefaultRenderer::class, |
|
| 50 | + ], |
|
| 51 | + 'action' => [ |
|
| 52 | + 'delete' => DeleteAction::class, |
|
| 53 | + 'update' => EditAction::class, |
|
| 54 | + 'edit' => EditAction::class, |
|
| 55 | + 'show' => ShowAction::class, |
|
| 56 | + 'view' => ShowAction::class, |
|
| 57 | + 'action' => Action::class, |
|
| 58 | + ] |
|
| 59 | + ]; |
|
| 60 | + |
|
| 61 | + private function __construct() {} |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Useful in case you want to register a new alias for your project |
|
| 65 | + * @param string $context |
|
| 66 | + * @param string $alias |
|
| 67 | + * @param string $aliasTo |
|
| 68 | + */ |
|
| 69 | + public static function registerAlias(string $context, string $alias, string $aliasTo) |
|
| 70 | + { |
|
| 71 | + self::$aliases[$context][$alias] = $aliasTo; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Allows to resolve class name by its alias |
|
| 76 | + * @param string $context |
|
| 77 | + * @param string $alias |
|
| 78 | + * @return mixed |
|
| 79 | + */ |
|
| 80 | + public static function resolveAlias(string $context, string $alias) |
|
| 81 | + { |
|
| 82 | + return self::$aliases[$context][$alias] ?? $alias; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Allows to convert options array to html string |
|
| 87 | + * @param array $htmlOptions |
|
| 88 | + * @param array $context - context is variables, which are allowed to use when property value calculated dynamically |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string |
|
| 92 | + { |
|
| 93 | + if (empty($htmlOptions)) { |
|
| 94 | + return ''; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $out = []; |
|
| 98 | + |
|
| 99 | + foreach ($htmlOptions as $k => $v) { |
|
| 100 | + |
|
| 101 | + if ($v instanceof \Closure) { |
|
| 102 | + $v = call_user_func_array($v, $context); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + return implode(' ', $out); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Allows to make column title by it key or attribute name |
|
| 113 | + * @param string|int $key |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + public static function columnTitle($key) : string |
|
| 117 | + { |
|
| 118 | + if (is_numeric($key)) { |
|
| 119 | + return 'Column'; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return ucwords( |
|
| 123 | + trim( |
|
| 124 | + preg_replace_callback( |
|
| 125 | + '/([A-Z]|_|\.)/', |
|
| 126 | + function($word) { |
|
| 127 | + $word = $word[0]; |
|
| 128 | + |
|
| 129 | + if ($word == '_' || $word == '.') { |
|
| 130 | + return ' '; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return ' ' . strtolower($word); |
|
| 134 | + }, |
|
| 135 | + $key |
|
| 136 | + ) |
|
| 137 | + ) |
|
| 138 | + ); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Helper for internal purposes |
|
| 143 | + * @param $id |
|
| 144 | + * @param $component |
|
| 145 | + * @return string |
|
| 146 | + */ |
|
| 147 | + public static function gridIdFormatter($id, $component) |
|
| 148 | + { |
|
| 149 | + if ($id == 0) { |
|
| 150 | + return $component; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return 'grid[' . $id . '][' . $component . ']'; |
|
| 154 | + } |
|
| 155 | 155 | } |
| 156 | 156 | \ 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 |