id = new Id(); $this->key = $key; $this->value = $value; $this->createdAt = new DateTime(); } public function getId(): Id { return $this->id; } public function getKey(): Key { return $this->key; } public function getValue(): Value { return $this->value; } public function setValue(Value $value): void { if (!$this->value->value) { $this->value = $value; return; } $current = json_decode($this->value->value, true); if (!$current || !is_array($current)) { $this->value = $value; return; } $new = json_decode($value->value, true); if (!$new || !is_array($new)) { $this->value = $value; return; } $this->value = new Value(json_encode( $this->replaceValue($current, $new) )); } public function getCreatedAt(): DateTimeInterface { return $this->createdAt; } public function getUpdatedAt(): ?DateTimeInterface { return $this->updatedAt; } #[ORM\PreUpdate] public function preUpdate(): void { $this->updatedAt = new DateTime(); } private function replaceValue( array $current, array $new ): array { foreach ($new as $key => $value) { if (is_array($value) && isset($current[$key]) && is_array($current[$key])) { // Check if the array is associative if (array_keys($current[$key]) !== range(0, count($current[$key]) - 1)) { $current[$key] = $this->replaceValue($current[$key], $value); } else { $current[$key] = $value; } } else { $current[$key] = $value; } } return $current; } }