Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/Pagerfanta/Adapter/ElasticsearchAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Pagerfanta\Adapter;

use Elasticsearch\Client;

/**
* ElasticsearchAdapter.
*
* Used with the official Elasticsearch PHP library.
*
* @author Danny Sipos <[email protected]>
*/
class ElasticsearchAdapter implements AdapterInterface
{

/**
* The Elasticsearch query parameters.
*
* @var array
*/
private $params;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use 4 spaces for the indentation to match the coding standards of the library


/**
* The Elasticsearch results
*
* @var array
*/
private $results;

/**
* The Elasticsearch client.
*
* @var Client
*/
private $client;

/**
* Constructor.
*
* @param array $params The array of parameters to use for performing the search.
* @param Client $client The Elasticsearch client.
*/
public function __construct(array $params, Client $client)
{
$this->params = $params;
$this->client = $client;
}

/**
* {@inheritdoc}
*/
public function getNbResults()
{
if (!$this->results) {
$this->runQuery();
}
return $this->results['hits']['total'];
}

/**
* {@inheritdoc}
*/
public function getSlice($offset, $length)
{
$params = $this->params;
$params['from'] = $offset;
$params['size'] = $length;
$this->runQuery($params);
return $this->results;
}

/**
* Runs the query and stores the results.
*
* @param array $params
*/
private function runQuery($params = array())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest typehinting this argument

{
if (!$params) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be simplified, by passing $this->params explicitly in getNbResults

$params = $this->params;
}
$this->results = $this->client->search($params);
}
}