<commit_before><?php


namespace Ems\App\View;

use Illuminate\Http\Request;
use Illuminate\Contracts\View\View;
use Versatile\Search\Contracts\Search;
use Versatile\View\Contracts\ModelPresenter;


class AutocompleteContentMorpher
{

    protected $request;

    protected $scaffold;

    public function __construct(Request $request, ModelPresenter $scaffold)
    {
        $this->request = $request;
        $this->scaffold = $scaffold;
    }

    public function morphIfQueried($response, $contentType)
    {
        if (!$search = $this->findSearch($response)) {
            return;
        }

        return $this->morphToAutocomplete($search, $response);
    }

    protected function morphToAutocomplete(Search $search, $response)
    {

        $search->replaceKeys('company','forename','surname');

        $result = $search->paginate([], 20);

        $this->addShortNames($result);

        $response->setContent($result);

    }

    protected function addShortNames($result)
    {
        foreach ($result as $model) {
            $model->setAttribute('short_name', $this->scaffold->shortName($model, ModelPresenter::VIEW_PREVIEW));
        }
    }

    protected function findSearch($response)
    {<commit_msg>Build custom array to add keys<commit_after><?php


namespace Ems\App\View;

use Illuminate\Http\Request;
use Illuminate\Contracts\View\View;
use Versatile\Search\Contracts\Search;
use Versatile\View\Contracts\ModelPresenter;


class AutocompleteContentMorpher
{

    protected $request;

    protected $scaffold;

    public function __construct(Request $request, ModelPresenter $scaffold)
    {
        $this->request = $request;
        $this->scaffold = $scaffold;
    }

    public function morphIfQueried($response, $contentType)
    {
        if (!$search = $this->findSearch($response)) {
            return;
        }

        return $this->morphToAutocomplete($search, $response);
    }

    protected function morphToAutocomplete(Search $search, $response)
    {

        $keys = $this->scaffold->keys($search->modelClass(), 
ModelPresenter::VIEW_PREVIEW );

        $search->replaceKeys($keys) ;

        $result = $search->paginate([], 20);

        $this->addShortNames($result);

        $array = $result->toArray();

        $array['keys'] = $keys;

        $response->setContent($array);

    }

    protected function addShortNames($result)
    {
        foreach ($result as $model) {
            $model->setAttribute('short_name', $this->scaffold->shortName($model, ModelPresenter::VIEW_PREVIEW));
        }
    }

    protected function findSearch($response)
    {
{'fraction_non_alphanumeric': 0.1314176245210728, 'fraction_numerical': 0.0015325670498084292, 'mean_word_length': 3.0543478260869565, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '2486977', 'n_tokens_mistral': 789, 'n_tokens_neox': 756, 'n_words': 136}