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

SlugNormalizer::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Normalizer;
15
16
/**
17
 * Creates URL-friendly strings based on the given string input
18
 */
19
final class SlugNormalizer implements TextNormalizerInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 177
    public function normalize(string $text, $context = null): string
25
    {
26
        // Trim whitespace
27 177
        $slug = \trim($text);
28
        // Convert to lowercase
29 177
        $slug = \mb_strtolower($slug);
30
        // Try replacing whitespace with a dash
31 177
        $slug = \preg_replace('/\s+/u', '-', $slug) ?? $slug;
32
        // Try removing characters other than letters, numbers, and marks.
33 177
        $slug = \preg_replace('/[^\p{L}\p{Nd}\p{Nl}\p{M}-]+/u', '', $slug) ?? $slug;
34
35 177
        return $slug;
36
    }
37
}
38