1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the TYPO3 CMS project. |
6
|
|
|
* |
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
9
|
|
|
* of the License, or any later version. |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please read the |
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
13
|
|
|
* |
14
|
|
|
* The TYPO3 project - inspiring people to share! |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\AbstractOptionsFacet; |
18
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet; |
19
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
20
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface; |
21
|
|
|
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class AbstractFacetParser |
25
|
|
|
* |
26
|
|
|
* @author Frans Saris <[email protected]> |
27
|
|
|
* @author Timo Hund <[email protected]> |
28
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets |
29
|
|
|
*/ |
30
|
|
|
abstract class AbstractFacetParser implements FacetParserInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var ContentObjectRenderer |
34
|
|
|
*/ |
35
|
|
|
protected static $reUseAbleContentObject; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ObjectManagerInterface |
39
|
|
|
*/ |
40
|
|
|
protected $objectManager; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ObjectManagerInterface $objectManager |
44
|
|
|
*/ |
45
|
56 |
|
public function injectObjectManager(ObjectManagerInterface $objectManager) |
46
|
|
|
{ |
47
|
56 |
|
$this->objectManager = $objectManager; |
48
|
56 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return ContentObjectRenderer |
52
|
|
|
*/ |
53
|
22 |
|
protected function getReUseAbleContentObject() |
54
|
|
|
{ |
55
|
|
|
/** @var $contentObject ContentObjectRenderer */ |
56
|
22 |
|
if (self::$reUseAbleContentObject !== null) { |
57
|
21 |
|
return self::$reUseAbleContentObject; |
58
|
|
|
} |
59
|
|
|
|
60
|
22 |
|
self::$reUseAbleContentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class); |
61
|
22 |
|
return self::$reUseAbleContentObject; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $configuration |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
56 |
|
protected function getPlainLabelOrApplyCObject($configuration) |
69
|
|
|
{ |
70
|
|
|
// when no label is configured we return an empty string |
71
|
56 |
|
if (!isset($configuration['label'])) { |
72
|
|
|
return ''; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// when no sub configuration is set, we use the string, configured as label |
76
|
56 |
|
if (!isset($configuration['label.'])) { |
77
|
55 |
|
return $configuration['label']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// when label and label. was set, we apply the cObject |
81
|
1 |
|
return $this->getReUseAbleContentObject()->cObjGetSingle($configuration['label'], $configuration['label.']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param mixed $value |
86
|
|
|
* @param integer $count |
87
|
|
|
* @param string $facetName |
88
|
|
|
* @param array $facetConfiguration |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
49 |
|
protected function getLabelFromRenderingInstructions($value, $count, $facetName, $facetConfiguration) |
92
|
|
|
{ |
93
|
49 |
|
$hasRenderingInstructions = isset($facetConfiguration['renderingInstruction']) && isset($facetConfiguration['renderingInstruction.']); |
94
|
49 |
|
if (!$hasRenderingInstructions) { |
95
|
48 |
|
return $value; |
96
|
|
|
} |
97
|
|
|
|
98
|
21 |
|
$this->getReUseAbleContentObject()->start(['optionValue' => $value, 'optionCount' => $count, 'facetName' => $facetName]); |
99
|
21 |
|
return $this->getReUseAbleContentObject()->cObjGetSingle( |
100
|
21 |
|
$facetConfiguration['renderingInstruction'], |
101
|
21 |
|
$facetConfiguration['renderingInstruction.'] |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Retrieves the active facetValue for a facet from the search request. |
108
|
|
|
* @param SearchResultSet $resultSet |
109
|
|
|
* @param string $facetName |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
45 |
|
protected function getActiveFacetValuesFromRequest(SearchResultSet $resultSet, $facetName) |
113
|
|
|
{ |
114
|
45 |
|
$activeFacetValues = $resultSet->getUsedSearchRequest()->getActiveFacetValuesByName($facetName); |
115
|
45 |
|
$activeFacetValues = is_array($activeFacetValues) ? $activeFacetValues : []; |
116
|
|
|
|
117
|
45 |
|
return $activeFacetValues; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $facetValuesFromSolrResponse |
123
|
|
|
* @param array $facetValuesFromSearchRequest |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
41 |
|
protected function getMergedFacetValueFromSearchRequestAndSolrResponse($facetValuesFromSolrResponse, $facetValuesFromSearchRequest) |
127
|
|
|
{ |
128
|
41 |
|
$facetValueItemsToCreate = $facetValuesFromSolrResponse; |
129
|
|
|
|
130
|
41 |
|
foreach ($facetValuesFromSearchRequest as $valueFromRequest) { |
131
|
|
|
// if we have options in the request that have not been in the response we add them with a count of 0 |
132
|
8 |
|
if (!isset($facetValueItemsToCreate[$valueFromRequest])) { |
133
|
8 |
|
$facetValueItemsToCreate[$valueFromRequest] = 0; |
134
|
|
|
} |
135
|
|
|
} |
136
|
41 |
|
return $facetValueItemsToCreate; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param AbstractOptionsFacet $facet |
141
|
|
|
* @param array $facetConfiguration |
142
|
|
|
* @return AbstractOptionsFacet |
143
|
|
|
*/ |
144
|
46 |
|
protected function applyManualSortOrder(AbstractOptionsFacet $facet, array $facetConfiguration) |
145
|
|
|
{ |
146
|
46 |
|
if (!isset($facetConfiguration['manualSortOrder'])) { |
147
|
44 |
|
return $facet; |
148
|
|
|
} |
149
|
3 |
|
$fields = GeneralUtility::trimExplode(',', $facetConfiguration['manualSortOrder']); |
150
|
3 |
|
$sortedOptions = $facet->getOptions()->getManualSortedCopy($fields); |
151
|
3 |
|
$facet->setOptions($sortedOptions); |
|
|
|
|
152
|
|
|
|
153
|
3 |
|
return $facet; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param AbstractOptionsFacet $facet |
158
|
|
|
* @param array $facetConfiguration |
159
|
|
|
* @return AbstractOptionsFacet |
160
|
|
|
*/ |
161
|
46 |
|
protected function applyReverseOrder(AbstractOptionsFacet $facet, array $facetConfiguration) |
162
|
|
|
{ |
163
|
46 |
|
if (empty($facetConfiguration['reverseOrder'])) { |
164
|
44 |
|
return $facet; |
165
|
|
|
} |
166
|
|
|
|
167
|
2 |
|
$facet->setOptions($facet->getOptions()->getReversedOrderCopy()); |
|
|
|
|
168
|
|
|
|
169
|
2 |
|
return $facet; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param mixed $value |
174
|
|
|
* @param array $facetConfiguration |
175
|
|
|
* @return boolean |
176
|
|
|
*/ |
177
|
|
|
protected function getIsExcludedFacetValue($value, array $facetConfiguration) |
178
|
|
|
{ |
179
|
|
|
if (!isset($facetConfiguration['excludeValues'])) { |
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$excludedValue = GeneralUtility::trimExplode(',', $facetConfiguration['excludeValues']); |
184
|
|
|
return in_array($value, $excludedValue); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.