From 286778245121b0d20332189b489f7edd324074f4 Mon Sep 17 00:00:00 2001 From: Tim Nagel Date: Fri, 25 Mar 2016 12:22:53 +1100 Subject: [PATCH] Implement Result Transformations --- lib/Elastica/Result.php | 29 ++++++++- .../ResultSet/TransformingBuilder.php | 49 +++++++++++++++ lib/Elastica/Transformer/ChainTransformer.php | 31 ++++++++++ .../Transformer/TransformerInterface.php | 18 ++++++ .../ResultSet/TransformingBuilderTest.php | 59 +++++++++++++++++++ .../Test/Transformer/ChainTransformerTest.php | 33 +++++++++++ 6 files changed, 217 insertions(+), 2 deletions(-) create mode 100644 lib/Elastica/ResultSet/TransformingBuilder.php create mode 100644 lib/Elastica/Transformer/ChainTransformer.php create mode 100644 lib/Elastica/Transformer/TransformerInterface.php create mode 100644 test/lib/Elastica/Test/ResultSet/TransformingBuilderTest.php create mode 100644 test/lib/Elastica/Test/Transformer/ChainTransformerTest.php diff --git a/lib/Elastica/Result.php b/lib/Elastica/Result.php index 74236d4e4c..a30e7e74a0 100644 --- a/lib/Elastica/Result.php +++ b/lib/Elastica/Result.php @@ -18,6 +18,13 @@ class Result */ protected $_hit = array(); + /** + * If the result can be transformed, this stores the transformed result. + * + * @var mixed + */ + private $_transformed; + /** * Constructs a single results object. * @@ -70,6 +77,14 @@ public function getId() return $this->getParam('_id'); } + /** + * @return mixed + */ + public function getTransformed() + { + return $this->_transformed; + } + /** * Returns the type of the result. * @@ -191,11 +206,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')) { @@ -215,6 +230,16 @@ public function getDocument() return $doc; } + /** + * Sets the transformed object derived from the result. + * + * @param mixed $transformed + */ + public function setTransformed($transformed) + { + $this->_transformed = $transformed; + } + /** * Magic function to directly access keys inside the result. * diff --git a/lib/Elastica/ResultSet/TransformingBuilder.php b/lib/Elastica/ResultSet/TransformingBuilder.php new file mode 100644 index 0000000000..8863ece76d --- /dev/null +++ b/lib/Elastica/ResultSet/TransformingBuilder.php @@ -0,0 +1,49 @@ +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; + } +} diff --git a/lib/Elastica/Transformer/ChainTransformer.php b/lib/Elastica/Transformer/ChainTransformer.php new file mode 100644 index 0000000000..e3f16d28e7 --- /dev/null +++ b/lib/Elastica/Transformer/ChainTransformer.php @@ -0,0 +1,31 @@ +transformers = $transformers; + } + + /** + * @inheritdoc + */ + public function fromElasticSearch(ResultSet $resultSet) + { + foreach ($this->transformers as $transformer) { + $transformer->fromElasticSearch($resultSet); + } + } +} diff --git a/lib/Elastica/Transformer/TransformerInterface.php b/lib/Elastica/Transformer/TransformerInterface.php new file mode 100644 index 0000000000..de3a95695b --- /dev/null +++ b/lib/Elastica/Transformer/TransformerInterface.php @@ -0,0 +1,18 @@ +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); + } +} diff --git a/test/lib/Elastica/Test/Transformer/ChainTransformerTest.php b/test/lib/Elastica/Test/Transformer/ChainTransformerTest.php new file mode 100644 index 0000000000..184e60d25b --- /dev/null +++ b/test/lib/Elastica/Test/Transformer/ChainTransformerTest.php @@ -0,0 +1,33 @@ +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); + } +}