| Total Complexity | 7 |
| Total Lines | 48 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 17 | class TrieNode extends KeyValueNode |
||
| 18 | { |
||
| 19 | 1 | public function add(NodeInterface ...$nodes): NodeInterface |
|
| 20 | { |
||
| 21 | 1 | foreach ($nodes as $node) { |
|
| 22 | 1 | $data = $node->getValue(); |
|
|
|
|||
| 23 | |||
| 24 | 1 | $hash = hash('sha256', $node->getKey() . $data); |
|
| 25 | |||
| 26 | 1 | $node = new self($hash, substr($data, 0, 1)); |
|
| 27 | 1 | $parent = $this->append($node); |
|
| 28 | |||
| 29 | 1 | $dataWithoutFirstLetter = substr($data, 1); |
|
| 30 | |||
| 31 | 1 | if ('' < $dataWithoutFirstLetter) { |
|
| 32 | 1 | $parent->add(new self($hash, $dataWithoutFirstLetter)); |
|
| 33 | } else { |
||
| 34 | 1 | $nodes = [$node->getValue()]; |
|
| 35 | |||
| 36 | /** @var KeyValueNodeInterface $ancestor */ |
||
| 37 | 1 | foreach ($node->getAncestors() as $ancestor) { |
|
| 38 | 1 | $nodes[] = $ancestor->getValue(); |
|
| 39 | } |
||
| 40 | 1 | array_pop($nodes); |
|
| 41 | 1 | $node->append(new self($hash, strrev(implode('', $nodes)))); |
|
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | 1 | return $this; |
|
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @throws Exception |
||
| 50 | * |
||
| 51 | * @return NodeInterface|ValueNodeInterface |
||
| 52 | */ |
||
| 53 | 1 | private function append(ValueNodeInterface $node) |
|
| 65 | } |
||
| 66 | } |
||
| 67 |