. */ declare(strict_types=1); namespace App\Services\InfoProviderSystem; use App\Services\InfoProviderSystem\DTOs\SearchResultDTO; use App\Services\InfoProviderSystem\Providers\InfoProviderInterface; class PartInfoRetriever { public function __construct(private readonly ProviderRegistry $provider_registry) { } /** * Search for a keyword in the given providers * @param string[]|InfoProviderInterface[] $providers A list of providers to search in, either as provider keys or as provider instances * @param string $keyword The keyword to search for * @return SearchResultDTO[] The search results */ public function searchByKeyword(string $keyword, array $providers): array { $results = []; foreach ($providers as $provider) { if (is_string($provider)) { $provider = $this->provider_registry->getProviderByKey($provider); } if (!$provider instanceof InfoProviderInterface) { throw new \InvalidArgumentException("The provider must be either a provider key or a provider instance!"); } /** @noinspection SlowArrayOperationsInLoopInspection */ $results = array_merge($results, $provider->searchByKeyword($keyword)); } return $results; } }