• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dakujem / oliva / 7768722820

03 Feb 2024 06:34PM UTC coverage: 85.621%. Remained the same
7768722820

push

github

dakujem
doc comment

393 of 459 relevant lines covered (85.62%)

4.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.79
/src/MaterializedPath/Path.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Dakujem\Oliva\MaterializedPath;
6

7
use Dakujem\Oliva\Exceptions\ConfigurationIssue;
8
use Dakujem\Oliva\Exceptions\InvalidTreePath;
9
use Dakujem\Oliva\TreeNodeContract;
10

11
/**
12
 * @author Andrej Rypak <xrypak@gmail.com>
13
 */
14
final class Path
15
{
16
    /**
17
     * Creates an extractor callable for tree builders that extracts vectors from materialized paths with delimiters.
18
     * These paths contain hierarchy information with variable-width levels delimited by a selected character.
19
     * The vectors are extracted by exploding the path string.
20
     *
21
     * @param string $delimiter The delimiter character.
22
     * @param callable $accessor An accessor callable that returns the raw path, signature `fn(mixed $data, mixed $inputIndex, TreeNodeContract $node): string`.
23
     * @return callable Vector extractor for the MPT builder.
24
     */
25
    public static function delimited(string $delimiter, callable $accessor): callable
26
    {
27
        if (strlen($delimiter) !== 1) {
5✔
28
            throw new ConfigurationIssue('The delimiter must be a single character.');
×
29
        }
30
        return function (mixed $data, mixed $inputIndex = null, ?TreeNodeContract $node = null) use (
5✔
31
            $delimiter,
5✔
32
            $accessor,
5✔
33
        ): array {
34
            $path = $accessor($data);
5✔
35
            if (null === $path) {
5✔
36
                return [];
×
37
            }
38
            if (!is_string($path)) {
5✔
39
                throw (new InvalidTreePath('Invalid tree path returned by the accessor. A string is required.'))
×
40
                    ->tag('path', $path)
×
41
                    ->tag('data', $data)
×
42
                    ->tag('index', $inputIndex)
×
43
                    ->tag('node', $node);
×
44
            }
45
            $path = trim($path, $delimiter);
5✔
46
            if ('' === $path) {
5✔
47
                return [];
5✔
48
            }
49
            return explode($delimiter, $path);
5✔
50
        };
5✔
51
    }
52

53
    /**
54
     * Creates an extractor callable for tree builders that extracts vectors from materialized paths without delimiters.
55
     * These paths contain hierarchy information with constant character count per level of depth.
56
     * The vectors are extracted by splitting the path string by the given number.
57
     *
58
     * @param int $levelWidth The number of characters per level.
59
     * @param callable $accessor An accessor callable that returns the raw path, signature `fn(mixed $data, mixed $inputIndex, TreeNodeContract $node): string`.
60
     * @return callable Vector extractor for the MPT builder.
61
     */
62
    public static function fixed(int $levelWidth, callable $accessor): callable
63
    {
64
        return function (mixed $data, mixed $inputIndex = null, ?TreeNodeContract $node = null) use (
5✔
65
            $levelWidth,
5✔
66
            $accessor,
5✔
67
        ): array {
68
            $path = $accessor($data);
5✔
69
            if (null === $path || $path === '') {
5✔
70
                return [];
5✔
71
            }
72
            if (!is_string($path)) {
5✔
73
                throw (new InvalidTreePath('Invalid tree path returned by the accessor. A string is required.'))
5✔
74
                    ->tag('path', $path)
5✔
75
                    ->tag('data', $data)
5✔
76
                    ->tag('index', $inputIndex)
5✔
77
                    ->tag('node', $node);
5✔
78
            }
79
            return str_split($path, $levelWidth);
5✔
80
        };
5✔
81
    }
82
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc