|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KGzocha\Searcher; |
|
4
|
|
|
|
|
5
|
|
|
use KGzocha\Searcher\Context\SearchingContextInterface; |
|
6
|
|
|
use KGzocha\Searcher\FilterImposer\Collection\FilterImposerCollectionInterface; |
|
7
|
|
|
use KGzocha\Searcher\FilterModel\Collection\FilterModelCollectionInterface; |
|
8
|
|
|
use KGzocha\Searcher\FilterModel\FilterModelInterface; |
|
9
|
|
|
|
|
10
|
|
|
class Searcher implements SearcherInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var FilterImposerCollectionInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
private $imposerCollection; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var SearchingContextInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $searchingContext; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param FilterImposerCollectionInterface $imposerCollection |
|
24
|
|
|
* @param SearchingContextInterface $searchingContext |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct( |
|
27
|
|
|
FilterImposerCollectionInterface $imposerCollection, |
|
28
|
|
|
SearchingContextInterface $searchingContext |
|
29
|
|
|
) { |
|
30
|
|
|
$this->imposerCollection = $imposerCollection; |
|
31
|
|
|
$this->searchingContext = $searchingContext; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritdoc |
|
36
|
|
|
*/ |
|
37
|
|
|
public function search( |
|
38
|
|
|
FilterModelCollectionInterface $filterCollection |
|
39
|
|
|
) { |
|
40
|
|
|
foreach ($filterCollection->getImposedModels() as $filterModel) { |
|
41
|
|
|
$this->searchForModel($filterModel, $this->searchingContext); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $this->searchingContext->getResults(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param FilterModelInterface $filterModel |
|
49
|
|
|
* @param SearchingContextInterface $searchingContext |
|
50
|
|
|
*/ |
|
51
|
|
|
private function searchForModel( |
|
52
|
|
|
FilterModelInterface $filterModel, |
|
53
|
|
|
SearchingContextInterface $searchingContext |
|
54
|
|
|
) { |
|
55
|
|
|
$imposers = $this |
|
56
|
|
|
->imposerCollection |
|
57
|
|
|
->getFilterImposersForContext($searchingContext); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($imposers as $imposer) { |
|
60
|
|
|
if ($imposer->supportsModel($filterModel)) { |
|
61
|
|
|
$imposer->imposeFilter($filterModel, $searchingContext); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|