<commit_before><?php

namespace App\Http\Search;

class Response
{

    /**
     * Response as it came back from Elasticsearch
     *
     * @var array
     */
    public $searchResponse;


    /**
     * Params passed to Elasticsearch
     *
     * @var array
     */
    public $searchParams;


    /**
     * Create a new request instance.
     *
     * @param array $searchResponse Response as it came back from Elasticsearch
     * @param array $searchParams Params passed to Elasticsearch
     *
     * @return void
     */
    public function __construct(array $searchResponse, array $searchParams)
    {
        $this->searchResponse = $searchResponse;
        $this->searchParams = $searchParams;
    }


    /**
     * Transform response for search queries.
     *
     * @return array
     */
    public function getSearchResponse()
    {

        $response = array_merge(
            $this->paginate(),
            $this->data()
        );

        $response = array_merge(
            $response,
            $this->suggest()
        );

        $response = array_merge(
            $response,
            $this->aggregate()
        );

        $response = array_merge(
            [
                'preference' => $this->searchParams['preference'] ?? null,
            ],
            $response
        );

        return $response;

    }


    /**
     * Transform response for explain queries.
     *
     * @return array
     */
    public function getExplainResponse()
    {

        return $this->searchResponse;

    }


    /**
     * Transform response for autocomplete queries.
     *
     * @return array
     */
    public function getAutocompleteResponse() {

        return $this->searchResponse;

    }


    /**
     * Temporary method to demonstrate fixed autocomplete.
     *
     * @return array
     */
    public function getAutocompleteResponse2() {

        // Defaulting to [] is safe, but ineffecient: catch empty `q` earlier!
        return $this->suggest()['suggest']['autocomplete'] ?? [];

    }


    /**
     * Add pagination to response.
     *
     * @return array
     */
    public function paginate()
    {

        // We assume that `size` and `from` have been set via getPaginationParams()
        // This method should not be used for endpoints that return no results

        // LengthAwarePaginator has trouble here
        $total = $this->searchResponse['hits']['total'];
        $limit = $this->searchParams['size'] ?? 10;
        $offset = $this->searchParams['from'] ?? 0;

        $total_pages = ceil( $total / $limit );
        $current_page = floor( $offset / $limit ) + 1;

        $pagination = [
            'total' => $total,
            'limit' => $limit,
            'offset' => $offset,
            'total_pages' => $total_pages,
            'current_page' => $current_page,
        ];

        return [
            'pagination' => $pagination
        ];

    }


    /**
     * Add data (i.e. hits, results) to response.
     *
     * @return array
     */
    public function data()
    {

        $hits = $this->searchResponse['hits']['hits'];
        $results = [];

        // Reduce to just the _source objects
        foreach( $hits as $hit ) {

            $result = [<commit_msg>Fix search pagination when size is zero<commit_after><?php

namespace App\Http\Search;

class Response
{

    /**
     * Response as it came back from Elasticsearch
     *
     * @var array
     */
    public $searchResponse;


    /**
     * Params passed to Elasticsearch
     *
     * @var array
     */
    public $searchParams;


    /**
     * Create a new request instance.
     *
     * @param array $searchResponse Response as it came back from Elasticsearch
     * @param array $searchParams Params passed to Elasticsearch
     *
     * @return void
     */
    public function __construct(array $searchResponse, array $searchParams)
    {
        $this->searchResponse = $searchResponse;
        $this->searchParams = $searchParams;
    }


    /**
     * Transform response for search queries.
     *
     * @return array
     */
    public function getSearchResponse()
    {

        $response = array_merge(
            $this->paginate(),
            $this->data()
        );

        $response = array_merge(
            $response,
            $this->suggest()
        );

        $response = array_merge(
            $response,
            $this->aggregate()
        );

        $response = array_merge(
            [
                'preference' => $this->searchParams['preference'] ?? null,
            ],
            $response
        );

        return $response;

    }


    /**
     * Transform response for explain queries.
     *
     * @return array
     */
    public function getExplainResponse()
    {

        return $this->searchResponse;

    }


    /**
     * Transform response for autocomplete queries.
     *
     * @return array
     */
    public function getAutocompleteResponse() {

        return $this->searchResponse;

    }


    /**
     * Temporary method to demonstrate fixed autocomplete.
     *
     * @return array
     */
    public function getAutocompleteResponse2() {

        // Defaulting to [] is safe, but ineffecient: catch empty `q` earlier!
        return $this->suggest()['suggest']['autocomplete'] ?? [];

    }


    /**
     * Add pagination to response.
     *
     * @return array
     */
    public function paginate()
    {

        // We assume that `size` and `from` have been set via getPaginationParams()
        // This method should not be used for endpoints that return no results

        // LengthAwarePaginator has trouble here
        $total = $this->searchResponse['hits']['total'];
        $limit = $this->searchParams['size'] ?? 10;
        $offset = $this->searchParams['from'] ?? 0;

        // Avoid division by zero
        if( $limit > 0 ) {

            $total_pages = ceil( $total / $limit );
            $current_page = floor( $offset / $limit ) + 1;

        } else {

            $total_pages = null;
            $current_page = null;

        }

        $pagination = [
            'total' => $total,
            'limit' => $limit,
            'offset' => $offset,
            'total_pages' => $total_pages,
            'current_page' => $current_page,
        ];

        return [
            'pagination' => $pagination
        ];

    }


    /**
     * Add data (i.e. hits, results) to response.
     *
     * @return array
     */
    public function data()
    {

        $hits = $this->searchResponse['hits']['hits'];
        $results = [];

        // Reduce to just the _source objects
        foreach( $hits as $hit ) {

            $result = [
{'fraction_non_alphanumeric': 0.11991051454138703, 'fraction_numerical': 0.0016405667412378823, 'mean_word_length': 1.678115015974441, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3281039', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1803, 'n_words': 474}