Skip to content

Commit

Permalink
TermSuggestions: Use ipl\Stdlib\yield_groups
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Sep 1, 2023
1 parent 07c38da commit bfa7bac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 57 deletions.
25 changes: 0 additions & 25 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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\\<string\\>, 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
Expand Down
69 changes: 37 additions & 32 deletions src/FormElement/TermInput/TermSuggestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Psr\Http\Message\ServerRequestInterface;
use Traversable;

use function ipl\Stdlib\yield_groups;

class TermSuggestions extends BaseHtmlElement
{
use Translation;
Expand Down Expand Up @@ -210,6 +212,7 @@ public function forRequest(ServerRequestInterface $request): self
return $this;
}

/** @var array<string, array<int|string, string>> $requestData */
$requestData = json_decode($request->getBody()->read(8192), true);
if (empty($requestData)) {
return $this;
Expand All @@ -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<?string, array<array<string, string>>> $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()) {
Expand Down

0 comments on commit bfa7bac

Please sign in to comment.