Completed
Push — master ( 8801ad...e19554 )
by Alexpts
02:50
created

RouteService::makeRegExp()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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