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

SlugNormalizer   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 13 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