<?php

declare(strict_types=1);

namespace PpPhp\Core\Platform\Services\Definition;

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

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

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

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

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

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