Completed
Push — master ( c8a26e...c11ae6 )
by Timo
10s
created

Wildcard::matches()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace hamburgscleanest\GuzzleAdvancedThrottle;
4
5
/**
6
 * Class Wildcard
7
 * @package hamburgscleanest\GuzzleAdvancedThrottle
8
 */
9
class Wildcard
10
{
11
12
    private const REGEX_WILDCARD  = '/{[^}]*}/';
13
    private const REGEX_ANY_CHAR  = '(.*)';
14
    private const REGEX_DELIMITER = '/';
15
16
    /**
17
     * @param string $wildcardText
18
     * @param string $text
19
     * @return bool
20
     */
21 6
    public static function matches(string $wildcardText, string $text) : bool
22
    {
23 6
        $regex = \preg_replace(self::REGEX_WILDCARD, self::REGEX_ANY_CHAR, $wildcardText);
24
25 6
        $matches = [];
26 6
        \preg_match_all(
27 6
            self::REGEX_DELIMITER . \str_replace(self::REGEX_DELIMITER, '\\' . self::REGEX_DELIMITER, $regex) . self::REGEX_DELIMITER,
28 6
            $text,
29 6
            $matches
30
        );
31
32 6
        \array_shift($matches);
33
34 6
        return \strtr($text, \array_fill_keys(\array_flatten($matches), self::REGEX_ANY_CHAR)) === $regex;
35
    }
36
}