-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Elastica\ResultSet; | ||
|
||
use Elastica\Query; | ||
use Elastica\Response; | ||
use Elastica\ResultSet; | ||
use Elastica\Transformer\TransformerInterface; | ||
|
||
class TransformingBuilder implements BuilderInterface | ||
{ | ||
/** | ||
* @var BuilderInterface | ||
*/ | ||
private $builder; | ||
|
||
/** | ||
* @var TransformerInterface | ||
*/ | ||
private $transformer; | ||
|
||
/** | ||
* @param BuilderInterface $builder | ||
* @param TransformerInterface $transformer | ||
*/ | ||
public function __construct(BuilderInterface $builder, TransformerInterface $transformer) | ||
{ | ||
$this->builder = $builder; | ||
$this->transformer = $transformer; | ||
} | ||
|
||
/** | ||
* 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->transformer->fromElasticSearch($resultSet); | ||
|
||
return $resultSet; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Elastica\Transformer; | ||
|
||
use Elastica\ResultSet; | ||
|
||
class ChainTransformer implements TransformerInterface | ||
{ | ||
/** | ||
* @var TransformerInterface[] | ||
*/ | ||
private $transformers; | ||
|
||
/** | ||
* @param TransformerInterface[] $transformers | ||
*/ | ||
public function __construct($transformers) | ||
{ | ||
$this->transformers = $transformers; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function fromElasticSearch(ResultSet $resultSet) | ||
{ | ||
foreach ($this->transformers as $transformer) { | ||
$transformer->fromElasticSearch($resultSet); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Elastica\Transformer; | ||
|
||
use Elastica\ResultSet; | ||
|
||
interface TransformerInterface | ||
{ | ||
/** | ||
* Iterates over a ResultSet setting transformed objects against Results if available. | ||
* | ||
* If a result has no transformed object or has a transformed object already set, the transformer | ||
* should leave the Result alone. | ||
* | ||
* @param ResultSet $resultSet | ||
*/ | ||
public function fromElasticSearch(ResultSet $resultSet); | ||
} |
59 changes: 59 additions & 0 deletions
59
test/lib/Elastica/Test/ResultSet/TransformingBuilderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Elastica\Test\ResultSet; | ||
|
||
use Elastica\Query; | ||
use Elastica\Response; | ||
use Elastica\ResultSet; | ||
use Elastica\ResultSet\BuilderInterface; | ||
use Elastica\ResultSet\TransformingBuilder; | ||
use Elastica\Transformer\TransformerInterface; | ||
use Elastica\Test\Base as BaseTest; | ||
|
||
/** | ||
* @group unit | ||
*/ | ||
class TransformingBuilderTest extends BaseTest | ||
{ | ||
/** | ||
* @var TransformingBuilder | ||
*/ | ||
private $builder; | ||
|
||
/** | ||
* @var BuilderInterface | ||
*/ | ||
private $innerBuilder; | ||
|
||
/** | ||
* @var TransformerInterface | ||
*/ | ||
private $transformer; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->innerBuilder = $this->getMock('Elastica\\ResultSet\\BuilderInterface'); | ||
$this->transformer = $this->getMock('Elastica\\Transformer\\TransformerInterface'); | ||
|
||
$this->builder = new TransformingBuilder($this->innerBuilder, $this->transformer); | ||
} | ||
|
||
public function testTransforms() | ||
{ | ||
$response = new Response(''); | ||
$query = new Query(); | ||
$resultSet = new ResultSet($response, $query, []); | ||
|
||
$this->innerBuilder->expects($this->once()) | ||
->method('buildResultSet') | ||
->with($response, $query) | ||
->willReturn($resultSet); | ||
$this->transformer->expects($this->once()) | ||
->method('fromElasticSearch') | ||
->with($resultSet); | ||
|
||
$this->builder->buildResultSet($response, $query); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
test/lib/Elastica/Test/Transformer/ChainTransformerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\Test\Base as BaseTest; | ||
use Elastica\Transformer\ChainTransformer; | ||
|
||
/** | ||
* @group unit | ||
*/ | ||
class ChainTransformerTest extends BaseTest | ||
{ | ||
public function testTransformer() | ||
{ | ||
$transformer = new ChainTransformer([ | ||
$transformer1 = $this->getMock('Elastica\Transformer\TransformerInterface'), | ||
$transformer2 = $this->getMock('Elastica\Transformer\TransformerInterface') | ||
]); | ||
$resultSet = new ResultSet(new Response(''), new Query(), []); | ||
|
||
$transformer1->expects($this->once()) | ||
->method('fromElasticSearch') | ||
->with($resultSet); | ||
$transformer2->expects($this->once()) | ||
->method('fromElasticSearch') | ||
->with($resultSet); | ||
|
||
$transformer->fromElasticSearch($resultSet); | ||
} | ||
} |