diff --git a/modules/doc/library/Doc/Search/DocSearchIterator.php b/modules/doc/library/Doc/Search/DocSearchIterator.php new file mode 100644 index 000000000..a3a4d8504 --- /dev/null +++ b/modules/doc/library/Doc/Search/DocSearchIterator.php @@ -0,0 +1,122 @@ +search = $search; + parent::__construct($iterator); + } + + /** + * Accept sections that match the search + * + * @return bool Whether the current element of the iterator is acceptable + * through this filter + */ + public function accept() + { + $section = $this->getInnerIterator()->current(); + /** @type $section \Icinga\Module\Doc\DocSection */ + $matches = array(); + if (($match = $this->search->search($section->getTitle())) !== null) { + $matches[] = $match->setMatchType(DocSearchMatch::MATCH_HEADER); + } + foreach ($section->getContent() as $lineno => $line) { + if (($match = $this->search->search($line)) !== null) { + $matches[] = $match + ->setMatchType(DocSearchMatch::MATCH_CONTENT) + ->setLineno($lineno); + } + } + if (! empty($matches)) { + $this->matches = $matches; + return $this; + } + if ($section->hasChildren()) { + $this->matches = null; + return true; + } + return false; + } + + /** + * Get the search criteria + * + * @return DocSearch + */ + public function getSearch() + { + return $this->search; + } + + /** + * {@inheritdoc} + */ + public function getChildren() + { + return new static($this->getInnerIterator()->getChildren(), $this->search); + } + + /** + * {@inheritdoc} + */ + public function count() + { + return iterator_count($this); + } + + /** + * Whether the search did not yield any match + * + * @return bool + */ + public function isEmpty() + { + return $this->count() === 0; + } + + /** + * Get matches + * + * @return DocSearchMatch[]|null + */ + public function getMatches() + { + return $this->matches; + } +}