Passed
Push — task/2976_TYPO3.11_compatibili... ( d7dfd0...4be1cc )
by Rafael
03:40
created

HighlightResultViewHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
wmc 7
eloc 31
dl 0
loc 79
ccs 32
cts 33
cp 0.9697
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renderStatic() 0 8 1
A getHighlightedContent() 0 11 2
A escapeEverythingExceptAllowedTags() 0 19 3
A initializeArguments() 0 6 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Document;
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\Result\SearchResult;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
19
use ApacheSolrForTypo3\Solr\ViewHelpers\AbstractSolrFrontendViewHelper;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23
24
/**
25
 * Class HighlightResultViewHelper
26
 *
27
 * @author Frans Saris <[email protected]>
28
 * @author Timo Hund <[email protected]>
29
 */
30
class HighlightResultViewHelper extends AbstractSolrFrontendViewHelper
31
{
32
    use CompileWithRenderStatic;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $escapeOutput = false;
38
39
    /**
40
     * Initializes the arguments
41
     */
42 22
    public function initializeArguments()
43
    {
44 22
        parent::initializeArguments();
45 22
        $this->registerArgument('resultSet', SearchResultSet::class, 'The context searchResultSet', true);
46 22
        $this->registerArgument('document', SearchResult::class, 'The document to highlight', true);
47 22
        $this->registerArgument('fieldName', 'string', 'The fieldName', true);
48 22
    }
49
50
    /**
51
     * @param array $arguments
52
     * @param \Closure $renderChildrenClosure
53
     * @param RenderingContextInterface $renderingContext
54
     * @return string
55
     */
56 25
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
57
    {
58
        /** @var $resultSet SearchResultSet */
59 25
        $resultSet = $arguments['resultSet'];
60 25
        $fieldName = $arguments['fieldName'];
61 25
        $document = $arguments['document'];
62 25
        $content = self::getHighlightedContent($resultSet, $document, $fieldName);
63 25
        return self::escapeEverythingExceptAllowedTags($resultSet, $content);
64
    }
65
66
    /**
67
     * @param SearchResultSet $resultSet
68
     * @param $document
69
     * @param $fieldName
70
     * @return mixed|string
71
     */
72 25
    protected static function getHighlightedContent(SearchResultSet $resultSet, $document, $fieldName)
73
    {
74 25
        $fragmentSeparator = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchResultsHighlightingFragmentSeparator();
75
76 25
        $content = call_user_func([$document, 'get' . $fieldName]);
77 25
        $highlightedContent = $resultSet->getUsedSearch()->getHighlightedContent();
78 25
        if (!empty($highlightedContent->{$document->getId()}->{$fieldName}[0])) {
79 3
            $content = implode(' ' . $fragmentSeparator . ' ', $highlightedContent->{$document->getId()}->{$fieldName});
80 3
            return $content;
81
        }
82 22
        return $content;
83
    }
84
85
    /**
86
     * @param SearchResultSet $resultSet
87
     * @param $content
88
     * @return string
89
     */
90 25
    protected static function escapeEverythingExceptAllowedTags(SearchResultSet $resultSet, $content)
91
    {
92 25
        $wrap = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchResultsHighlightingWrap();
93 25
        if ($wrap === '') {
94
            return htmlspecialchars($content);
95
        }
96
97 25
        $wrapParts = GeneralUtility::trimExplode("|", $wrap);
98 25
        if (count($wrapParts) !== 2) {
99 1
            return htmlspecialchars($content);
100
        }
101
102 24
        $substitutedContent = str_replace($wrapParts[0], '___highlight_begin___', $content);
103 24
        $substitutedContent = str_replace($wrapParts[1], '___highlight_end___', $substitutedContent);
104 24
        $output = htmlspecialchars($substitutedContent);
105 24
        $output = str_replace('___highlight_begin___', $wrapParts[0], $output);
106 24
        $output = str_replace('___highlight_end___', $wrapParts[1], $output);
107
108 24
        return $output;
109
    }
110
}
111