<?php

declare(strict_types=1);

namespace PpPhp\Core\Platform\Services\Metadata;

final class ServiceCollection
{
    /** @var array<string, ServiceMetadata> */
    private array $items = [];

    public function add(ServiceMetadata $metadata): void
    {
        $this->items[$metadata->id()] = $metadata;
    }

    public function has(string $id): bool
    {
        return isset($this->items[$id]);
    }

    public function get(string $id): ?ServiceMetadata
    {
        return $this->items[$id] ?? null;
    }

    /** @return ServiceMetadata[] */
    public function all(): array
    {
        return array_values($this->items);
    }

    public function toArray(): array
    {
        return array_map(static fn (ServiceMetadata $metadata): array => $metadata->toArray(), $this->all());
    }
}
