Completed
Push — 1.5 ( 09e907...fb52dd )
by Colin
01:21
created

TextNormalizer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 31
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 15 2
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace League\CommonMark\Normalizer;
15
16
/***
17
 * Normalize text input using the steps given by the CommonMark spec to normalize labels
18
 *
19
 * @see https://spec.commonmark.org/0.29/#matches
20
 */
21
final class TextNormalizer implements TextNormalizerInterface
22
{
23
    /**
24
     * @var array<int, array<int, string>>
25
     *
26
     * Source: https://github.com/symfony/polyfill-mbstring/blob/master/Mbstring.php
27
     */
28
    private const CASE_FOLD = [
29
        ['µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE", "\xC3\x9F", "\xE1\xBA\x9E"],
30
        ['μ', 's', 'ι',        'σ', 'β',        'θ',        'φ',        'π',        'κ',        'ρ',        'ε',        "\xE1\xB9\xA1", 'ι',            'ss',       'ss'],
31
    ];
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 477
    public function normalize(string $text, $context = null): string
37
    {
38
        // Collapse internal whitespace to single space and remove
39
        // leading/trailing whitespace
40 477
        $text = \preg_replace('/\s+/', ' ', \trim($text));
41
42 477
        if (!\defined('MB_CASE_FOLD')) {
43
            // We're not on a version of PHP (7.3+) which has this feature
44 318
            $text = \str_replace(self::CASE_FOLD[0], self::CASE_FOLD[1], $text);
45
46 318
            return \mb_strtolower($text, 'UTF-8');
47
        }
48
49 159
        return \mb_convert_case($text, \MB_CASE_FOLD, 'UTF-8');
50
    }
51
}
52