|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Search; |
|
4
|
|
|
|
|
5
|
|
|
use CultuurNet\UDB3\Offer\IriOfferIdentifierFactoryInterface; |
|
6
|
|
|
use CultuurNet\UDB3\Offer\OfferIdentifierCollection; |
|
7
|
|
|
use GuzzleHttp\Psr7\Request; |
|
8
|
|
|
use Http\Client\HttpClient; |
|
9
|
|
|
use Psr\Http\Message\UriInterface; |
|
10
|
|
|
use ValueObjects\Number\Integer; |
|
11
|
|
|
use ValueObjects\Web\Url; |
|
12
|
|
|
|
|
13
|
|
|
class Sapi3SearchService implements SearchServiceInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var UriInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $searchLocation; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var HttpClient |
|
22
|
|
|
*/ |
|
23
|
|
|
private $httpClient; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var IriOfferIdentifierFactoryInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $offerIdentifier; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param UriInterface $searchLocation |
|
32
|
|
|
* @param HttpClient $httpClient |
|
33
|
|
|
* @param IriOfferIdentifierFactoryInterface $offerIdentifier |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct( |
|
36
|
|
|
UriInterface $searchLocation, |
|
37
|
|
|
HttpClient $httpClient, |
|
38
|
|
|
IriOfferIdentifierFactoryInterface $offerIdentifier |
|
39
|
|
|
) { |
|
40
|
|
|
$this->searchLocation = $searchLocation; |
|
41
|
|
|
$this->httpClient = $httpClient; |
|
42
|
|
|
$this->offerIdentifier = $offerIdentifier; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function search($query, $limit = 30, $start = 0, $sort = null) |
|
46
|
|
|
{ |
|
47
|
|
|
$queryParameters = 'q=' . urlencode($query) . '&start=' . $start . '&limit=' . $limit; |
|
48
|
|
|
|
|
49
|
|
|
$offerQuery = $this->searchLocation->withQuery($queryParameters); |
|
50
|
|
|
|
|
51
|
|
|
$offerRequest = new Request( |
|
52
|
|
|
'GET', |
|
53
|
|
|
(string) $offerQuery |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
$searchResponseData = json_decode($this->httpClient |
|
57
|
|
|
->sendRequest($offerRequest) |
|
58
|
|
|
->getBody() |
|
59
|
|
|
->getContents()); |
|
60
|
|
|
|
|
61
|
|
|
$offerIds = array_reduce( |
|
62
|
|
|
$searchResponseData->{'member'}, |
|
63
|
|
|
function (OfferIdentifierCollection $offerIds, $item) { |
|
64
|
|
|
return $offerIds->with( |
|
65
|
|
|
$this->offerIdentifier->fromIri(Url::fromNative($item->{'@id'})) |
|
66
|
|
|
); |
|
67
|
|
|
}, |
|
68
|
|
|
new OfferIdentifierCollection() |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
return new Results($offerIds, new Integer($searchResponseData->{'totalItems'})); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|