PatternReplacer::replace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Furious\Router\Pattern;
6
7
final class PatternReplacer
8
{
9
    private const REGEXP = "#\{([^\}]+)\}#";
10
11
    public function replace(string $pattern, array $tokens): string
12
    {
13
        return preg_replace_callback(self::REGEXP, function ($matches) use ($tokens) {
14
            $argument = $matches[1];
15
            $replace = $tokens[$argument] ?? '[^}]+';
16
            return '(?P<' . $argument . '>' . $replace . ')';
17
        }, $pattern);
18
    }
19
}