contentRepo = $contentRepo; $this->ngramFilter = $ngramFilter; $this->publishedTermsProvider = $publishedTermsProvider; $this->termCoveragePolicy = $termCoveragePolicy; } /** * Resolve candidate term rows for a taxonomy type. * * @param string $type 'category' or 'tag' (n-gram cache type). * @param string $query The URL slug / cleaned URL to prefilter against. * @return array Uniform term-row shape; same either way. */ public function getCandidateTermRows(string $type, string $query): array { if ($query !== '' && $this->ngramFilter !== null && $this->publishedTermsProvider !== null && $this->termCoveragePolicy !== null && $this->termCoveragePolicy->isReady($type)) { $candidates = $this->ngramFilter->findSimilarTermIds( $query, $type, self::NGRAM_PREFILTER_THRESHOLD, self::NGRAM_PREFILTER_MAX_CANDIDATES ); return $this->publishedTermsProvider->getTermsByIds(array_keys($candidates), $type); } return $this->fullScan($type); } /** * @param string $type * @return array */ private function fullScan(string $type): array { $repo = $this->contentRepo; if (!is_object($repo)) { return array(); } if ($type === self::TYPE_CATEGORY && method_exists($repo, 'getPublishedCategories')) { return $this->objectRows($repo->getPublishedCategories()); } if ($type === self::TYPE_TAG && method_exists($repo, 'getPublishedTags')) { return $this->objectRows($repo->getPublishedTags()); } return array(); } /** * @param mixed $rows * @return array */ private function objectRows($rows): array { if (!is_array($rows)) { return array(); } $objects = array(); foreach ($rows as $row) { if (is_object($row)) { $objects[] = $row; } } return $objects; } }