File tree Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 *
You can’t perform that action at this time.
0 commit comments