Skip to content

Commit

Permalink
allow use regexp at resolver names
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasPilar committed Aug 28, 2019
1 parent db678b4 commit 56cf2f5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
9 changes: 9 additions & 0 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ final class ItemFilterResolver extends AbstractFilterResolver
'price' => function (Range $range) {
return $this->priceResolver($range);
},
'brand(?:\[(.+)\])?\.id' => function (string $value, array $matches) { // resolver will match key "brand.id" or e.g. "brand[xyz].id" (value "xyz" will be passed via $matches property)
return $this->brandResolver($value);
},
];
}

Expand Down Expand Up @@ -89,6 +92,12 @@ final class ItemFilterResolver extends AbstractFilterResolver

return $condition;
}

private function brandResolver(string $value, array $matches): string
{
$subKey = $matches[1][0] ?? 'default';
return "brand_${subKey} = '${value}'";
}
}
```

Expand Down
17 changes: 12 additions & 5 deletions src/Generator/SQL/Resolver/AbstractFilterResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ abstract class AbstractFilterResolver
{

/**
* @param mixed $value
* @param array<int, mixed> $values
*/
public function resolve(string $column, $value): string
public function resolve(string $column, ...$values): string
{
$mapping = $this->getResolvers();
if (! isset($mapping[$column])) {
throw new InvalidArgumentException('Missing filter for "' . $column . '"');
foreach ($mapping as $pattern => $resolver) {
$matches = [];
if ((bool) preg_match_all("#^${pattern}$#", $column, $matches) === false) {
continue;
}

array_push($values, $matches);

return call_user_func_array($resolver, $values);
}

return call_user_func($mapping[$column], $value);
throw new InvalidArgumentException($column);
}


Expand Down

0 comments on commit 56cf2f5

Please sign in to comment.