Completed
Push — unique-permalinks ( ca79cc )
by Colin
01:24
created

UniqueSlugNormalizer::normalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
c 0
b 0
f 0
rs 9.7998
cc 2
nc 2
nop 2
crap 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
final class UniqueSlugNormalizer implements TextNormalizerInterface
17
{
18
    /** @var SlugNormalizer */
19
    private $slugNormalizer;
20
21
    /** @var array<string, int> */
22
    private $usages;
23
24 81
    public function __construct()
25
    {
26 81
        $this->slugNormalizer = new SlugNormalizer();
27 81
    }
28
29 75
    public function normalize(string $text, $context = null): string
30
    {
31 75
        $slug = $this->slugNormalizer->normalize($text, $context);
32
33 75
        if (!isset($this->usages[$slug])) {
34 75
            $this->usages[$slug] = 1;
35
36 75
            return $slug;
37
        }
38
39 9
        $count = $this->usages[$slug]++;
40
41 9
        return $slug . '-' . $count;
42
    }
43
}
44