Skip to content

Commit

Permalink
custom search interface SearcherInterface.php
Browse files Browse the repository at this point in the history
  • Loading branch information
GutsVadim committed May 10, 2017
1 parent 8a43999 commit 3ccbe76
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/SearchComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use bl\search\data\SearchResult;
use bl\search\interfaces\SearchInterface;
use bl\search\interfaces\SearcherInterface;

/**
* Component for search on site in Active Record models
Expand Down Expand Up @@ -65,10 +66,13 @@ class SearchComponent extends Component
*/
public function search($query) {
foreach($this->models as $model) {
/** @var ActiveRecordInterface|SearchInterface $ar */
/** @var ActiveRecordInterface|SearchInterface|SearcherInterface $ar */
$ar = Yii::createObject($model['class']);

if($ar instanceof SearchInterface && $ar instanceof ActiveRecordInterface) {
if($ar instanceof SearcherInterface && $ar instanceof ActiveRecordInterface) {
$activeQuery = $ar->search($query);
}
else if($ar instanceof SearchInterface && $ar instanceof ActiveRecordInterface) {
$searchFields = $ar->getSearchFields();
$activeQuery = $ar::find();

Expand All @@ -82,16 +86,17 @@ public function search($query) {
}
}

$modelObjects = $activeQuery->all();
if($modelObjects != null) {
$this->_currentModel = $ar;
$this->addToResult($modelObjects);
}
}
else {
$message = sprintf("%s should be instance of `%s` and `%s`", $ar, SearchInterface::class, ActiveRecordInterface::class);
throw new InvalidConfigException($message);
}

$modelObjects = $activeQuery->all();
if($modelObjects != null) {
$this->_currentModel = $ar;
$this->addToResult($modelObjects);
}
}

return $this->_result;
Expand Down
17 changes: 17 additions & 0 deletions src/interfaces/SearcherInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace bl\search\interfaces;
use yii\db\ActiveQueryInterface;

/**
* Interface for searching
*
* @author Gutsulyak Vadim <guts.vadim@gmail.com>
*/
interface SearcherInterface extends SearchInterface
{
/**
* @param string $query keyword for search
* @return ActiveQueryInterface
*/
public function search($query);
}

0 comments on commit 3ccbe76

Please sign in to comment.