Skip to content

Commit

Permalink
Optimize Searcher::searchForModel and collections (#86)
Browse files Browse the repository at this point in the history
* Optimize Searcher::searchForModel and collections

* Fixed PHPDoc
  • Loading branch information
krzysztof-gzocha authored Aug 9, 2016
1 parent 58c5209 commit 4309e0e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
20 changes: 18 additions & 2 deletions src/KGzocha/Searcher/Criteria/Collection/CriteriaCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public function __construct($providedCriteria = [])
*/
public function getApplicableCriteria()
{
return array_filter(
return new self(array_filter(
$this->getCriteria(),
function(CriteriaInterface $criteria) {
return $criteria->shouldBeApplied();
}
);
));
}

/**
Expand All @@ -60,6 +60,22 @@ public function addCriteria(CriteriaInterface $criteria)
return $this;
}

/**
* @inheritDoc
*/
public function getIterator()
{
return new \ArrayIterator($this->criteria);
}

/**
* @inheritDoc
*/
public function count()
{
return count($this->criteria);
}

/**
* Will ensure that provided criteria are array or traversable object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
* @author Krzysztof Gzocha <[email protected]>
* @author Daniel Ribeiro <[email protected]>
*/
interface CriteriaCollectionInterface
interface CriteriaCollectionInterface extends \Countable, \IteratorAggregate
{
/**
* Will return array of CriteriaInterface
* that returns true in shouldBeApplied().
*
* @return CriteriaInterface[]
* @return CriteriaCollectionInterface
*/
public function getApplicableCriteria();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,28 @@ public function getCriteriaBuilders()
public function getCriteriaBuildersForContext(
SearchingContextInterface $searchingContext
) {
return array_filter(
return new self(array_filter(
$this->getCriteriaBuilders(),
function(CriteriaBuilderInterface $criteriaBuilder) use ($searchingContext) {
return $criteriaBuilder->supportsSearchingContext($searchingContext);
}
);
));
}

/**
* @inheritDoc
*/
public function getIterator()
{
return new \ArrayIterator($this->criteriaBuilders);
}

/**
* @inheritDoc
*/
public function count()
{
return count($this->criteriaBuilders);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @author Krzysztof Gzocha <[email protected]>
*/
interface CriteriaBuilderCollectionInterface
interface CriteriaBuilderCollectionInterface extends \Countable, \IteratorAggregate
{
/**
* @param CriteriaBuilderInterface $criteriaBuilder
Expand All @@ -30,7 +30,7 @@ public function getCriteriaBuilders();
/**
* @param SearchingContextInterface $searchingContext
*
* @return CriteriaBuilderInterface[]
* @return CriteriaBuilderCollectionInterface
*/
public function getCriteriaBuildersForContext(
SearchingContextInterface $searchingContext
Expand Down
18 changes: 10 additions & 8 deletions src/KGzocha/Searcher/Searcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,27 @@ public function __construct(
public function search(
CriteriaCollectionInterface $criteriaCollection
) {
$builders = $this
->builders
->getCriteriaBuildersForContext($this->searchingContext);

foreach ($criteriaCollection->getApplicableCriteria() as $criteria) {
$this->searchForModel($criteria, $this->searchingContext);
$this->searchForModel($criteria, $this->searchingContext, $builders);
}

return $this->searchingContext->getResults();
}

/**
* @param CriteriaInterface $criteria
* @param SearchingContextInterface $searchingContext
* @param CriteriaInterface $criteria
* @param SearchingContextInterface $searchingContext
* @param CriteriaBuilderCollectionInterface $builders
*/
private function searchForModel(
CriteriaInterface $criteria,
SearchingContextInterface $searchingContext
SearchingContextInterface $searchingContext,
CriteriaBuilderCollectionInterface $builders
) {
$builders = $this
->builders
->getCriteriaBuildersForContext($searchingContext);

foreach ($builders as $builder) {
if ($builder->allowsCriteria($criteria)) {
$builder->buildCriteria($criteria, $searchingContext);
Expand Down

0 comments on commit 4309e0e

Please sign in to comment.