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 TYPO3\CMS\Core\Utility\GeneralUtility; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Service class to check for a facet if allRequirements are met for that facet. |
21
|
|
|
* |
22
|
|
|
* @author Timo Hund <[email protected]> |
23
|
|
|
* @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facet |
24
|
|
|
*/ |
25
|
|
|
class RequirementsService |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Checks if facet meets all requirements. |
29
|
|
|
* |
30
|
|
|
* Evaluates configuration in "plugin.tx_solr.search.faceting.facets.[facetName].requirements", |
31
|
|
|
* |
32
|
|
|
* @param AbstractFacet $facet |
33
|
|
|
* @return bool true if facet might be rendered |
34
|
|
|
*/ |
35
|
56 |
|
public function getAllRequirementsMet(AbstractFacet $facet) |
36
|
|
|
{ |
37
|
56 |
|
$requirements = $facet->getRequirements(); |
38
|
56 |
|
if (!is_array($requirements) || count($requirements) === 0) { |
39
|
51 |
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
7 |
|
foreach ($requirements as $requirement) { |
43
|
7 |
|
$requirementMet = $this->getRequirementMet($facet, $requirement); |
44
|
6 |
|
$requirementMet = $this->getNegationWhenConfigured($requirementMet, $requirement); |
45
|
|
|
|
46
|
6 |
|
if ($requirementMet) { |
47
|
|
|
// early return |
48
|
6 |
|
return true; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Checks if a single requirement is met. |
57
|
|
|
* |
58
|
|
|
* @param AbstractFacet $facet |
59
|
|
|
* @param array $requirement |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
7 |
|
protected function getRequirementMet(AbstractFacet $facet, $requirement = []) { |
63
|
7 |
|
$selectedItemValues = $this->getSelectedItemValues($facet, $requirement['facet']); |
64
|
6 |
|
$csvActiveFacetItemValues = implode(', ', $selectedItemValues); |
65
|
6 |
|
$requirementValues = GeneralUtility::trimExplode(',', $requirement['values']); |
66
|
|
|
|
67
|
6 |
|
foreach ($requirementValues as $value) { |
68
|
6 |
|
$noFacetOptionSelectedRequirementMet = ($value === '__none' && empty($selectedItemValues)); |
69
|
6 |
|
$anyFacetOptionSelectedRequirementMet = ($value === '__any' && !empty($selectedItemValues)); |
70
|
|
|
|
71
|
6 |
|
if ($noFacetOptionSelectedRequirementMet || $anyFacetOptionSelectedRequirementMet || in_array($value, $selectedItemValues) || fnmatch($value, $csvActiveFacetItemValues)) { |
72
|
|
|
// when we find a single matching requirement we can exit and return true |
73
|
6 |
|
return true; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the active item values of a facet |
82
|
|
|
* |
83
|
|
|
* @param string $facetNameToCheckRequirementsOn |
84
|
|
|
* @return AbstractFacetItem[] |
85
|
|
|
*/ |
86
|
7 |
|
protected function getSelectedItemValues(AbstractFacet $facet, $facetNameToCheckRequirementsOn) |
87
|
|
|
{ |
88
|
7 |
|
$facetToCheckRequirements = $facet->getResultSet()->getFacets()->getByName($facetNameToCheckRequirementsOn)->getByPosition(0); |
89
|
7 |
|
if (!$facetToCheckRequirements instanceof AbstractFacet) { |
90
|
1 |
|
throw new \InvalidArgumentException('Requirement for unexisting facet configured'); |
91
|
|
|
} |
92
|
|
|
|
93
|
6 |
|
if (!$facetToCheckRequirements->getIsUsed()) { |
94
|
|
|
// unused facets do not have active values. |
95
|
2 |
|
return []; |
96
|
|
|
} |
97
|
|
|
|
98
|
4 |
|
$itemValues = []; |
99
|
4 |
|
$activeFacetItems = $facetToCheckRequirements->getAllFacetItems(); |
100
|
4 |
|
foreach ($activeFacetItems as $item) { |
101
|
|
|
/** @var AbstractFacetItem $item */ |
102
|
4 |
|
if ($item->getSelected()) { |
103
|
|
|
$itemValues[] = $item->getUriValue(); |
104
|
|
|
} |
105
|
4 |
|
} |
106
|
|
|
|
107
|
|
|
return $itemValues; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Negates the result when configured. |
112
|
|
|
* |
113
|
|
|
* @param boolean $value |
114
|
|
|
* @param array $configuration |
115
|
6 |
|
* @return boolean |
116
|
|
|
*/ |
117
|
6 |
|
protected function getNegationWhenConfigured($value, $configuration) |
118
|
5 |
|
{ |
119
|
|
|
if (!is_array($configuration) || empty($configuration['negate']) || (bool)$configuration['negate'] === false) { |
120
|
|
|
return $value; |
121
|
1 |
|
} |
122
|
|
|
|
123
|
|
|
return !((bool)$value); |
124
|
|
|
} |
125
|
|
|
} |