Completed
Push — master ( f2d6e9...b6358f )
by Rafael
34:00
created

LabelPrefixesViewHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 8 1
A renderStatic() 0 17 3
A applySortBy() 0 13 4
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Facet\Options\Group\Prefix;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 Timo Hund <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\OptionCollection;
28
use ApacheSolrForTypo3\Solr\ViewHelpers\AbstractSolrFrontendViewHelper;
29
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
30
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
31
32
/**
33
 * Class LabelPrefixesViewHelper
34
 *
35
 * @author Timo Hund <[email protected]>
36
 * @package ApacheSolrForTypo3\Solr\ViewHelpers\Facet\Options
37
 */
38
class LabelPrefixesViewHelper extends AbstractSolrFrontendViewHelper
39
{
40
    use CompileWithRenderStatic;
41
42
    /**
43
     * @var bool
44
     */
45
    protected $escapeOutput = false;
46
47
    /**
48
     * Initializes the arguments
49
     */
50
    public function initializeArguments()
51
    {
52
        parent::initializeArguments();
53
        $this->registerArgument('options', OptionCollection::class, 'The options where prefixed should be available', true);
54
        $this->registerArgument('length', 'int', 'The length of the prefixed that should be retrieved', false, 1);
55
        $this->registerArgument('sortBy', 'string', 'The sorting mode (count,alpha)', false, 'count');
56
57
    }
58
59
    /**
60
     * @param array $arguments
61
     * @param \Closure $renderChildrenClosure
62
     * @param RenderingContextInterface $renderingContext
63
     * @return string
64
     */
65
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
66
    {
67
        /** @var  $options OptionCollection */
68
        $options = $arguments['options'];
69
        $length = isset($arguments['length']) ? $arguments['length'] : 1;
70
        $sortBy = isset($arguments['sortBy']) ? $arguments['sortBy'] : 'count';
71
        $prefixes = $options->getLowercaseLabelPrefixes($length);
72
73
        $prefixes = static::applySortBy($prefixes, $sortBy);
74
75
        $templateVariableProvider = $renderingContext->getVariableProvider();
76
        $templateVariableProvider->add('prefixes', $prefixes);
77
        $content = $renderChildrenClosure();
78
        $templateVariableProvider->remove('prefixes');
79
80
        return $content;
81
    }
82
83
    /**
84
     * Applies the configured sortBy.
85
     *
86
     * @param array $prefixes
87
     * @param string $sortBy
88
     * @return array
89
     */
90
    protected static function applySortBy(array $prefixes, $sortBy): array
91
    {
92
        if($sortBy === 'count' || $sortBy === '')
93
        {
94
            return $prefixes;
95
        }
96
97
        if($sortBy === 'alpha')
98
        {
99
            sort($prefixes);
100
            return $prefixes;
101
        }
102
    }
103
}
104