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    public function buildResultSet(Response $response, Query $query): ResultSet
16    {
17        $results = $this->buildResults($response);
18
19        return new ResultSet($response, $query, $results);
20    }
21
22    /**
23     * Builds individual result objects.
24     *
25     * @return Result[]
26     */
27    private function buildResults(Response $response): array
28    {
29        $data = $response->getData();
30        $results = [];
31
32        if (!isset($data['hits']['hits'])) {
33            return $results;
34        }
35
36        foreach ($data['hits']['hits'] as $hit) {
37            $results[] = new Result($hit);
38        }
39
40        return $results;
41    }
42}
43