<?php
declare(strict_types=1); namespace App\Domain\Tags; class InvertedIndex { private array $index; public function __construct(array $initialIndex = []) { $this->index = $initialIndex; } public function updateForResource(string $cardId, array $newTags): void { foreach ($this->index as $tag => $ids) { if (is_array($ids)) { $this->index[$tag] = array_values(array_filter($ids, fn($id) => $id !== $cardId)); } if (empty($this->index[$tag])) { unset($this->index[$tag]); } } foreach ($newTags as $tag) { if (trim($tag) === '') { continue; } if (!isset($this->index[$tag])) { $this->index[$tag] = []; } if (!in_array($cardId, $this->index[$tag], true)) { $this->index[$tag][] = $cardId; } } } public function toArray(): array { return $this->index; } } 