1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Happyr Doctrine Specification package. |
5
|
|
|
* |
6
|
|
|
* (c) Tobias Nyholm <[email protected]> |
7
|
|
|
* Kacper Gunia <[email protected]> |
8
|
|
|
* Peter Gribanov <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Happyr\DoctrineSpecification\Filter; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\Query\Expr\Comparison as DoctrineComparison; |
17
|
|
|
use Doctrine\ORM\QueryBuilder; |
18
|
|
|
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter; |
19
|
|
|
use Happyr\DoctrineSpecification\Operand\LikePattern; |
20
|
|
|
use Happyr\DoctrineSpecification\Operand\Operand; |
21
|
|
|
|
22
|
|
|
class Like implements Filter |
23
|
|
|
{ |
24
|
|
|
const CONTAINS = LikePattern::CONTAINS; |
25
|
|
|
|
26
|
|
|
const ENDS_WITH = LikePattern::ENDS_WITH; |
27
|
|
|
|
28
|
|
|
const STARTS_WITH = LikePattern::STARTS_WITH; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Operand|string |
32
|
|
|
*/ |
33
|
|
|
private $field; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var LikePattern |
37
|
|
|
*/ |
38
|
|
|
private $value; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string|null |
42
|
|
|
*/ |
43
|
|
|
private $dqlAlias; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Operand|string $field |
47
|
|
|
* @param LikePattern|string $value |
48
|
|
|
* @param string $format |
49
|
|
|
* @param string|null $dqlAlias |
50
|
|
|
*/ |
51
|
|
|
public function __construct($field, $value, $format = LikePattern::CONTAINS, $dqlAlias = null) |
52
|
|
|
{ |
53
|
|
|
if (!($value instanceof LikePattern)) { |
54
|
|
|
$value = new LikePattern($value, $format); |
55
|
|
|
} |
56
|
|
|
$this->field = $field; |
57
|
|
|
$this->value = $value; |
58
|
|
|
$this->dqlAlias = $dqlAlias; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param QueryBuilder $qb |
63
|
|
|
* @param string $dqlAlias |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function getFilter(QueryBuilder $qb, $dqlAlias) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if (null !== $this->dqlAlias) { |
70
|
|
|
$dqlAlias = $this->dqlAlias; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$field = ArgumentToOperandConverter::toField($this->field); |
74
|
|
|
|
75
|
|
|
$field = $field->transform($qb, $dqlAlias); |
76
|
|
|
$value = $this->value->transform($qb, $dqlAlias); |
77
|
|
|
|
78
|
|
|
return (string) new DoctrineComparison($field, 'LIKE', $value); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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.