Passed
Push — master ( cc3f84...4a930e )
by Timo
23:43
created

Sortings   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 76.19%

Importance

Changes 0
Metric Value
wmc 7
eloc 14
dl 0
loc 65
ccs 16
cts 21
cp 0.7619
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setSortings() 0 3 1
A getSortings() 0 3 1
A fromString() 0 10 2
A getEmpty() 0 3 1
A addSorting() 0 3 1
A __construct() 0 4 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 <[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 3 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 TYPO3\CMS\Core\Utility\GeneralUtility;
28
29
/**
30
 * The Sorting ParameterProvider is responsible to build the solr query parameters
31
 * that are needed for the sorting.
32
 *
33
 * @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder
34
 */
35
class Sortings extends AbstractDeactivatable
36
{
37
    /**
38
     * @var array
39
     */
40
    protected $sortings = [];
41
42
    /**
43
     * Sortings constructor.
44
     * @param bool $isEnabled
45
     * @param array $sortings
46
     */
47 6
    public function __construct($isEnabled = false, $sortings = [])
48
    {
49 6
        $this->isEnabled = $isEnabled;
50 6
        $this->setSortings($sortings);
51 6
    }
52
53
    /**
54
     * @return Sortings
55
     */
56
    public static function getEmpty()
57
    {
58
        return new Sortings(false);
59
    }
60
61
    /**
62
     * @return Sorting[]
63
     */
64 6
    public function getSortings(): array
65
    {
66 6
        return $this->sortings;
67
    }
68
69
    /**
70
     * @param array $sortings
71
     */
72 6
    public function setSortings(array $sortings)
73
    {
74 6
        $this->sortings = $sortings;
75 6
    }
76
77
    /**
78
     * @param Sorting $sorting
79
     */
80
    public function addSorting(Sorting $sorting)
81
    {
82
        $this->sortings[] = $sorting;
83
    }
84
85
    /**
86
     * Parses a sortings representation "<fieldName> <direction>,<fieldName> <direction>"
87
     * @param string $sortingsString
88
     * @return Sortings
89
     */
90 6
    public static function fromString($sortingsString)
91
    {
92 6
        $sortFields = GeneralUtility::trimExplode(',',$sortingsString);
93 6
        $sortings = [];
94 6
        foreach($sortFields as $sortField) {
95 6
            $sorting = Sorting::fromString($sortField);
96 6
            $sortings[] = $sorting;
97
        }
98
99 6
        return new Sortings(true, $sortings);
100
    }
101
}