|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkSearchBundle\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkSearchBundle\Entity\IndexItemRepository; |
|
6
|
|
|
use SumoCoders\FrameworkSearchBundle\Entity\SearchResult; |
|
7
|
|
|
use SumoCoders\FrameworkSearchBundle\Event\SearchEvent; |
|
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
9
|
|
|
|
|
10
|
|
|
class Search |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $eventDispatcher; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $term; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \SumoCoders\FrameworkSearchBundle\Entity\IndexItemRepository |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $repository; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param IndexItemRepository $repository |
|
29
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
30
|
|
|
* @param string $term |
|
|
|
|
|
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function __construct( |
|
33
|
|
|
IndexItemRepository $repository, |
|
34
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
35
|
|
|
$term = null |
|
36
|
|
|
) { |
|
37
|
4 |
|
$this->repository = $repository; |
|
38
|
4 |
|
$this->eventDispatcher = $eventDispatcher; |
|
39
|
4 |
|
$this->term = $term; |
|
40
|
4 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Executes the real search |
|
44
|
|
|
*/ |
|
45
|
4 |
|
public function search() |
|
46
|
|
|
{ |
|
47
|
|
|
// grab the ids and their number of occurence from the search index |
|
48
|
4 |
|
$idsAndWeightsPerClass = (array) $this->repository->search($this->term); |
|
49
|
|
|
|
|
50
|
|
|
// process the results, and sort them by weight/class |
|
51
|
4 |
|
$foundItems = array(); |
|
52
|
4 |
|
foreach ($idsAndWeightsPerClass as $class => &$row) { |
|
53
|
4 |
|
arsort($row); |
|
54
|
4 |
|
$foundItems[$class] = array_keys($row); |
|
55
|
4 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
// create the event, and add our findings |
|
58
|
4 |
|
$event = new SearchEvent(); |
|
59
|
4 |
|
$event->setFoundItems($foundItems); |
|
60
|
4 |
|
$this->eventDispatcher->dispatch('framework_search.search', $event); |
|
61
|
|
|
|
|
62
|
|
|
// sort the results based on their weights |
|
63
|
4 |
|
$results = $event->getResults(); |
|
64
|
4 |
|
return $this->sortResults($idsAndWeightsPerClass, $results); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Sort the results |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $idsAndWeightsPerClass |
|
71
|
|
|
* @param array $results |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
4 |
|
protected function sortResults(array $idsAndWeightsPerClass, array $results) |
|
75
|
|
|
{ |
|
76
|
4 |
|
$sortedItems = array(); |
|
77
|
|
|
|
|
78
|
4 |
|
foreach ($idsAndWeightsPerClass as $class => $ids) { |
|
79
|
4 |
|
if (isset($results[$class])) { |
|
80
|
3 |
|
foreach ($ids as $id => $weight) { |
|
81
|
3 |
|
if (isset($results[$class][$id])) { |
|
82
|
3 |
|
$result = $results[$class][$id]; |
|
83
|
3 |
|
$result->setWeight($weight); |
|
84
|
3 |
|
$sortedItems[] = $result; |
|
85
|
3 |
|
} |
|
86
|
3 |
|
} |
|
87
|
3 |
|
} |
|
88
|
4 |
|
} |
|
89
|
|
|
|
|
90
|
4 |
|
usort( |
|
91
|
4 |
|
$sortedItems, |
|
92
|
2 |
|
function (SearchResult $result1, SearchResult $result2) { |
|
93
|
2 |
|
if ($result1->getWeight() < $result2->getWeight()) { |
|
94
|
1 |
|
return 1; |
|
95
|
|
|
} |
|
96
|
1 |
|
if ($result1->getWeight() > $result2->getWeight()) { |
|
97
|
1 |
|
return -1; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
return 0; |
|
101
|
|
|
} |
|
102
|
4 |
|
); |
|
103
|
|
|
|
|
104
|
4 |
|
return $sortedItems; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.