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