Add duplicate (^D), basic search, bugfixes
* Use ^D to duplicate array items * Use / to search, matching keys will be highlighted * Don't attempt to load non-existing files when passed on command line
This commit is contained in:
@@ -43,5 +43,42 @@ class Tree
|
||||
}
|
||||
}
|
||||
|
||||
public function search(string $search): array
|
||||
{
|
||||
return $this->searchNode(null, $this->root, $search);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @return array<Node>
|
||||
*/
|
||||
private function searchNode(?string $key, Node $node, string $search): array
|
||||
{
|
||||
$results = [];
|
||||
|
||||
if ($node instanceof ArrayNode) {
|
||||
if ($key && str_contains($key, $search)) {
|
||||
$results[] = $node;
|
||||
}
|
||||
foreach ($node->items as $index=>$item) {
|
||||
$results = [ ...$results, ...$this->searchNode($index, $item, $search) ];
|
||||
}
|
||||
} elseif ($node instanceof ObjectNode) {
|
||||
if ($key && str_contains($key, $search)) {
|
||||
$results[] = $node;
|
||||
}
|
||||
foreach ($node->properties as $key=>$item) {
|
||||
$results = [ ...$results, ...$this->searchNode($key, $item, $search) ];
|
||||
}
|
||||
} elseif ($node instanceof ValueNode) {
|
||||
if (str_contains($node->value, $search) || ($key && str_contains($key, $search))) {
|
||||
$results[] = $node;
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user