Passed
Pull Request — task/3376-TYPO3_12_compatibili... (#3456)
by Rafael
43:06
created

AbstractFacetParser::getReUseAbleContentObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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