forked from Probesys/bileto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocaleSorter.php
More file actions
51 lines (38 loc) · 1.18 KB
/
LocaleSorter.php
File metadata and controls
51 lines (38 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
// This file is part of Bileto.
// Copyright 2022-2025 Probesys
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace App\Service\Sorter;
use App\Service;
use Symfony\Component\HttpFoundation\RequestStack;
class LocaleSorter
{
private ?\Collator $collator = null;
public function __construct(
private RequestStack $requestStack,
private Service\Locales $locales,
) {
}
protected function getCollator(): \Collator
{
if (!$this->collator) {
$currentRequest = $this->requestStack->getCurrentRequest();
if ($currentRequest) {
$locale = $currentRequest->getLocale();
} else {
$locale = $this->locales->getDefaultLocale();
}
$this->collator = new \Collator($locale);
}
return $this->collator;
}
public function localeCompare(string $field1, string $field2): int
{
$collator = $this->getCollator();
$fieldsComparison = $collator->compare($field1, $field2);
if ($fieldsComparison === false) {
$fieldsComparison = strcmp($field1, $field2);
}
return $fieldsComparison;
}
}