diff --git a/src/Collection/SearchBuilder/SearchBuilder.php b/src/Collection/SearchBuilder/SearchBuilder.php index f0f2691..f7aba6f 100644 --- a/src/Collection/SearchBuilder/SearchBuilder.php +++ b/src/Collection/SearchBuilder/SearchBuilder.php @@ -14,6 +14,9 @@ class SearchBuilder /** @var array */ protected array $searchables = []; + /** @var string[] */ + protected array $extraSearchables = []; + public function create(QueryBuilder $qb, CollectionRequest $request): self { $this->queryBuilder = $qb; @@ -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']); @@ -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)); } @@ -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; @@ -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), ]; diff --git a/src/Collection/SearchBuilder/Searchable.php b/src/Collection/SearchBuilder/Searchable.php index 4bd2d17..f1587ed 100644 --- a/src/Collection/SearchBuilder/Searchable.php +++ b/src/Collection/SearchBuilder/Searchable.php @@ -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 ]; } } \ No newline at end of file