Passed
Push — main ( 083a11...ff30e2 )
by Greg
09:24
created

ModuleLanguageTrait::normalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\ExtCalendar\CalendarInterface;
23
use Fisharebest\ExtCalendar\GregorianCalendar;
24
use Fisharebest\Localization\Locale\LocaleEnUs;
25
use Fisharebest\Localization\Locale\LocaleInterface;
26
use Fisharebest\Webtrees\I18N;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Fisharebest\Webtrees\Relationship;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Relationship was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Normalizer;
29
30
use function mb_substr;
31
use function normalizer_normalize;
32
33
/**
34
 * Default implementation of ModuleLanguageInterface.
35
 */
36
trait ModuleLanguageTrait
37
{
38
    /**
39
     * @return array<int,string>
40
     */
41
    public function alphabet(): array
42
    {
43
        return [
44
            'A',
45
            'B',
46
            'C',
47
            'D',
48
            'E',
49
            'F',
50
            'G',
51
            'H',
52
            'I',
53
            'J',
54
            'K',
55
            'L',
56
            'M',
57
            'N',
58
            'O',
59
            'P',
60
            'Q',
61
            'R',
62
            'S',
63
            'T',
64
            'U',
65
            'V',
66
            'W',
67
            'X',
68
            'Y',
69
            'Z',
70
        ];
71
    }
72
73
    public function calendar(): CalendarInterface
74
    {
75
        return new GregorianCalendar();
76
    }
77
78
    public function dateOrder(): string
79
    {
80
        return 'DMY';
81
    }
82
83
    public function initialLetter(string $string): string
84
    {
85
        return mb_substr($string, 0, 1);
86
    }
87
88
    /**
89
     * Ignore diacritics on letters - unless the language considers them a different letter.
90
     */
91
    public function normalize(string $text): string
92
    {
93
        // Decompose any combined characters.
94
        $decomposed = normalizer_normalize($text, Normalizer::FORM_KD);
0 ignored issues
show
Bug introduced by
Normalizer::FORM_KD of type string is incompatible with the type integer expected by parameter $form of normalizer_normalize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
        $decomposed = normalizer_normalize($text, /** @scrutinizer ignore-type */ Normalizer::FORM_KD);
Loading history...
95
96
        if ($decomposed === false) {
97
            // Invalid UTF8?
98
            return $text;
99
        }
100
101
        // Keep any diacritic marks that are significant to this language.
102
        $text = strtr($decomposed, $this->normalizeExceptions());
103
104
        // Remove the others.
105
        return preg_replace('/\p{M}/u', '', $text);
106
    }
107
108
    /**
109
     * Letters with diacritics that are considered distinct letters in this language.
110
     *
111
     * @return array<string,string>
112
     */
113
    protected function normalizeExceptions(): array
114
    {
115
        return [];
116
    }
117
118
    public function title(): string
119
    {
120
        return  $this->locale()->endonym();
121
    }
122
123
    public function description(): string
124
    {
125
        return I18N::translate('Language') . ' — ' . $this->title() . ' — ' . $this->locale()->languageTag();
126
    }
127
128
    public function locale(): LocaleInterface
129
    {
130
        return new LocaleEnUs();
131
    }
132
133
    /**
134
     * @return array<Relationship>
135
     */
136
    public function relationships(): array
137
    {
138
        return [];
139
    }
140
}
141