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\QueryBuilder; |
17
|
|
|
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter; |
18
|
|
|
use Happyr\DoctrineSpecification\Operand\Operand; |
19
|
|
|
|
20
|
|
View Code Duplication |
class In implements Filter |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @deprecated This property will be marked as private in 2.0. |
24
|
|
|
* |
25
|
|
|
* @var Operand|string |
26
|
|
|
*/ |
27
|
|
|
protected $field; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @deprecated This property will be marked as private in 2.0. |
31
|
|
|
* |
32
|
|
|
* @var Operand|mixed |
33
|
|
|
*/ |
34
|
|
|
protected $value; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @deprecated This property will be marked as private in 2.0. |
38
|
|
|
* |
39
|
|
|
* @var string|null |
40
|
|
|
*/ |
41
|
|
|
protected $dqlAlias; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Make sure the $field has a value equals to $value. |
45
|
|
|
* |
46
|
|
|
* @param Operand|string $field |
47
|
|
|
* @param Operand|mixed $value |
48
|
|
|
* @param string|null $dqlAlias |
49
|
|
|
*/ |
50
|
|
|
public function __construct($field, $value, $dqlAlias = null) |
51
|
|
|
{ |
52
|
|
|
$this->field = $field; |
|
|
|
|
53
|
|
|
$this->value = $value; |
|
|
|
|
54
|
|
|
$this->dqlAlias = $dqlAlias; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param QueryBuilder $qb |
59
|
|
|
* @param string $dqlAlias |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getFilter(QueryBuilder $qb, $dqlAlias) |
64
|
|
|
{ |
65
|
|
|
if (null !== $this->dqlAlias) { |
|
|
|
|
66
|
|
|
$dqlAlias = $this->dqlAlias; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$field = ArgumentToOperandConverter::toField($this->field); |
|
|
|
|
70
|
|
|
$value = ArgumentToOperandConverter::toValue($this->value); |
|
|
|
|
71
|
|
|
|
72
|
|
|
return (string) $qb->expr()->in( |
73
|
|
|
$field->transform($qb, $dqlAlias), |
74
|
|
|
$value->transform($qb, $dqlAlias) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
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.