Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
26 / 26 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
Sorter | |
100.00% |
26 / 26 |
|
100.00% |
6 / 6 |
11 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
compare | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
sort | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
asort | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
natsort | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
collatorNatsort | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Class Sorter |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Moravian Library 2022. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package I18n |
25 | * @author Josef Moravec <moravec@mzk.cz> |
26 | * @license https://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development Wiki |
28 | */ |
29 | |
30 | declare(strict_types=1); |
31 | |
32 | namespace VuFind\I18n; |
33 | |
34 | /** |
35 | * Class Sorter |
36 | * |
37 | * @category VuFind |
38 | * @package I18n |
39 | * @author Josef Moravec <moravec@mzk.cz> |
40 | * @license https://opensource.org/licenses/gpl-2.0.php GNU General Public License |
41 | * @link https://vufind.org/wiki/development Wiki |
42 | */ |
43 | class Sorter implements SorterInterface |
44 | { |
45 | /** |
46 | * Intl Collator |
47 | * |
48 | * @var \Collator |
49 | */ |
50 | protected $collator; |
51 | |
52 | /** |
53 | * Do respect current locale? |
54 | * |
55 | * @var bool |
56 | */ |
57 | protected $respectLocale; |
58 | |
59 | /** |
60 | * Constructor |
61 | * |
62 | * @param \Collator $collator Current user locale |
63 | * @param bool $respectLocale Do respect current locale? |
64 | */ |
65 | public function __construct(\Collator $collator, bool $respectLocale = false) |
66 | { |
67 | $this->collator = $collator; |
68 | $this->respectLocale = $respectLocale; |
69 | } |
70 | |
71 | /** |
72 | * Compare function |
73 | * |
74 | * @param string $string1 First string to compare |
75 | * @param string $string2 Second string to compare |
76 | * |
77 | * @return int |
78 | */ |
79 | public function compare(string $string1, string $string2): int |
80 | { |
81 | return $this->respectLocale |
82 | ? $this->collator->compare($string1, $string2) |
83 | : strcasecmp($string1, $string2); |
84 | } |
85 | |
86 | /** |
87 | * Sort array by values |
88 | * |
89 | * @param array $array Array to sort |
90 | * |
91 | * @return bool |
92 | */ |
93 | public function sort(array &$array): bool |
94 | { |
95 | return $this->respectLocale |
96 | ? $this->collator->sort($array) |
97 | : sort($array); |
98 | } |
99 | |
100 | /** |
101 | * Sort array by values and maintain index association |
102 | * |
103 | * @param array $array Array to sort |
104 | * |
105 | * @return bool |
106 | */ |
107 | public function asort(array &$array): bool |
108 | { |
109 | return $this->respectLocale |
110 | ? $this->collator->asort($array) |
111 | : asort($array); |
112 | } |
113 | |
114 | /** |
115 | * Natural sort by values and maintain index association |
116 | * |
117 | * @param array $array Array to sort |
118 | * |
119 | * @return bool |
120 | */ |
121 | public function natsort(array &$array): bool |
122 | { |
123 | return $this->respectLocale |
124 | ? $this->collatorNatsort($array) |
125 | : natcasesort($array); |
126 | } |
127 | |
128 | /** |
129 | * Function to actually do natural sorting |
130 | * |
131 | * @param array $array Array to sort |
132 | * |
133 | * @return bool |
134 | */ |
135 | protected function collatorNatsort(array &$array): bool |
136 | { |
137 | $data = $array; |
138 | $data = preg_replace_callback( |
139 | '~([0-9]+)~', |
140 | function ($matches) { |
141 | return sprintf('%032d', $matches[0]); |
142 | }, |
143 | $data |
144 | ); |
145 | $success = $this->collator->asort($data); |
146 | foreach ($data as $key => $item) { |
147 | $data[$key] = $array[$key]; |
148 | } |
149 | $array = $data; |
150 | return $success; |
151 | } |
152 | } |