Skip to content

Commit ad7214b

Browse files
committed
Rewrite NameFormatter to interface.
1 parent a1dc7df commit ad7214b

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/NameFormatter.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Baraja\SelectboxTree;
6+
7+
8+
interface NameFormatter
9+
{
10+
public function format(string $name): string;
11+
}

src/SelectboxTree.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ final class SelectboxTree
1111

1212
private int $maxDepth = 32;
1313

14+
private ?NameFormatter $nameFormatter = null;
15+
1416

1517
/**
1618
* Build tree of categories or items to simple-plain selectbox.
@@ -25,16 +27,15 @@ final class SelectboxTree
2527
* | Windows
2628
*
2729
* @param mixed[][] $data raw database result in format [{"id", "name", "parent_id"}]
28-
* @param callable(mixed $name): string|null $nameFormatter
2930
* @return string[] (id => user haystack)
3031
*/
31-
public function process(array $data, ?callable $nameFormatter = null): array
32+
public function process(array $data): array
3233
{
3334
$categories = [];
3435
foreach ($data as $item) {
3536
$categories[] = [
3637
'id' => $item['id'],
37-
'name' => (string) ($nameFormatter === null ? $item['name'] : $nameFormatter($item['name'])),
38+
'name' => $this->nameFormatter === null ? (string) $item['name'] : $this->nameFormatter->format($item['name']),
3839
'parent' => $item['parent_id'],
3940
];
4041
}
@@ -72,6 +73,12 @@ public function setMaxDepth(int $maxDepth): void
7273
}
7374

7475

76+
public function setNameFormatter(NameFormatter $nameFormatter): void
77+
{
78+
$this->nameFormatter = $nameFormatter;
79+
}
80+
81+
7582
/**
7683
* Build category tree to simple selectbox array.
7784
*

0 commit comments

Comments
 (0)