. */ declare(strict_types=1); namespace SP\Core\Bootstrap; use ArrayAccess; use SplObjectStorage; use ValueError; /** * Class PathsContext * * @template-implements ArrayAccess */ final readonly class PathsContext implements ArrayAccess { /** * @var SplObjectStorage */ private SplObjectStorage $paths; public function __construct() { $this->paths = new SplObjectStorage(); } /** * @inheritDoc */ public function offsetExists(mixed $offset): bool { return $this->paths->contains($offset); } /** * @inheritDoc */ public function offsetGet(mixed $offset): mixed { return $this->paths->offsetGet($offset); } /** * @inheritDoc */ public function offsetSet(mixed $offset, mixed $value): void { $this->addPath($offset, $value); } /** * @param Path $path * @param string $value * @return void */ public function addPath(Path $path, string $value): void { if ($this->paths->contains($path)) { throw new ValueError('Duplicated path found: ' . $path->name); } $this->paths->attach($path, $value); } /** * @param array $paths * @return void */ public function addPaths(array $paths): void { foreach ($paths as $pathSpec) { if (!is_array($pathSpec)) { throw new ValueError('Path spec must be an array'); } $this->addPath(...$pathSpec); } } /** * @inheritDoc */ public function offsetUnset(mixed $offset): void { $this->paths->offsetUnset($offset); } }