Skip to content

Commit

Permalink
Add SearchResultSet
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveRandom committed Feb 16, 2017
1 parent e2f3d32 commit a541179
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/SearchResultSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace Room11\GoogleSearcher;

class SearchResultSet
{
private $searchTerm;
private $searchUrl;
private $results;

public function __construct(string $searchTerm, string $searchUrl, array $results)
{
$this->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;
}
}
11 changes: 9 additions & 2 deletions src/Searcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -135,10 +141,11 @@ public function __construct(HttpClient $httpClient)

/**
* @param string $term
* @return Promise<SearchResult[]>
* @return Promise<SearchResultSet>
*/
public function search(string $term): Promise
{
/** @noinspection PhpParamsInspection */
return resolve($this->doSearch($term));
}
}

0 comments on commit a541179

Please sign in to comment.