Skip to content

Commit 37996ea

Browse files
Update executor to also check the canApply directive
1 parent 45d782b commit 37996ea

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/SearchExecutor.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,32 @@ protected function applyDirective(PatternDirective $directive, string $query): s
8080
{
8181
$matchCount = preg_match_all($directive->pattern(), $query, $matches, PREG_SET_ORDER);
8282

83-
if (!$matchCount) {
83+
if (! $matchCount) {
8484
return $query;
8585
}
8686

87-
collect($matches)
88-
->filter(fn(array $match) => $directive->canApply(array_shift($match), $match))
89-
->each(function (array $match) use ($directive) {
90-
if ($directive instanceof GroupDirective) {
91-
if ($this->groupDirective) {
92-
return;
93-
} else {
94-
$this->groupDirective = $directive;
95-
}
87+
$matches = array_filter(
88+
$matches,
89+
fn(array $match) => $directive->canApply(array_shift($match), $match)
90+
);
91+
92+
if (empty($matches)) {
93+
return $query;
94+
}
95+
96+
foreach ($matches as $match) {
97+
if ($directive instanceof GroupDirective) {
98+
if ($this->groupDirective) {
99+
continue;
100+
} else {
101+
$this->groupDirective = $directive;
96102
}
103+
}
97104

98-
$directive->apply($this->builder, array_shift($match), $match);
105+
$directive->apply($this->builder, array_shift($match), $match);
99106

100-
$this->appliedDirectives[] = $directive;
101-
});
107+
$this->appliedDirectives[] = $directive;
108+
}
102109

103110
return preg_filter($directive->pattern(), '', $query);
104111
}

0 commit comments

Comments
 (0)