1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\DoctrineSpecification\Filter; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
6
|
|
|
use Doctrine\ORM\Query\Expr\Comparison as DoctrineComparison; |
7
|
|
|
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter; |
8
|
|
|
use Happyr\DoctrineSpecification\Operand\LikePattern; |
9
|
|
|
use Happyr\DoctrineSpecification\Operand\Operand; |
10
|
|
|
|
11
|
|
|
class Like implements Filter |
12
|
|
|
{ |
13
|
|
|
const CONTAINS = LikePattern::CONTAINS; |
14
|
|
|
|
15
|
|
|
const ENDS_WITH = LikePattern::ENDS_WITH; |
16
|
|
|
|
17
|
|
|
const STARTS_WITH = LikePattern::STARTS_WITH; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Operand|string |
21
|
|
|
*/ |
22
|
|
|
private $field; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var LikePattern |
26
|
|
|
*/ |
27
|
|
|
private $value; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $dqlAlias; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Operand|string $field |
36
|
|
|
* @param LikePattern|string $value |
37
|
|
|
* @param string $format |
38
|
|
|
* @param string|null $dqlAlias |
39
|
|
|
*/ |
40
|
|
|
public function __construct($field, $value, $format = LikePattern::CONTAINS, $dqlAlias = null) |
41
|
|
|
{ |
42
|
|
|
if (!($value instanceof LikePattern)) { |
43
|
|
|
$value = new LikePattern($value, $format); |
44
|
|
|
} |
45
|
|
|
$this->field = $field; |
46
|
|
|
$this->value = $value; |
47
|
|
|
$this->dqlAlias = $dqlAlias; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param QueryBuilder $qb |
52
|
|
|
* @param string $dqlAlias |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function getFilter(QueryBuilder $qb, $dqlAlias) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
if (null !== $this->dqlAlias) { |
59
|
|
|
$dqlAlias = $this->dqlAlias; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$field = ArgumentToOperandConverter::toField($this->field); |
63
|
|
|
|
64
|
|
|
$field = $field->transform($qb, $dqlAlias); |
65
|
|
|
$value = $this->value->transform($qb, $dqlAlias); |
66
|
|
|
|
67
|
|
|
return (string) new DoctrineComparison($field, 'LIKE', $value); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.