1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Hierarchy; |
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\AbstractFacetParser; |
18
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class OptionsFacetParser |
22
|
|
|
*/ |
23
|
|
|
class HierarchyFacetParser extends AbstractFacetParser |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param SearchResultSet $resultSet |
27
|
|
|
* @param string $facetName |
28
|
|
|
* @param array $facetConfiguration |
29
|
|
|
* @return HierarchyFacet|null |
30
|
|
|
*/ |
31
|
29 |
|
public function parse(SearchResultSet $resultSet, $facetName, array $facetConfiguration) |
32
|
|
|
{ |
33
|
29 |
|
$response = $resultSet->getResponse(); |
34
|
29 |
|
$fieldName = $facetConfiguration['field']; |
35
|
29 |
|
$label = $this->getPlainLabelOrApplyCObject($facetConfiguration); |
36
|
29 |
|
$optionsFromSolrResponse = isset($response->facet_counts->facet_fields->{$fieldName}) ? get_object_vars($response->facet_counts->facet_fields->{$fieldName}) : []; |
37
|
29 |
|
$optionsFromRequest = $this->getActiveFacetValuesFromRequest($resultSet, $facetName); |
38
|
29 |
|
$hasOptionsInResponse = !empty($optionsFromSolrResponse); |
39
|
29 |
|
$hasSelectedOptionsInRequest = count($optionsFromRequest) > 0; |
40
|
29 |
|
$hasNoOptionsToShow = !$hasOptionsInResponse && !$hasSelectedOptionsInRequest; |
41
|
29 |
|
$hideEmpty = !$resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchFacetingShowEmptyFacetsByName($facetName); |
42
|
|
|
|
43
|
29 |
|
if ($hasNoOptionsToShow && $hideEmpty) { |
44
|
3 |
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** @var $facet HierarchyFacet */ |
48
|
26 |
|
$facet = $this->objectManager->get(HierarchyFacet::class, $resultSet, $facetName, $fieldName, $label, $facetConfiguration); |
|
|
|
|
49
|
|
|
|
50
|
26 |
|
$hasActiveOptions = count($optionsFromRequest) > 0; |
51
|
26 |
|
$facet->setIsUsed($hasActiveOptions); |
52
|
|
|
|
53
|
26 |
|
$facet->setIsAvailable($hasOptionsInResponse); |
54
|
|
|
|
55
|
26 |
|
$nodesToCreate = $this->getMergedFacetValueFromSearchRequestAndSolrResponse($optionsFromSolrResponse, $optionsFromRequest); |
56
|
|
|
|
57
|
26 |
|
foreach ($nodesToCreate as $value => $count) { |
58
|
26 |
|
if ($this->getIsExcludedFacetValue($value, $facetConfiguration)) { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
26 |
|
$isActive = in_array($value, $optionsFromRequest); |
62
|
26 |
|
$delimiterPosition = strpos($value, '-'); |
63
|
26 |
|
$path = substr($value, $delimiterPosition + 1); |
64
|
26 |
|
$pathArray = $this->getPathAsArray($path); |
65
|
26 |
|
$key = array_pop($pathArray); |
66
|
26 |
|
$parentKey = array_pop($pathArray); |
67
|
26 |
|
$value = '/' . $path; |
68
|
26 |
|
$label = $this->getLabelFromRenderingInstructions($key, $count, $facetName, $facetConfiguration); |
69
|
|
|
|
70
|
26 |
|
$facet->createNode($parentKey, $key, $label, $value, $count, $isActive); |
71
|
|
|
} |
72
|
|
|
|
73
|
26 |
|
return $facet; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* This method is used to get the path array from a hierarchical facet. It substitutes escaped slashes to keep them |
78
|
|
|
* when they are used inside a facetValue. |
79
|
|
|
* |
80
|
|
|
* @param string $path |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
26 |
|
protected function getPathAsArray($path) |
84
|
|
|
{ |
85
|
26 |
|
$path = str_replace('\/', '@@@', $path); |
86
|
26 |
|
$path = rtrim($path, "/"); |
87
|
26 |
|
$segments = explode('/', $path); |
88
|
26 |
|
return array_map(function($item) { |
89
|
26 |
|
return str_replace('@@@', '/', $item); |
90
|
26 |
|
}, $segments); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Retrieves the active facetValue for a facet from the search request. |
95
|
|
|
* @param SearchResultSet $resultSet |
96
|
|
|
* @param string $facetName |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
29 |
|
protected function getActiveFacetValuesFromRequest(SearchResultSet $resultSet, $facetName) |
100
|
|
|
{ |
101
|
29 |
|
$activeFacetValues = []; |
102
|
29 |
|
$values = $resultSet->getUsedSearchRequest()->getActiveFacetValuesByName($facetName); |
103
|
|
|
|
104
|
29 |
|
foreach (is_array($values) ? $values : [] as $valueFromRequest) { |
105
|
|
|
// Attach the 'depth' param again to the value |
106
|
1 |
|
if (strpos($valueFromRequest, '-') === false) { |
107
|
1 |
|
$valueFromRequest = trim($valueFromRequest, '/'); |
108
|
1 |
|
$valueFromRequest = (count(explode('/', $valueFromRequest)) - 1) . '-' . $valueFromRequest . '/'; |
109
|
|
|
} |
110
|
1 |
|
$activeFacetValues[] = $valueFromRequest; |
111
|
|
|
} |
112
|
29 |
|
return $activeFacetValues; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.