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 118acce
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1480,16 +1480,6 @@ parameters:
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
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, mixed>> $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 118acce

Please sign in to comment.