[COMPONENTS][Search] Slightly refactor parser, since the inline lambda was somewhat complex (and cs-fixer kept moving the comment, so doc-checker complained)

This commit is contained in:
Hugo Sales 2021-10-10 09:23:51 +01:00 committed by Diogo Peralta Cordeiro
parent 703e66fd2e
commit 8ef2d3339f
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0

View File

@ -27,6 +27,21 @@ use Doctrine\Common\Collections\Criteria;
abstract class Parser abstract class Parser
{ {
/**
* Merge $parts into $criteria_arr
*/
private static function connectParts(array &$parts, array &$criteria_arr, string $last_op, mixed $eb, bool $force = false): void
{
foreach ([' ' => 'orX', '|' => 'orX', '&' => 'andX'] as $op => $func) {
if ($last_op === $op || $force) {
$criteria_arr[] = $eb->{$func}(...$parts);
$note_parts = [];
break;
}
}
}
/** /**
* Parse $input string into a Doctrine query Criteria * Parse $input string into a Doctrine query Criteria
* *
@ -53,19 +68,6 @@ abstract class Parser
$actor_parts = []; $actor_parts = [];
$last_op = null; $last_op = null;
$connect_parts = /**
* Merge $parts into $criteria_arr
*/
function (array &$parts, array &$criteria_arr, bool $force = false) use ($eb, $last_op) {
foreach ([' ' => 'orX', '|' => 'orX', '&' => 'andX'] as $op => $func) {
if ($last_op === $op || $force) {
$criteria_arr[] = $eb->{$func}(...$parts);
$note_parts = [];
break;
}
}
};
for ($index = 0; $index < $lenght; ++$index) { for ($index = 0; $index < $lenght; ++$index) {
$end = false; $end = false;
$match = false; $match = false;
@ -91,9 +93,9 @@ abstract class Parser
$right = $left = $index + 1; $right = $left = $index + 1;
if (!is_null($last_op) && $last_op !== $delimiter) { if (!\is_null($last_op) && $last_op !== $delimiter) {
$connect_parts($note_parts, $note_criteria_arr, force: false); self::connectParts($note_parts, $note_criteria_arr, $last_op, $eb, force: false);
$connect_parts($actor_parts, $actor_criteria_arr, force: false); self::connectParts($actor_parts, $actor_criteria_arr, $last_op, $eb, force: false);
} else { } else {
$last_op = $delimiter; $last_op = $delimiter;
} }
@ -108,14 +110,12 @@ abstract class Parser
$note_criteria = $actor_criteria = null; $note_criteria = $actor_criteria = null;
if (!empty($note_parts)) { if (!empty($note_parts)) {
$connect_parts($note_parts, $note_criteria_arr, force: true); self::connectParts($note_parts, $note_criteria_arr, $last_op, $eb, force: true);
$note_criteria = new Criteria($eb->orX(...$note_criteria_arr)); $note_criteria = new Criteria($eb->orX(...$note_criteria_arr));
} else { } elseif (!empty($actor_parts)) {
if (!empty($actor_parts)) { self::connectParts($actor_parts, $actor_criteria_arr, $last_op, $eb, force: true);
$connect_parts($actor_parts, $actor_criteria_arr, force: true);
$actor_criteria = new Criteria($eb->orX(...$actor_criteria_arr)); $actor_criteria = new Criteria($eb->orX(...$actor_criteria_arr));
} }
}
return [$note_criteria, $actor_criteria]; return [$note_criteria, $actor_criteria];
} }