1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2017 <[email protected]> |
8
|
|
|
* All rights reserved |
9
|
|
|
* |
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
11
|
|
|
* free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* The GNU General Public License can be found at |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
18
|
|
|
* |
19
|
|
|
* This script is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
25
|
|
|
***************************************************************/ |
26
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The Sorting ParameterProvider is responsible to build the solr query parameters |
31
|
|
|
* that are needed for the sorting. |
32
|
|
|
* |
33
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder |
34
|
|
|
*/ |
35
|
|
|
class Sorting extends AbstractDeactivatableParameterBuilder implements ParameterBuilder |
36
|
|
|
{ |
37
|
|
|
const SORT_ASC = 'ASC'; |
38
|
|
|
const SORT_DESC = 'DESC'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $sortField = ''; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Debug constructor. |
47
|
|
|
* |
48
|
|
|
* @param bool $isEnabled |
49
|
|
|
* @param string $sortField |
50
|
|
|
*/ |
51
|
|
|
public function __construct($isEnabled = false, $sortField = '') |
52
|
|
|
{ |
53
|
|
|
$this->isEnabled = $isEnabled; |
54
|
|
|
$this->setSortField($sortField); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Query $query |
59
|
|
|
* @return Query |
60
|
|
|
*/ |
61
|
|
|
public function build(Query $query): Query |
62
|
|
|
{ |
63
|
|
|
$isEnabledAndField = $this->isEnabled && !empty($this->sortField); |
64
|
|
|
if (!$isEnabledAndField) { |
65
|
|
|
$query->getQueryParametersContainer()->remove('sort'); |
66
|
|
|
return $query; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$query->getQueryParametersContainer()->set('sort', $this->sortField); |
70
|
|
|
return $query; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return Sorting |
75
|
|
|
*/ |
76
|
|
|
public static function getEmpty() |
77
|
|
|
{ |
78
|
|
|
return new Sorting(false); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $sortField |
83
|
|
|
*/ |
84
|
|
|
public function setSortField($sortField) |
85
|
|
|
{ |
86
|
|
|
$sortField = $this->removeRelevanceSortField($sortField); |
87
|
|
|
$this->sortField = $sortField; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Removes the relevance sort field if present in the sorting field definition. |
92
|
|
|
* |
93
|
|
|
* @param string $sorting |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
protected function removeRelevanceSortField($sorting) |
97
|
|
|
{ |
98
|
|
|
$sortParameter = $sorting; |
99
|
|
|
list($sortField) = explode(' ', $sorting); |
100
|
|
|
if ($sortField === 'relevance') { |
101
|
|
|
$sortParameter = ''; |
102
|
|
|
return $sortParameter; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $sortParameter; |
106
|
|
|
} |
107
|
|
|
} |