<?php

declare(strict_types=1);

namespace PpPhp\Core\Registry\Abstracts;

use PpPhp\Core\Registry\Contracts\RegistryInterface;

abstract class AbstractRegistry implements RegistryInterface
{
    protected array $items = [];
    public function register(string $key, object $definition): void
    {
        $this->items[$key] = $definition;
    }
    public function unregister(string $key): void
    {
        unset($this->items[$key]);
    }
    public function has(string $key): bool
    {
        return isset($this->items[$key]);
    }
    public function get(string $key): ?object
    {
        return $this->items[$key] ?? null;
    }
    public function all(): array
    {
        return $this->items;
    }
    public function clear(): void
    {
        $this->items = [];
    }
    public function count(): int
    {
        return count($this->items);
    }
    public function search(string $term): array
    {
        $term = strtolower($term);
        return array_filter($this->items, fn($k) => str_contains(strtolower((string)$k), $term), ARRAY_FILTER_USE_KEY);
    }
}
