Skip to content

Commit

Permalink
Merge pull request #72 from krzysztof-gzocha/pass-criteria-to-transfo…
Browse files Browse the repository at this point in the history
…rmer

Pass previous criteria to chain transformer
  • Loading branch information
krzysztof-gzocha authored Jul 5, 2016
2 parents 2cd2cbe + 824ab12 commit 5d42127
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
4 changes: 3 additions & 1 deletion docs/chain-search.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ injected into the ``$statisticSearcher``:
/**
* @param mixed $results
*/
public function transform($results)
public function transform($results, CriteriaCollectionInterface $criteria)
{
// Assuming that UserIdsCriteria will holds an array of user IDs
$userIdsCriteria = new UserIdsCriteria(array_map(
Expand All @@ -61,6 +61,8 @@ injected into the ``$statisticSearcher``:
$results
));
// We can use some criteria from previous search, but in this scenario we do not need them.
return new CriteriaCollection($userIdsCriteria);
}
Expand Down
2 changes: 1 addition & 1 deletion src/KGzocha/Searcher/Chain/ChainSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function search(

$previousResults = $cell->getSearcher()->search($previousCriteria);
if ($cell->hasTransformer()) {
$previousCriteria = $cell->getTransformer()->transform($previousResults);
$previousCriteria = $cell->getTransformer()->transform($previousResults, $previousCriteria);
}

if ($cell->getName()) {
Expand Down
4 changes: 3 additions & 1 deletion src/KGzocha/Searcher/Chain/EndTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace KGzocha\Searcher\Chain;

use KGzocha\Searcher\Criteria\Collection\CriteriaCollectionInterface;

/**
* Use this class to indicate that there is no more searchers in the chain.
* It is like a NullObject. It does not do much.
Expand All @@ -13,7 +15,7 @@ final class EndTransformer implements TransformerInterface
/**
* @inheritDoc
*/
public function transform($results)
public function transform($results, CriteriaCollectionInterface $criteria)
{
throw new \RuntimeException(
'Transform method on EmptyTransformer should never be called.'
Expand Down
5 changes: 3 additions & 2 deletions src/KGzocha/Searcher/Chain/TransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use KGzocha\Searcher\Criteria\Collection\CriteriaCollectionInterface;

/**
* Only responsibility of services implementing this interface is to transform results from previous
* Only responsibility of services implementing this interface is to transform results and(/or) criteria from previous
* search to CriteriaCollectionInterface that can be used in next search.
*
* @author Krzysztof Gzocha <[email protected]>
Expand All @@ -14,10 +14,11 @@ interface TransformerInterface
{
/**
* @param mixed $results
* @param CriteriaCollectionInterface $criteria
*
* @return CriteriaCollectionInterface
*/
public function transform($results);
public function transform($results, CriteriaCollectionInterface $criteria);

/**
* Important! Results might be null when cell will be used as first one.
Expand Down
3 changes: 2 additions & 1 deletion tests/Chain/EndTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace KGzocha\Searcher\Test\Chain;

use KGzocha\Searcher\Chain\EndTransformer;
use KGzocha\Searcher\Criteria\Collection\CriteriaCollection;

/**
* @author Krzysztof Gzocha <[email protected]>
Expand All @@ -21,6 +22,6 @@ public function testSkipMethod()
public function testTransformMethod()
{
$transformer = new EndTransformer();
$transformer->transform([]);
$transformer->transform([], new CriteriaCollection());
}
}

0 comments on commit 5d42127

Please sign in to comment.