1<?php
2
3namespace Elastica\ResultSet;
4
5use Elastica\Query;
6use Elastica\Response;
7use Elastica\Result;
8use Elastica\ResultSet;
9
10class DefaultBuilder implements BuilderInterface
11{
12    /**
13     * Builds a ResultSet for a given Response.
14     *
15     * @param Response $response
16     * @param Query    $query
17     *
18     * @return ResultSet
19     */
20    public function buildResultSet(Response $response, Query $query): ResultSet
21    {
22        $results = $this->buildResults($response);
23        $resultSet = new ResultSet($response, $query, $results);
24
25        return $resultSet;
26    }
27
28    /**
29     * Builds individual result objects.
30     *
31     * @param Response $response
32     *
33     * @return Result[]
34     */
35    private function buildResults(Response $response): array
36    {
37        $data = $response->getData();
38        $results = [];
39
40        if (!isset($data['hits']['hits'])) {
41            return $results;
42        }
43
44        foreach ($data['hits']['hits'] as $hit) {
45            $results[] = new Result($hit);
46        }
47
48        return $results;
49    }
50}
51