1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of gpupo/search |
5
|
|
|
* |
6
|
|
|
* (c) Gilmar Pupo <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gpupo\Search; |
13
|
|
|
|
14
|
|
|
use Gpupo\Search\Paginator\Paginator; |
15
|
|
|
use Gpupo\Search\Query\QueryInterface; |
16
|
|
|
use Gpupo\Search\Result\Collection; |
17
|
|
|
use Gpupo\Search\Result\CollectionInterface; |
18
|
|
|
use Gpupo\Search\Sphinx\SphinxService; |
19
|
|
|
|
20
|
|
|
class Search extends SearchAbstract implements SearchInterface |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
protected static $_instance; |
23
|
|
|
|
24
|
|
View Code Duplication |
public static function getInstance() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
if (!isset(self::$_instance)) { |
27
|
|
|
$class = get_called_class(); |
28
|
|
|
self::$_instance = new $class(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return self::$_instance; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function findByQuery(QueryInterface $query) |
35
|
|
|
{ |
36
|
|
|
$collection = $this->getCollection( |
37
|
|
|
$query->getIndex(), |
38
|
|
|
$query->getFilters(), |
39
|
|
|
$query->getQueries(), |
40
|
|
|
$query->getFieldWeights(), |
41
|
|
|
$query->getLimit(), |
42
|
|
|
$query->getOffSet(), |
43
|
|
|
$query->getCountableAttributes() |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$paginator = new Paginator(); |
47
|
|
|
|
48
|
|
|
if ($query->getPaginator()) { |
49
|
|
|
$page = $query->getPaginator()->getCurrentPageNumber(); |
50
|
|
|
} else { |
51
|
|
|
$page = 1; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$paginator->paginateResult($collection, $page); |
55
|
|
|
$collection->setPaginator($paginator); |
56
|
|
|
|
57
|
|
|
return $collection; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function searchByKeyword($keyword) |
61
|
|
|
{ |
62
|
|
|
$results = $this->search( |
63
|
|
|
'produtoIndex', |
64
|
|
|
null, |
65
|
|
|
[[ |
66
|
|
|
'key' => '*', |
67
|
|
|
'values' => [ |
68
|
|
|
$keyword, |
69
|
|
|
], |
70
|
|
|
'strict' => false, |
71
|
|
|
]], |
72
|
|
|
null, |
73
|
|
|
20, |
74
|
|
|
0 |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
return $results; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getResultsByKeyword($keyword, CollectionInterface $collection) |
81
|
|
|
{ |
82
|
|
|
$results = $this->searchByKeyword($keyword); |
83
|
|
|
|
84
|
|
|
if ($results) { |
85
|
|
|
$collection->load($results); |
86
|
|
|
|
87
|
|
|
return $collection; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return CollectionInterface |
93
|
|
|
*/ |
94
|
|
|
public function factoryCollection(array $array) |
95
|
|
|
{ |
96
|
|
|
$collection = new Collection($array); |
97
|
|
|
|
98
|
|
|
return $collection; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Acesso ao Client Sphinx Search. |
103
|
|
|
* |
104
|
|
|
* @return \SphinxClient |
105
|
|
|
*/ |
106
|
|
|
public function getSphinxClient() |
107
|
|
|
{ |
108
|
|
|
return $this->getSphinxService()->getFreshClient(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getSphinxService() |
112
|
|
|
{ |
113
|
|
|
return SphinxService::getInstance(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|