@@ -108,7 +108,8 @@ discard block |
||
| 108 | 108 | ['view' => $view, 'source' => $source] = $this->applySorters($view, $source, $schema); |
| 109 | 109 | ['view' => $view, 'source' => $source] = $this->applyPaginator($view, $source, $schema); |
| 110 | 110 | |
| 111 | - if (!is_iterable($source)) { |
|
| 111 | + if (!is_iterable($source)) |
|
| 112 | + { |
|
| 112 | 113 | throw new GridViewException('GridView expects the source to be iterable after all.'); |
| 113 | 114 | } |
| 114 | 115 | |
@@ -126,11 +127,14 @@ discard block |
||
| 126 | 127 | protected function getFilters($source, GridSchema $schema): array |
| 127 | 128 | { |
| 128 | 129 | $filters = []; |
| 129 | - foreach ($this->getOptionArray(static::KEY_FILTER) ?? [] as $name => $value) { |
|
| 130 | - if ($schema->hasFilter($name)) { |
|
| 130 | + foreach ($this->getOptionArray(static::KEY_FILTER) ?? [] as $name => $value) |
|
| 131 | + { |
|
| 132 | + if ($schema->hasFilter($name)) |
|
| 133 | + { |
|
| 131 | 134 | $filter = $schema->getFilter($name)->withValue($value); |
| 132 | 135 | |
| 133 | - if ($filter !== null) { |
|
| 136 | + if ($filter !== null) |
|
| 137 | + { |
|
| 134 | 138 | $source = $this->compiler->compile($source, $filter); |
| 135 | 139 | $filters[$name] = $filter->getValue(); |
| 136 | 140 | } |
@@ -142,7 +146,8 @@ discard block |
||
| 142 | 146 | |
| 143 | 147 | protected function applyCounter(GridInterface $view, $source, GridSchema $schema): array |
| 144 | 148 | { |
| 145 | - if (is_countable($source) && $this->getOption(static::KEY_FETCH_COUNT)) { |
|
| 149 | + if (is_countable($source) && $this->getOption(static::KEY_FETCH_COUNT)) |
|
| 150 | + { |
|
| 146 | 151 | $view = $view->withOption(GridInterface::COUNT, ($this->count)($source)); |
| 147 | 152 | } |
| 148 | 153 | |
@@ -160,11 +165,14 @@ discard block |
||
| 160 | 165 | protected function getSorters($source, GridSchema $schema): array |
| 161 | 166 | { |
| 162 | 167 | $sorters = []; |
| 163 | - foreach ($this->getOptionArray(static::KEY_SORT) ?? [] as $name => $value) { |
|
| 164 | - if ($schema->hasSorter($name)) { |
|
| 168 | + foreach ($this->getOptionArray(static::KEY_SORT) ?? [] as $name => $value) |
|
| 169 | + { |
|
| 170 | + if ($schema->hasSorter($name)) |
|
| 171 | + { |
|
| 165 | 172 | $sorter = $schema->getSorter($name)->withDirection($value); |
| 166 | 173 | |
| 167 | - if ($sorter !== null) { |
|
| 174 | + if ($sorter !== null) |
|
| 175 | + { |
|
| 168 | 176 | $source = $this->compiler->compile($source, $sorter); |
| 169 | 177 | $sorters[$name] = $sorter->getValue(); |
| 170 | 178 | } |
@@ -176,7 +184,8 @@ discard block |
||
| 176 | 184 | |
| 177 | 185 | protected function applyPaginator(GridInterface $view, $source, GridSchema $schema): array |
| 178 | 186 | { |
| 179 | - if ($schema->getPaginator() !== null) { |
|
| 187 | + if ($schema->getPaginator() !== null) |
|
| 188 | + { |
|
| 180 | 189 | ['source' => $source, 'paginator' => $paginator] = $this->getPaginator($source, $schema); |
| 181 | 190 | $view = $view->withOption(GridInterface::PAGINATOR, $paginator); |
| 182 | 191 | } |
@@ -187,12 +196,14 @@ discard block |
||
| 187 | 196 | protected function getPaginator($source, GridSchema $schema): array |
| 188 | 197 | { |
| 189 | 198 | $paginator = $schema->getPaginator(); |
| 190 | - if (!$paginator instanceof FilterInterface) { |
|
| 199 | + if (!$paginator instanceof FilterInterface) |
|
| 200 | + { |
|
| 191 | 201 | throw new CompilerException('Paginator can not be null'); |
| 192 | 202 | } |
| 193 | 203 | |
| 194 | 204 | $withValue = $paginator->withValue($this->getOption(static::KEY_PAGINATE)); |
| 195 | - if ($withValue === null) { |
|
| 205 | + if ($withValue === null) |
|
| 206 | + { |
|
| 196 | 207 | throw new CompilerException('Paginator can not be null'); |
| 197 | 208 | } |
| 198 | 209 | |
@@ -211,7 +222,8 @@ discard block |
||
| 211 | 222 | protected function getOptionArray(string $option): array |
| 212 | 223 | { |
| 213 | 224 | $result = $this->getOption($option); |
| 214 | - if (!is_array($result)) { |
|
| 225 | + if (!is_array($result)) |
|
| 226 | + { |
|
| 215 | 227 | return []; |
| 216 | 228 | } |
| 217 | 229 | |
@@ -226,7 +238,8 @@ discard block |
||
| 226 | 238 | */ |
| 227 | 239 | protected function getOption(string $option) |
| 228 | 240 | { |
| 229 | - if ($this->input->hasValue($option)) { |
|
| 241 | + if ($this->input->hasValue($option)) |
|
| 242 | + { |
|
| 230 | 243 | return $this->input->getValue($option); |
| 231 | 244 | } |
| 232 | 245 | |
@@ -66,14 +66,17 @@ discard block |
||
| 66 | 66 | { |
| 67 | 67 | $result = $core->callAction($controller, $action, $parameters); |
| 68 | 68 | |
| 69 | - if (is_iterable($result)) { |
|
| 69 | + if (is_iterable($result)) |
|
| 70 | + { |
|
| 70 | 71 | $schema = $this->getSchema($controller, $action); |
| 71 | - if ($schema !== null) { |
|
| 72 | + if ($schema !== null) |
|
| 73 | + { |
|
| 72 | 74 | $grid = $this->makeFactory($schema) |
| 73 | 75 | ->withDefaults($schema['defaults']) |
| 74 | 76 | ->create($result, $schema['grid']); |
| 75 | 77 | |
| 76 | - if ($schema['view'] !== null) { |
|
| 78 | + if ($schema['view'] !== null) |
|
| 79 | + { |
|
| 77 | 80 | $grid = $grid->withView($schema['view']); |
| 78 | 81 | } |
| 79 | 82 | |
@@ -93,14 +96,18 @@ discard block |
||
| 93 | 96 | private function getSchema(string $controller, string $action): ?array |
| 94 | 97 | { |
| 95 | 98 | $key = sprintf('%s:%s', $controller, $action); |
| 96 | - if (array_key_exists($key, $this->cache)) { |
|
| 99 | + if (array_key_exists($key, $this->cache)) |
|
| 100 | + { |
|
| 97 | 101 | return $this->cache[$key]; |
| 98 | 102 | } |
| 99 | 103 | |
| 100 | 104 | $this->cache[$key] = null; |
| 101 | - try { |
|
| 105 | + try |
|
| 106 | + { |
|
| 102 | 107 | $method = new \ReflectionMethod($controller, $action); |
| 103 | - } catch (\ReflectionException $e) { |
|
| 108 | + } |
|
| 109 | + catch (\ReflectionException $e) |
|
| 110 | + { |
|
| 104 | 111 | return null; |
| 105 | 112 | } |
| 106 | 113 | |
@@ -108,7 +115,8 @@ discard block |
||
| 108 | 115 | |
| 109 | 116 | /** @var DataGrid $dataGrid */ |
| 110 | 117 | $dataGrid = $reader->getMethodAnnotation($method, DataGrid::class); |
| 111 | - if ($dataGrid === null) { |
|
| 118 | + if ($dataGrid === null) |
|
| 119 | + { |
|
| 112 | 120 | return null; |
| 113 | 121 | } |
| 114 | 122 | |
@@ -129,19 +137,23 @@ discard block |
||
| 129 | 137 | 'factory' => $dataGrid->factory, |
| 130 | 138 | ]; |
| 131 | 139 | |
| 132 | - if (is_string($schema['view'])) { |
|
| 140 | + if (is_string($schema['view'])) |
|
| 141 | + { |
|
| 133 | 142 | $schema['view'] = $this->container->get($schema['view']); |
| 134 | 143 | } |
| 135 | 144 | |
| 136 | - if ($schema['defaults'] === [] && method_exists($schema['grid'], 'getDefaults')) { |
|
| 145 | + if ($schema['defaults'] === [] && method_exists($schema['grid'], 'getDefaults')) |
|
| 146 | + { |
|
| 137 | 147 | $schema['defaults'] = $schema['grid']->getDefaults(); |
| 138 | 148 | } |
| 139 | 149 | |
| 140 | - if ($schema['options'] === [] && method_exists($schema['grid'], 'getOptions')) { |
|
| 150 | + if ($schema['options'] === [] && method_exists($schema['grid'], 'getOptions')) |
|
| 151 | + { |
|
| 141 | 152 | $schema['options'] = $schema['grid']->getOptions(); |
| 142 | 153 | } |
| 143 | 154 | |
| 144 | - if ($schema['view'] === null && is_callable($schema['grid'])) { |
|
| 155 | + if ($schema['view'] === null && is_callable($schema['grid'])) |
|
| 156 | + { |
|
| 145 | 157 | $schema['view'] = $schema['grid']; |
| 146 | 158 | } |
| 147 | 159 | |
@@ -150,9 +162,11 @@ discard block |
||
| 150 | 162 | |
| 151 | 163 | private function makeFactory(array $schema): GridFactoryInterface |
| 152 | 164 | { |
| 153 | - if (!empty($schema['factory']) && $this->container->has($schema['factory'])) { |
|
| 165 | + if (!empty($schema['factory']) && $this->container->has($schema['factory'])) |
|
| 166 | + { |
|
| 154 | 167 | $factory = $this->container->get($schema['factory']); |
| 155 | - if ($factory instanceof GridFactoryInterface) { |
|
| 168 | + if ($factory instanceof GridFactoryInterface) |
|
| 169 | + { |
|
| 156 | 170 | return $factory; |
| 157 | 171 | } |
| 158 | 172 | } |