-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleMatcher.php
51 lines (45 loc) · 1.21 KB
/
SimpleMatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace Kellegous\CodeOwners;
use Closure;
/**
* SimpleMatcher does a linear search of the rules present in the code owners
* file. This is a simpler matcher that uses less memory but is slower than an
* AutomatonMatcher..
*/
final class SimpleMatcher implements RuleMatcher
{
/**
* @var array{Closure(string):bool, Rule}[]
*/
private array $rules;
/**
* @param iterable<Rule> $rules
*/
public function __construct(iterable $rules)
{
$matchers = [];
foreach ($rules as $rule) {
$matchers[] = [$rule->getPattern()->getMatcher(), $rule];
}
$this->rules = array_reverse($matchers);
}
/**
* @inerhitDoc
* @param string $path
* @return Rule|null
*/
public function match(string $path): ?Rule
{
if (str_starts_with($path, '/') || str_ends_with($path, '/')) {
throw new \InvalidArgumentException(
"path should be a relative path to a file, thus it cannot start or end with a /"
);
}
foreach ($this->rules as [$matcher, $rule]) {
if ($matcher($path)) {
return $rule;
}
}
return null;
}
}