Passed
Push — master ( 4dee65...c8942a )
by Timo
30:11 queued 09:50
created

HighlightResultViewHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 96.97%

Importance

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