Prevent duplicate/overwritten keys

This commit is contained in:
2024-10-07 00:02:15 +02:00
parent 54b00f5925
commit 46bf135446
3 changed files with 21 additions and 17 deletions

View File

@@ -15,8 +15,16 @@ class ObjectNode extends Node implements CollapsibleNode
$this->properties[$key] = $value;
}
public function hasKey(string $key): bool
{
return array_key_exists($key, $this->properties);
}
public function rename(string $key, string $newKey)
{
if (array_key_exists($newKey, $this->properties)) {
throw new \Exception("Key {$newKey} already exists");
}
$renamed = [];
foreach ($this->properties as $k=>$v) {
$renamed[($k==$key)?$newKey:$k] = $v;