Passed
Push — master ( f9805a...14c2de )
by Paweł
03:11
created

UriMatcher   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 21
ccs 6
cts 6
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A matchUri() 0 6 1
A normalizeUri() 0 3 1
1
<?php
2
/**
3
 * Copyright (c) 2020.
4
 * @author Paweł Antosiak <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace Gorynych\Http\Routing;
10
11
final class UriMatcher
12
{
13
    /**
14
     * Returns TRUE if provided paths combination matches request URI
15
     *
16
     * @param string $uri '/clients/1/address'
17
     * @param string $resourcePath '/clients/<REGEX>'
18
     * @param string $operationPath '/address'
19
     * @param mixed[]|null $matches
20
     */
21 3
    public static function matchUri(string $uri, string $resourcePath, string $operationPath, &$matches = null): bool
22
    {
23 3
        $uri = self::normalizeUri($uri);
24 3
        $path = self::normalizeUri($resourcePath . $operationPath);
25
26 3
        return (bool) preg_match("#^{$path}$#", $uri, $matches);
27
    }
28
29 3
    private static function normalizeUri(string $uri): string
30
    {
31 3
        return rtrim(str_replace('//', '/', $uri), '/');
32
    }
33
}
34