Nqxcode\LuceneSearch\Model\Config::models PHP Метод

models() публичный Метод

Get models by query hits.
public models ( ZendSearch\Lucene\Search\QueryHit[] $hits ) : Nqxcode\LuceneSearch\Support\Collection
$hits ZendSearch\Lucene\Search\QueryHit[]
Результат Nqxcode\LuceneSearch\Support\Collection
    public function models($hits)
    {
        $models = [];
        $groupedIdsAsKeys = $this->groupedSearchableIdsAsKeys($hits);
        foreach ($hits as $hit) {
            $model = $this->model($hit);
            $id = $model->{$model->getKeyName()};
            $searchableIds = array_get($groupedIdsAsKeys, get_class($model), []);
            if (isset($searchableIds[$id])) {
                $models[] = $model;
            }
        }
        return Collection::make($models);
    }

Usage Example

Пример #1
0
 public function testModels()
 {
     // Asserts for product model
     list($cKey, $cValue) = $this->config->classUidPair(new Product());
     list($pKey, $pValue) = $this->config->primaryKeyPair(new Product());
     $this->productRepoMock->shouldReceive('searchableIds')->andReturn([1, 2, 3])->byDefault();
     $hitMock = m::mock('ZendSearch\\Lucene\\Search\\QueryHit');
     $hitMock->{$cKey} = $cValue;
     $hitMock->{$pKey} = 1;
     $this->assertEquals([1], $this->config->models([$hitMock])->lists('id')->all());
     $hitMock->{$pKey} = 5;
     $this->assertEquals([], $this->config->models([$hitMock])->lists('id')->all());
     // Asserts for dummy model
     list($cKey, $cValue) = $this->config->classUidPair(new DummyModel());
     list($pKey, $pValue) = $this->config->primaryKeyPair(new DummyModel());
     $builderMock = m::mock(Builder::class);
     $this->dummyMock->shouldReceive('newQuery')->andReturn($builderMock);
     $builderMock->shouldReceive('lists')->with('pk')->andReturn([1, 2, 3])->byDefault();
     $hitMock = m::mock('ZendSearch\\Lucene\\Search\\QueryHit');
     $hitMock->{$cKey} = $cValue;
     $hitMock->{$pKey} = 3;
     $this->assertEquals([3], $this->config->models([$hitMock])->lists('pk')->all());
     $hitMock->{$pKey} = 10;
     $this->assertEquals([], $this->config->models([$hitMock])->lists('pk')->all());
 }