Skip to content

Commit

Permalink
Implement Result Processors
Browse files Browse the repository at this point in the history
  • Loading branch information
merk committed Apr 28, 2016
1 parent 0d03f1b commit 4d3971e
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file based on the
- Fix php notice on `\Elastica\Index::getAliases()` if index has no aliases #1078

### Added
- Added the concept of ResultSet Transformers. The Transformer adds more information to a Result, for example the original object or data that created the Result. #1066

### Improvements
- `Elastica\Type->deleteByQuery($query, $options)` $query param can be a query `array` again https://github.com/ruflin/Elastica/issues/1072
Expand Down
15 changes: 13 additions & 2 deletions lib/Elastica/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ public function getExplanation()
/**
* Returns Document.
*
* @return \Elastica\Document
* @return Document
*/
public function getDocument()
{
$doc = new \Elastica\Document();
$doc = new Document();
$doc->setData($this->getSource());
$hit = $this->getHit();
if ($this->hasParam('_source')) {
Expand All @@ -215,6 +215,17 @@ public function getDocument()
return $doc;
}

/**
* Sets a parameter on the hit.
*
* @param string $param
* @param mixed $value
*/
public function setParam($param, $value)
{
$this->_hit[$param] = $value;
}

/**
* Magic function to directly access keys inside the result.
*
Expand Down
35 changes: 35 additions & 0 deletions lib/Elastica/ResultSet/ChainProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Elastica\ResultSet;

use Elastica\ResultSet;

/**
* Allows multiple ProcessorInterface instances to operate on the same
* ResultSet, calling each in turn.
*/
class ChainProcessor implements ProcessorInterface
{
/**
* @var ProcessorInterface[]
*/
private $processors;

/**
* @param ProcessorInterface[] $processors
*/
public function __construct($processors)
{
$this->processors = $processors;
}

/**
* @inheritdoc
*/
public function process(ResultSet $resultSet)
{
foreach ($this->processors as $processor) {
$processor->process($resultSet);
}
}
}
48 changes: 48 additions & 0 deletions lib/Elastica/ResultSet/ProcessingBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Elastica\ResultSet;

use Elastica\Query;
use Elastica\Response;
use Elastica\ResultSet;

class ProcessingBuilder implements BuilderInterface
{
/**
* @var BuilderInterface
*/
private $builder;

/**
* @var ProcessorInterface
*/
private $processor;

/**
* @param BuilderInterface $builder
* @param ProcessorInterface $processor
*/
public function __construct(BuilderInterface $builder, ProcessorInterface $processor)
{
$this->builder = $builder;
$this->processor = $processor;
}

/**
* Runs any registered transformers on the ResultSet before
* returning it, allowing the transformers to inject additional
* data into each Result.
*
* @param Response $response
* @param Query $query
* @return ResultSet
*/
public function buildResultSet(Response $response, Query $query)
{
$resultSet = $this->builder->buildResultSet($response, $query);

$this->processor->process($resultSet);

return $resultSet;
}
}
16 changes: 16 additions & 0 deletions lib/Elastica/ResultSet/ProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Elastica\ResultSet;

use Elastica\ResultSet;

interface ProcessorInterface
{
/**
* Iterates over a ResultSet allowing a processor to iterate over any
* Results as required.
*
* @param ResultSet $resultSet
*/
public function process(ResultSet $resultSet);
}
33 changes: 33 additions & 0 deletions test/lib/Elastica/Test/ResultSet/ChainProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Elastica\Test\Transformer;

use Elastica\Query;
use Elastica\Response;
use Elastica\ResultSet;
use Elastica\ResultSet\ChainProcessor;
use Elastica\Test\Base as BaseTest;

/**
* @group unit
*/
class ChainProcessorTest extends BaseTest
{
public function testProcessor()
{
$processor = new ChainProcessor([
$processor1 = $this->getMock('Elastica\\ResultSet\\ProcessorInterface'),
$processor2 = $this->getMock('Elastica\\ResultSet\\ProcessorInterface')
]);
$resultSet = new ResultSet(new Response(''), new Query(), []);

$processor1->expects($this->once())
->method('process')
->with($resultSet);
$processor2->expects($this->once())
->method('process')
->with($resultSet);

$processor->process($resultSet);
}
}
57 changes: 57 additions & 0 deletions test/lib/Elastica/Test/ResultSet/ProcessingBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Elastica\Test\ResultSet;

use Elastica\Query;
use Elastica\Response;
use Elastica\ResultSet;
use Elastica\ResultSet\BuilderInterface;
use Elastica\Test\Base as BaseTest;

/**
* @group unit
*/
class ProcessingBuilderTest extends BaseTest
{
/**
* @var ResultSet\ProcessingBuilder
*/
private $builder;

/**
* @var BuilderInterface
*/
private $innerBuilder;

/**
* @var ResultSet\ProcessorInterface
*/
private $processor;

protected function setUp()
{
parent::setUp();

$this->innerBuilder = $this->getMock('Elastica\\ResultSet\\BuilderInterface');
$this->processor = $this->getMock('Elastica\\ResultSet\\ProcessorInterface');

$this->builder = new ResultSet\ProcessingBuilder($this->innerBuilder, $this->processor);
}

public function testProcessors()
{
$response = new Response('');
$query = new Query();
$resultSet = new ResultSet($response, $query, []);

$this->innerBuilder->expects($this->once())
->method('buildResultSet')
->with($response, $query)
->willReturn($resultSet);
$this->processor->expects($this->once())
->method('process')
->with($resultSet);

$this->builder->buildResultSet($response, $query);
}
}

0 comments on commit 4d3971e

Please sign in to comment.