Passed
Push — master ( 3e6704...db24af )
by Rafael
43:26 queued 10s
created

ParameterSortingUtility::sortByIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Utility;
4
5
/**
6
 * Copyright notice
7
 *
8
 * (c) 2020 Lars Tode <[email protected]>
9
 * All rights reserved
10
 *
11
 * This script is part of the TYPO3 project. The TYPO3 project is
12
 * free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 3 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * The GNU General Public License can be found at
18
 * http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 * This script is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * This copyright notice MUST APPEAR in all copies of the script!
26
 */
27
28
/**
29
 * Utility class to sort parameters
30
 *
31
 * This class is used in places building URI for links, facets etc.
32
 *
33
 * @see \ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\UrlFacetContainer::getActiveFacets
34
 * @see \ApacheSolrForTypo3\Solr\Domain\Search\Uri\SearchUriBuilder::getAddFacetValueUri
35
 */
36
class ParameterSortingUtility
37
{
38
    /**
39
     * Sort a list of parameters either by their key or value
40
     *
41
     * @param array $parameters
42
     * @param string $type
43
     * @return array
44
     */
45
    public static function sortByType(array &$parameters, string $type = 'index'): array
46
    {
47
        switch ($type) {
48
            case 'assoc':
49
                return self::sortByIndex($parameters);
50
            case 'index':
51
            default:
52
                return self::sortByValue($parameters);
53
        }
54
    }
55
56
    /**
57
     * Sort a list of parameters by their values
58
     *
59
     * @param array $parameters
60
     * @return array
61
     */
62
    public static function sortByValue(array &$parameters)
63
    {
64
        usort(
65
            $parameters,
66
            [self::class, 'sort']
67
        );
68
        return $parameters;
69
    }
70
71
    /**
72
     * Sort a list of parameters by their keys
73
     *
74
     * @param array $parameters
75
     * @return array
76
     */
77
    public static function sortByIndex(array &$parameters)
78
    {
79
        uksort(
80
            $parameters,
81
            [self::class, 'sort']
82
        );
83
84
        return $parameters;
85
    }
86
87
    /**
88
     * Since the sort operation did not differs between keys and values it is placed inside an own method
89
     *
90
     * @param string $a
91
     * @param string $b
92
     * @return bool
93
     */
94
    public static function sort(string $a, string $b): bool
95
    {
96
        return strcmp($a, $b) > 0;
97
    }
98
}
99