diff --git a/src/SearchResultSet.php b/src/SearchResultSet.php new file mode 100644 index 0000000..13336a7 --- /dev/null +++ b/src/SearchResultSet.php @@ -0,0 +1,35 @@ +searchTerm = $searchTerm; + $this->searchUrl = $searchUrl; + $this->results = $results; + } + + public function getSearchTerm(): string + { + return $this->searchTerm; + } + + public function getSearchUrl(): string + { + return $this->searchUrl; + } + + /** + * @return SearchResult[] + */ + public function getResults(): array + { + return $this->results; + } +} diff --git a/src/Searcher.php b/src/Searcher.php index 8349a4d..7e22dbf 100644 --- a/src/Searcher.php +++ b/src/Searcher.php @@ -86,6 +86,10 @@ private function getSearchResults(\DOMNodeList $resultNodes, \DOMXPath $xpath): return $results; } + /** + * @param string $term + * @return SearchResultSet + */ private function doSearch(string $term) { $uri = $this->getSearchURL($term); @@ -123,9 +127,11 @@ private function doSearch(string $term) $xpath = new \DOMXPath($dom); $resultNodes = $xpath->query('//*[' . xpath_html_class('g') . ']'); - return $resultNodes->length > 0 + $results = $resultNodes->length > 0 ? $this->getSearchResults($resultNodes, $xpath) : []; + + return new SearchResultSet($term, $uri, $results); } public function __construct(HttpClient $httpClient) @@ -135,10 +141,11 @@ public function __construct(HttpClient $httpClient) /** * @param string $term - * @return Promise + * @return Promise */ public function search(string $term): Promise { + /** @noinspection PhpParamsInspection */ return resolve($this->doSearch($term)); } }