Skip to content

Commit

Permalink
Fixed searchables for Postgres DB
Browse files Browse the repository at this point in the history
  • Loading branch information
jzaplet committed Jul 28, 2024
1 parent 6689626 commit ecd8be5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
29 changes: 23 additions & 6 deletions src/Collection/SearchBuilder/SearchBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Doctrine\ORM\QueryBuilder;
use Megio\Collection\CollectionRequest;
use Symfony\Component\Uid\UuidV6;

class SearchBuilder
{
Expand Down Expand Up @@ -33,7 +34,9 @@ public function build(): QueryBuilder
if ($searchText !== null) {
$whereDql = [];

foreach ($this->searchables as $searchable) {
$validSearchables = array_filter($this->searchables, fn(Searchable $searchable) => $searchable->isEnabled($searchText));

foreach ($validSearchables as $searchable) {
$relationCol = $searchable->getRelation();
$colName = 'entity.' . $searchable->getColumn();

Expand All @@ -44,10 +47,10 @@ public function build(): QueryBuilder
}

$paramName = 'param_' . str_replace('.', '_', $colName);
$value = $searchable->hasFormatter() ? $searchable->format($searchText) : "%{$searchText}%";
$value = $searchable->hasFormatter() ? $searchable->format($searchText) : $searchText;

$whereDql[] = [
'dql' => "{$colName} {$searchable->getOperator()} :{$paramName}",
'dql' => "$colName {$searchable->getOperator()} :{$paramName}",
'paramName' => $paramName,
'paramValue' => $value
];
Expand All @@ -68,9 +71,23 @@ public function build(): QueryBuilder

public function keepDefaults(): self
{
foreach (['id', 'createdAt', 'updatedAt'] as $columnName) {
$this->addSearchable(new Searchable($columnName));
}
$this->addSearchable(new Searchable(
column: 'id',
operator: '=',
enabled: fn($value) => UuidV6::isValid($value)
));

$this->addSearchable(new Searchable(
column: 'createdAt',
operator: '=',
enabled: fn($value) => \DateTime::createFromFormat('Y-m-d H:i:s', $value) !== false
));

$this->addSearchable(new Searchable(
column: 'updatedAt',
operator: '=',
enabled: fn($value) => \DateTime::createFromFormat('Y-m-d H:i:s', $value) !== false
));

return $this;
}
Expand Down
21 changes: 19 additions & 2 deletions src/Collection/SearchBuilder/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ class Searchable
/** @var array<int, callable> */
private array $formatter = [];

/** @var array<int, callable> */
private array $enabled = [];

public function __construct(
protected string $column,
protected ?string $relation = null,
protected string $operator = 'LIKE',
?callable $formatter = null
protected string $operator = '=',
?callable $formatter = null,
?callable $enabled = null,
)
{
if ($formatter !== null) {
$this->formatter[] = $formatter;
}

if ($enabled !== null) {
$this->enabled[] = $enabled;
}
}

public function getColumn(): string
Expand All @@ -40,6 +48,15 @@ public function format(?string $value): mixed
return $this->formatter[0]($value);
}

public function isEnabled(?string $value): bool
{
if (count($this->enabled) === 0) {
return true;
}

return $this->enabled[0]($value);
}

public function hasFormatter(): bool
{
return count($this->formatter) !== 0;
Expand Down
8 changes: 6 additions & 2 deletions src/Recipe/AdminRecipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public function search(SearchBuilder $builder, CollectionRequest $request): Sear
{
$builder
->keepDefaults()
->addSearchable(new Searchable('email'))
->addSearchable(new Searchable('lastLogin'));
->addSearchable(new Searchable('email', operator: 'LIKE', formatter: fn($value) => "%{$value}%"))
->addSearchable(new Searchable(
column: 'lastLogin',
operator: '=',
enabled: fn($value) => \DateTime::createFromFormat('Y-m-d H:i:s', $value) !== false
));

return $builder;
}
Expand Down

0 comments on commit ecd8be5

Please sign in to comment.