1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\Doctrine\Manager\ORM; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\Query\Expr\Composite; |
15
|
|
|
use Doctrine\ORM\Query\Expr\Orx; |
16
|
|
|
use Doctrine\ORM\QueryBuilder; |
17
|
|
|
|
18
|
|
|
trait SearchQueryTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Creates a like search for a given field and text values. |
22
|
|
|
*/ |
23
|
|
|
final protected function searchWhere(QueryBuilder $qb, string $field, array $values, bool $strict = false): Composite |
24
|
|
|
{ |
25
|
|
|
$orx = $qb->expr()->orX(); |
26
|
|
|
foreach ($values as $index => $word) { |
27
|
|
|
$orx->add(sprintf('%s = :name'.$index, $field)); |
28
|
|
|
$qb->setParameter('name'.$index, $word); |
29
|
|
|
|
30
|
|
|
if (!$strict) { |
31
|
|
|
$this->buildLikeExpressions($qb, $orx, $field, $word, $index); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $orx; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function buildLikeExpressions(QueryBuilder $qb, Orx $orx, string $field, string $word, int $index): void |
39
|
|
|
{ |
40
|
|
|
$orx->add(sprintf('%s LIKE :name'.$index.'_any', $field)); |
41
|
|
|
$orx->add(sprintf('%s LIKE :name'.$index.'_pre', $field)); |
42
|
|
|
$orx->add(sprintf('%s LIKE :name'.$index.'_suf', $field)); |
43
|
|
|
|
44
|
|
|
$qb->setParameter('name'.$index.'_any', '% '.$word.' %'); |
45
|
|
|
$qb->setParameter('name'.$index.'_pre', '% '.$word); |
46
|
|
|
$qb->setParameter('name'.$index.'_suf', $word.' %'); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|