Files
jsonedit/src/Tree/ObjectNode.php

48 lines
971 B
PHP
Raw Normal View History

2024-10-01 18:46:03 +02:00
<?php
namespace NoccyLabs\JsonEdit\Tree;
2024-10-01 18:46:03 +02:00
class ObjectNode extends Node implements CollapsibleNode
{
use CollapsibleNodeTrait;
public function __construct(public array $properties)
{
}
public function set(string $key, Node $value)
{
$this->properties[$key] = $value;
}
2024-10-02 00:53:11 +02:00
public function rename(string $key, string $newKey)
{
$renamed = [];
foreach ($this->properties as $k=>$v) {
$renamed[($k==$key)?$newKey:$k] = $v;
}
$this->properties = $renamed;
}
2024-10-01 18:46:03 +02:00
public function unset(string $key)
{
unset($this->properties[$key]);
}
public function __clone()
{
$properties = array_combine(
array_keys($this->properties),
array_map(fn($v) => clone $v, $this->properties)
);
return new ObjectNode($properties);
}
2024-10-01 21:40:13 +02:00
public function jsonSerialize(): mixed
{
return $this->properties;
}
2024-10-01 18:46:03 +02:00
}