Completed
Push — master ( 0b6d3a...3ff1a5 )
by Colin
31:14 queued 11s
created

SlugNormalizer::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
 * @psalm-immutable
20
 */
21
final class SlugNormalizer implements TextNormalizerInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     *
26
     * @psalm-pure
27
     */
28
    public function normalize(string $text, $context = null): string
29
    {
30
        // Trim whitespace
31
        $slug = \trim($text);
32
        // Convert to lowercase
33
        $slug = \mb_strtolower($slug);
34
        // Try replacing whitespace with a dash
35
        $slug = \preg_replace('/\s+/u', '-', $slug) ?? $slug;
36
        // Try removing characters other than letters, numbers, and marks.
37
        $slug = \preg_replace('/[^\p{L}\p{Nd}\p{Nl}\p{M}-]+/u', '', $slug) ?? $slug;
38
39
        return $slug;
40
    }
41
}
42