1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Objects; |
4
|
|
|
|
5
|
|
|
use Mado\QueryBundle\Objects\Operator; |
6
|
|
|
use Mado\QueryBundle\Objects\Salt; |
7
|
|
|
|
8
|
|
|
final class WhereCondition |
9
|
|
|
{ |
10
|
|
|
private $entityAlias; |
11
|
|
|
|
12
|
|
|
private $operator; |
13
|
|
|
|
14
|
|
|
private $salt; |
15
|
|
|
|
16
|
|
|
private $filtering; |
17
|
|
|
|
18
|
|
|
private $value; |
19
|
|
|
|
20
|
|
|
private $fieldName; |
21
|
|
|
|
22
|
|
|
private $relationEntityAlias; |
23
|
|
|
|
24
|
|
|
public function setEntityAlias(string $entityAlias) |
25
|
|
|
{ |
26
|
|
|
$this->entityAlias = $entityAlias; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function setOperator(Operator $operator) |
30
|
|
|
{ |
31
|
|
|
$this->operator = $operator; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function setSalt(Salt $salt) |
35
|
|
|
{ |
36
|
|
|
$this->salt = $salt; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setFiltering(FilteringObject $filtering) |
40
|
|
|
{ |
41
|
|
|
$this->filtering = $filtering; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setValue(string $value) |
45
|
|
|
{ |
46
|
|
|
$this->value = $value; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setFieldName(string $fieldName) |
50
|
|
|
{ |
51
|
|
|
$this->fieldName = $fieldName; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function isListOperator() |
55
|
|
|
{ |
56
|
|
|
return $this->filtering->isListOperator(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getCondition() |
60
|
|
|
{ |
61
|
|
|
if ($this->filtering->hasOperator()) { |
62
|
|
|
if ($this->isListOperator()) { |
63
|
|
|
return $this->entityAlias . '.' . $this->fieldName . ' ' . |
64
|
|
|
$this->operator->getMeta() . ' ' . |
65
|
|
|
'(:field_' . $this->fieldName . $this->salt->getSalt() . ')'; |
66
|
|
View Code Duplication |
} else if ($this->filtering->isFieldEqualsOperator()) { |
|
|
|
|
67
|
|
|
return $this->entityAlias . '.' . $this->fieldName . ' ' . |
68
|
|
|
$this->operator->getMeta() . ' ' . |
69
|
|
|
$this->entityAlias . '.' . $this->value; |
70
|
|
|
} else { |
71
|
|
|
return $this->entityAlias . '.' . $this->fieldName . ' ' . |
72
|
|
|
$this->operator->getMeta() . ' ' . |
73
|
|
|
':field_' . $this->fieldName . $this->salt->getSalt(); |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
return $this->entityAlias . '.' . $this->fieldName . ' ' . |
77
|
|
|
$this->operator->getMeta() . ' ' . |
78
|
|
|
':field_' . $this->fieldName . $this->salt->getSalt(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setRelationEntityAlias(string $relationEntityAlias) |
83
|
|
|
{ |
84
|
|
|
$this->relationEntityAlias = $relationEntityAlias; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getEmbeddedCondition() |
88
|
|
|
{ |
89
|
|
View Code Duplication |
if ($this->isListOperator()) { |
|
|
|
|
90
|
|
|
return $this->relationEntityAlias . '.' . $this->fieldName . ' ' . |
91
|
|
|
$this->operator->getMeta() . ' ' . |
92
|
|
|
'(:field_' . $this->fieldName . $this->salt->getSalt() . ')'; |
93
|
|
|
} else { |
94
|
|
|
return $this->relationEntityAlias . '.' . $this->fieldName . ' ' . |
95
|
|
|
$this->operator->getMeta() . ' ' . |
96
|
|
|
':field_' . $this->fieldName . $this->salt->getSalt(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getFieldName() |
101
|
|
|
{ |
102
|
|
|
return 'field_' . $this->fieldName . $this->salt->getSalt(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.