Skip to content

Commit

Permalink
Added addSearchablesToSchema() method in SearchBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
jzaplet committed May 3, 2024
1 parent c01a128 commit be07f72
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
31 changes: 24 additions & 7 deletions src/Collection/SearchBuilder/SearchBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class SearchBuilder
/** @var array<string, Searchable> */
protected array $searchables = [];

/** @var string[] */
protected array $extraSearchables = [];

public function create(QueryBuilder $qb, CollectionRequest $request): self
{
$this->queryBuilder = $qb;
Expand Down Expand Up @@ -52,7 +55,7 @@ public function build(): QueryBuilder

if (count($whereDql) !== 0) {
$where = implode(' OR ', array_map(fn($where) => $where['dql'], $whereDql));
$this->queryBuilder->andWhere($where);
$this->queryBuilder->orWhere($where);

foreach ($whereDql as $where) {
$this->queryBuilder->setParameter($where['paramName'], $where['paramValue']);
Expand All @@ -63,12 +66,9 @@ public function build(): QueryBuilder
return $this->queryBuilder;
}

/**
* @param string[] $searchables
*/
public function keepDefaults(array $searchables = ['id', 'createdAt', 'updatedAt']): self
public function keepDefaults(): self
{
foreach ($searchables as $columnName) {
foreach (['id', 'createdAt', 'updatedAt'] as $columnName) {
$this->addSearchable(new Searchable($columnName));
}

Expand All @@ -81,6 +81,16 @@ public function addSearchable(Searchable $searchable): self
return $this;
}

/**
* @param string[] $columnNames
*/
public function addSearchablesToSchema(array $columnNames): self
{
// Add only unique values
$this->extraSearchables = $columnNames;
return $this;
}

public function getQueryBuilder(): QueryBuilder
{
return $this->queryBuilder;
Expand All @@ -92,13 +102,20 @@ public function getRequest(): CollectionRequest
}

/** @return array{
* searchables: array{column: string, relation: string|null, operator: string}[]
* searchables: array{column: string, relation: string|null}[]
* }
*/
public function toArray(): array
{
$searchables = array_map(fn(Searchable $searchable) => $searchable->toArray(), $this->searchables);

foreach ($this->extraSearchables as $columnName) {
$searchables[] = [
'column' => $columnName,
'relation' => null
];
}

return [
'searchables' => array_values($searchables),
];
Expand Down
5 changes: 2 additions & 3 deletions src/Collection/SearchBuilder/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ public function hasFormatter(): bool
}

/**
* @return array{column: string, relation: string|null, operator: string}
* @return array{column: string, relation: string|null}
*/
public function toArray(): array
{
return [
'column' => $this->column,
'relation' => $this->relation,
'operator' => $this->operator,
'relation' => $this->relation
];
}
}

0 comments on commit be07f72

Please sign in to comment.