-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
268 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Megio\Collection\SearchBuilder; | ||
|
||
use Doctrine\ORM\QueryBuilder; | ||
use Megio\Collection\CollectionRequest; | ||
|
||
class SearchBuilder | ||
{ | ||
protected QueryBuilder $queryBuilder; | ||
protected CollectionRequest $request; | ||
|
||
/** @var array<string, Searchable> */ | ||
protected array $searchables = []; | ||
|
||
public function create(QueryBuilder $qb, CollectionRequest $request): self | ||
{ | ||
$this->queryBuilder = $qb; | ||
$this->request = $request; | ||
|
||
return $this; | ||
} | ||
|
||
public function build(): QueryBuilder | ||
{ | ||
// Text search | ||
$searchText = $this->request->getSearchText(); | ||
|
||
if ($searchText !== null) { | ||
$whereDql = []; | ||
|
||
foreach ($this->searchables as $searchable) { | ||
$relationCol = $searchable->getRelation(); | ||
$colName = 'entity.' . $searchable->getColumn(); | ||
|
||
if ($relationCol !== null) { | ||
$alias = 'alias_' . $searchable->getRelation() . '_' . $searchable->getColumn(); | ||
$this->queryBuilder->leftJoin("entity.{$relationCol}", $alias); | ||
$colName = "{$alias}.{$searchable->getColumn()}"; | ||
} | ||
|
||
$paramName = 'param_' . str_replace('.', '_', $colName); | ||
$value = $searchable->hasFormatter() ? $searchable->format($searchText) : "%{$searchText}%"; | ||
|
||
$whereDql[] = [ | ||
'dql' => "{$colName} {$searchable->getOperator()} :{$paramName}", | ||
'paramName' => $paramName, | ||
'paramValue' => $value | ||
]; | ||
} | ||
|
||
if (count($whereDql) !== 0) { | ||
$where = implode(' OR ', array_map(fn($where) => $where['dql'], $whereDql)); | ||
$this->queryBuilder->andWhere($where); | ||
|
||
foreach ($whereDql as $where) { | ||
$this->queryBuilder->setParameter($where['paramName'], $where['paramValue']); | ||
} | ||
} | ||
} | ||
|
||
return $this->queryBuilder; | ||
} | ||
|
||
/** | ||
* @param string[] $searchables | ||
*/ | ||
public function keepDefaults(array $searchables = ['id', 'createdAt', 'updatedAt']): self | ||
{ | ||
foreach ($searchables as $columnName) { | ||
$this->addSearchable(new Searchable($columnName)); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function addSearchable(Searchable $searchable): self | ||
{ | ||
$this->searchables[$searchable->getColumn()] = $searchable; | ||
return $this; | ||
} | ||
|
||
public function getQueryBuilder(): QueryBuilder | ||
{ | ||
return $this->queryBuilder; | ||
} | ||
|
||
public function getRequest(): CollectionRequest | ||
{ | ||
return $this->request; | ||
} | ||
|
||
/** @return array{ | ||
* searchables: array{column: string, relation: string|null, operator: string}[] | ||
* } | ||
*/ | ||
public function toArray(): array | ||
{ | ||
$searchables = array_map(fn(Searchable $searchable) => $searchable->toArray(), $this->searchables); | ||
|
||
return [ | ||
'searchables' => array_values($searchables), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Megio\Collection\SearchBuilder; | ||
|
||
class Searchable | ||
{ | ||
/** @var array<int, callable> */ | ||
private array $formatter = []; | ||
|
||
public function __construct( | ||
protected string $column, | ||
protected ?string $relation = null, | ||
protected string $operator = 'LIKE', | ||
?callable $formatter = null | ||
) | ||
{ | ||
if ($formatter !== null) { | ||
$this->formatter[] = $formatter; | ||
} | ||
} | ||
|
||
public function getColumn(): string | ||
{ | ||
return $this->column; | ||
} | ||
|
||
public function getRelation(): ?string | ||
{ | ||
return $this->relation; | ||
} | ||
|
||
public function getOperator(): string | ||
{ | ||
return $this->operator; | ||
} | ||
|
||
public function format(?string $value): mixed | ||
{ | ||
return $this->formatter[0]($value); | ||
} | ||
|
||
public function hasFormatter(): bool | ||
{ | ||
return count($this->formatter) !== 0; | ||
} | ||
|
||
/** | ||
* @return array{column: string, relation: string|null, operator: string} | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [ | ||
'column' => $this->column, | ||
'relation' => $this->relation, | ||
'operator' => $this->operator, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.