Passed
Push — master ( 203e1d...831cab )
by Timo
27:25
created

HighlightResultViewHelper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 6
dl 0
loc 42
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 7 1
A renderStatic() 0 15 2
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 TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
21
22
/**
23
 * Class HighlightResultViewHelper
24
 *
25
 * @author Frans Saris <[email protected]>
26
 * @author Timo Hund <[email protected]>
27
 * @package ApacheSolrForTypo3\Solr\ViewHelpers\Document
28
 */
29
class HighlightResultViewHelper extends AbstractSolrFrontendViewHelper
30
{
31
    use CompileWithRenderStatic;
32
33
    /**
34
     * @var bool
35
     */
36
    protected $escapeOutput = false;
37
38
    /**
39
     * Initializes the arguments
40
     */
41 1
    public function initializeArguments()
42
    {
43 1
        parent::initializeArguments();
44 1
        $this->registerArgument('resultSet', SearchResultSet::class, 'The context searchResultSet', true);
45 1
        $this->registerArgument('document', \Apache_Solr_Document::class, 'The document to highlight', true);
46 1
        $this->registerArgument('fieldName', 'string', 'The fieldName', true);
47 1
    }
48
49
    /**
50
     * @param array $arguments
51
     * @param \Closure $renderChildrenClosure
52
     * @param RenderingContextInterface $renderingContext
53
     * @return string
54
     */
55 21
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
56
    {
57
        /** @var $resultSet SearchResultSet */
58 21
        $resultSet = $arguments['resultSet'];
59 21
        $fieldName = $arguments['fieldName'];
60 21
        $document = $arguments['document'];
61
62 21
        $fragmentSeparator = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchResultsHighlightingFragmentSeparator();
63 21
        $content = call_user_func([$document, 'get' . $fieldName]);
64 21
        $highlightedContent = $resultSet->getUsedSearch()->getHighlightedContent();
65 21
        if (!empty($highlightedContent->{$document->getId()}->{$fieldName}[0])) {
66 18
            $content = implode(' ' . $fragmentSeparator . ' ', $highlightedContent->{$document->getId()}->{$fieldName});
67
        }
68 21
        return $content;
69
    }
70
}
71