diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f67f45ac..87dcc4f1 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1475,26 +1475,6 @@ parameters: count: 1 path: src/FormElement/TermInput/TermContainer.php - - - message: "#^Cannot access offset 'exclude' on mixed\\.$#" - count: 1 - path: src/FormElement/TermInput/TermSuggestions.php - - - - message: "#^Cannot access offset 'label' on mixed\\.$#" - count: 1 - path: src/FormElement/TermInput/TermSuggestions.php - - - - message: "#^Cannot access offset 'search' on mixed\\.$#" - count: 1 - path: src/FormElement/TermInput/TermSuggestions.php - - - - message: "#^Cannot access offset 'term' on mixed\\.$#" - count: 2 - path: src/FormElement/TermInput/TermSuggestions.php - - message: "#^Method ipl\\\\Web\\\\FormElement\\\\TermInput\\\\TermSuggestions\\:\\:__construct\\(\\) has parameter \\$provider with no value type specified in iterable type Traversable\\.$#" count: 1 @@ -1515,11 +1495,6 @@ parameters: count: 1 path: src/FormElement/TermInput/TermSuggestions.php - - - message: "#^Parameter \\#1 \\$terms of method ipl\\\\Web\\\\FormElement\\\\TermInput\\\\TermSuggestions\\:\\:setExcludeTerms\\(\\) expects array\\, mixed given\\.$#" - count: 1 - path: src/FormElement/TermInput/TermSuggestions.php - - message: "#^Property ipl\\\\Web\\\\FormElement\\\\TermInput\\\\TermSuggestions\\:\\:\\$provider type has no value type specified in iterable type Traversable\\.$#" count: 1 diff --git a/src/FormElement/TermInput/TermSuggestions.php b/src/FormElement/TermInput/TermSuggestions.php index a1dd6f50..26b00eac 100644 --- a/src/FormElement/TermInput/TermSuggestions.php +++ b/src/FormElement/TermInput/TermSuggestions.php @@ -10,6 +10,8 @@ use Psr\Http\Message\ServerRequestInterface; use Traversable; +use function ipl\Stdlib\yield_groups; + class TermSuggestions extends BaseHtmlElement { use Translation; @@ -210,6 +212,7 @@ public function forRequest(ServerRequestInterface $request): self return $this; } + /** @var array> $requestData */ $requestData = json_decode($request->getBody()->read(8192), true); if (empty($requestData)) { return $this; @@ -225,44 +228,46 @@ public function forRequest(ServerRequestInterface $request): self protected function assemble() { $groupingCallback = $this->getGroupingCallback(); + if ($groupingCallback) { + $provider = yield_groups($this->provider, $groupingCallback); + } else { + $provider = [null => $this->provider]; + } - $lastGroup = null; - foreach ($this->provider as $data) { - if ($groupingCallback !== null) { - $group = $groupingCallback($data); - if ($group && $group !== $lastGroup) { - $lastGroup = $group; - - $this->addHtml( - new HtmlElement( - 'li', - Attributes::create([ - 'class' => 'suggestion-title' - ]), - Text::create($group) - ) - ); - } + /** @var iterable>> $provider */ + foreach ($provider as $group => $suggestions) { + if ($group) { + $this->addHtml( + new HtmlElement( + 'li', + Attributes::create([ + 'class' => 'suggestion-title' + ]), + Text::create($group) + ) + ); } - $attributes = [ - 'type' => 'button', - 'value' => $data['label'] ?? $data['search'] - ]; - foreach ($data as $name => $value) { - $attributes["data-$name"] = $value; - } + foreach ($suggestions as $data) { + $attributes = [ + 'type' => 'button', + 'value' => $data['label'] ?? $data['search'] + ]; + foreach ($data as $name => $value) { + $attributes["data-$name"] = $value; + } - $this->addHtml( - new HtmlElement( - 'li', - null, + $this->addHtml( new HtmlElement( - 'input', - Attributes::create($attributes) + 'li', + null, + new HtmlElement( + 'input', + Attributes::create($attributes) + ) ) - ) - ); + ); + } } if ($this->isEmpty()) {