RouteService::makeRegExp()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 2
nop 1
crap 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace PTS\Routing;
5
6
class RouteService
7
{
8
9 7
    public function makeRegExp(Route $route): string
10
    {
11 7
        $regexp = $route->getPath();
12 7
        $restrictions = $route->getRestrictions();
13
14 7
        if (preg_match_all('~{(.*)}~Uu', $regexp, $placeholders)) {
15 6
            foreach ($placeholders[0] as $index => $match) {
16 6
                $name = $placeholders[1][$index];
17 6
                $replace = array_key_exists($name, $restrictions) ? $restrictions[$name] : '[^\/]+';
18 6
                $replace = '(?<'.$name.'>'.$replace.')';
19 6
                $regexp = str_replace($match, $replace, $regexp);
20
            }
21
        }
22
23 7
        return $regexp;
24
    }
25
}
26