1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Solr\Filter; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
use Solr\Bridge\Manager; |
14
|
|
|
use Zend\Filter\Exception; |
15
|
|
|
use Zend\Filter\FilterInterface; |
16
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class AbstractPaginationQuery |
20
|
|
|
* |
21
|
|
|
* @author Anthonius Munthi <[email protected]> |
22
|
|
|
* @since 0.27 |
23
|
|
|
* @package Solr\Filter |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractPaginationQuery implements FilterInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $sortPropertiesMap = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Store property name and converter to be used |
34
|
|
|
* during result conversion |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $propertiesMap = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Manager |
42
|
|
|
*/ |
43
|
|
|
protected $manager = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* AbstractPaginationQuery constructor. |
47
|
|
|
* |
48
|
|
|
* @param Manager $manager |
49
|
|
|
*/ |
50
|
|
|
public function __construct(Manager $manager) |
51
|
|
|
{ |
52
|
|
|
$this->manager = $manager; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Filter query based on given value |
57
|
|
|
* |
58
|
|
|
* @param mixed $value |
59
|
|
|
* @return \SolrQuery |
60
|
|
|
*/ |
61
|
|
|
public function filter($value) |
62
|
|
|
{ |
63
|
|
|
$query = new \SolrQuery(); |
64
|
|
|
|
65
|
|
|
return $this->createQuery($value,$query); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns sort parameter to be used for query |
70
|
|
|
* |
71
|
|
|
* @param $sort |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
protected function filterSort($sort) |
75
|
|
|
{ |
76
|
|
|
if ('-' == $sort{0}) { |
77
|
|
|
$sortProp = substr($sort, 1); |
78
|
|
|
$sortDir = Manager::SORT_DESCENDING; |
79
|
|
|
} else { |
80
|
|
|
$sortProp = $sort; |
81
|
|
|
$sortDir = Manager::SORT_ASCENDING; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
if (isset($this->sortPropertiesMap[$sortProp])) { |
|
|
|
|
85
|
|
|
$sortProp = $this->sortPropertiesMap[$sortProp]; |
86
|
|
|
|
87
|
|
|
if (is_array($sortProp)) { |
88
|
|
|
return array_fill_keys($sortProp, $sortDir); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return array($sortProp => $sortDir); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returs an array key => value for this pagination filter |
97
|
|
|
* to define custom solr result handler |
98
|
|
|
* @return array |
99
|
|
|
* @codeCoverageIgnore |
100
|
|
|
*/ |
101
|
|
|
public function getPropertiesMap() |
102
|
|
|
{ |
103
|
|
|
return $this->propertiesMap; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Creates new instance for this filter |
108
|
|
|
* |
109
|
|
|
* @param ServiceLocatorInterface $sl |
110
|
|
|
* @return static |
111
|
|
|
*/ |
112
|
|
|
static public function factory(ServiceLocatorInterface $sl) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$manager = $sl->getServiceLocator()->get('Solr/Manager'); |
|
|
|
|
115
|
|
|
return new static($manager); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns class to be used for entity object creation |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
abstract public function getEntityClass(); |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param array $params |
127
|
|
|
* @param \SolrQuery $query |
128
|
|
|
* @return \SolrQuery |
129
|
|
|
*/ |
130
|
|
|
abstract public function createQuery(array $params,$query); |
131
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.