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

UriMatcher::matchUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
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