Skip to content

Commit

Permalink
delimited path extractor improved
Browse files Browse the repository at this point in the history
  • Loading branch information
dakujem committed Jan 15, 2024
1 parent 1474bf2 commit 5bcd01e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/MaterializedPath/TreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class TreeBuilder
{
public static function fixed(int $levelWidth, callable $accessor): callable
{
return function (mixed $data) use ($levelWidth, $accessor) {
return function (mixed $data) use ($levelWidth, $accessor): array {
$path = $accessor($data);
if (null === $path) {
return [];
Expand All @@ -62,7 +62,10 @@ public static function fixed(int $levelWidth, callable $accessor): callable

public static function delimited(string $delimiter, callable $accessor): callable
{
return function (mixed $data) use ($delimiter, $accessor) {
if (strlen($delimiter) !== 1) {
throw new LogicException('The delimiter must be a single character.');
}
return function (mixed $data) use ($delimiter, $accessor): array {
$path = $accessor($data);
if (null === $path) {
return [];
Expand All @@ -71,6 +74,10 @@ public static function delimited(string $delimiter, callable $accessor): callabl
// TODO improve exceptions (index/path etc)
throw new LogicException('Invalid path returned.');
}
$path = trim($path, $delimiter);
if ('' === $path) {
return [];
}
return explode($delimiter, $path);
};
}
Expand Down

0 comments on commit 5bcd01e

Please sign in to comment.