-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenTrieNode.php
More file actions
34 lines (27 loc) · 673 Bytes
/
Copy pathTokenTrieNode.php
File metadata and controls
34 lines (27 loc) · 673 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php declare(strict_types=1);
namespace uuf6429\PhpCsFixerBlockstring\InterpolationCodec;
final class TokenTrieNode
{
/** @var array<string, TokenTrieNode> */
public array $children = [];
public ?string $token = null;
/**
* @param array<string, mixed> $mapping
*/
public static function fromMapping(array $mapping): self
{
$root = new self();
foreach (array_keys($mapping) as $token) {
$root->insert($token);
}
return $root;
}
public function insert(string $token): void
{
$node = $this;
foreach (str_split($token) as $char) {
$node = $node->children[$char] ?? ($node->children[$char] = new self());
}
$node->token = $token;
}
}