<?php

declare(strict_types=1);

namespace PpPhp\Core\Platform\Services\Descriptor;

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

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

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

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

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

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