<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\DI\Repository;

use PpPhp\Core\Platform\DI\Contracts\DefinitionInterface;

final class DefinitionRepository
{
    private array $definitions = [];

    public function add(DefinitionInterface $definition): void { $this->definitions[$definition->id()] = $definition; }
    public function has(string $id): bool { return isset($this->definitions[$id]); }
    public function get(string $id): ?DefinitionInterface { return $this->definitions[$id] ?? null; }
    public function all(): array { return $this->definitions; }
    public function count(): int { return count($this->definitions); }

    public function toArray(): array
    {
        return array_map(static fn (DefinitionInterface $d): array => $d->toArray(), array_values($this->definitions));
    }
}
