-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves MPT static methods to a separate helper class (Path)
- Loading branch information
Showing
4 changed files
with
78 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dakujem\Oliva\MaterializedPath; | ||
|
||
use LogicException; | ||
|
||
/** | ||
* @author Andrej Rypak <[email protected]> | ||
*/ | ||
final class Path | ||
{ | ||
/** | ||
* Creates an extractor callable for tree builders that extracts vectors from materialized paths with delimiters. | ||
* These paths contain hierarchy information with variable-width levels delimited by a selected character. | ||
* The vectors are extracted by exploding the path string. | ||
* | ||
* @param string $delimiter The delimiter character. | ||
* @param callable $accessor An accessor callable that returns the raw path, signature `fn(mixed $data, mixed $inputIndex, TreeNodeContract $node): string`. | ||
* @return callable Vector extractor for the MPT builder. | ||
*/ | ||
public static function delimited(string $delimiter, callable $accessor): callable | ||
{ | ||
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 []; | ||
} | ||
if (!is_string($path)) { | ||
// TODO improve exceptions (index/path etc) | ||
throw new InvalidTreePath('Invalid tree path returned by the accessor. A string is required.'); | ||
} | ||
$path = trim($path, $delimiter); | ||
if ('' === $path) { | ||
return []; | ||
} | ||
return explode($delimiter, $path); | ||
}; | ||
} | ||
|
||
/** | ||
* Creates an extractor callable for tree builders that extracts vectors from materialized paths without delimiters. | ||
* These paths contain hierarchy information with constant character count per level of depth. | ||
* The vectors are extracted by splitting the path string by the given number. | ||
* | ||
* @param int $levelWidth The number of characters per level. | ||
* @param callable $accessor An accessor callable that returns the raw path, signature `fn(mixed $data, mixed $inputIndex, TreeNodeContract $node): string`. | ||
* @return callable Vector extractor for the MPT builder. | ||
*/ | ||
public static function fixed(int $levelWidth, callable $accessor): callable | ||
{ | ||
return function (mixed $data) use ($levelWidth, $accessor): array { | ||
$path = $accessor($data); | ||
if (null === $path || $path === '') { | ||
return []; | ||
} | ||
if (!is_string($path)) { | ||
// TODO improve exceptions (index/path etc) | ||
throw new InvalidTreePath('Invalid tree path returned by the accessor. A string is required.'); | ||
} | ||
return str_split($path, $levelWidth); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters