|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Queries; |
|
4
|
|
|
|
|
5
|
|
|
use Mado\QueryBundle\Services\StringParser; |
|
6
|
|
|
|
|
7
|
|
|
class AndFilter |
|
8
|
|
|
{ |
|
9
|
|
|
private $entityAlias; |
|
10
|
|
|
|
|
11
|
|
|
private $fields; |
|
12
|
|
|
|
|
13
|
|
|
private $join; |
|
14
|
|
|
|
|
15
|
|
|
private $conditions; |
|
16
|
|
|
|
|
17
|
|
|
private $parameters; |
|
18
|
|
|
|
|
19
|
|
|
private $relationEntityAlias; |
|
20
|
|
|
|
|
21
|
|
|
private $parser; |
|
22
|
|
|
|
|
23
|
12 |
|
public function __construct(string $entityAlias, array $fields, Join $join) |
|
24
|
|
|
{ |
|
25
|
12 |
|
$this->entityAlias = $entityAlias; |
|
26
|
12 |
|
$this->fields = $fields; |
|
27
|
12 |
|
$this->join = $join; |
|
28
|
|
|
|
|
29
|
12 |
|
$this->conditions = []; |
|
30
|
12 |
|
$this->parameters = []; |
|
31
|
12 |
|
$this->parser = new StringParser(); |
|
32
|
12 |
|
} |
|
33
|
|
|
|
|
34
|
12 |
|
public function createFilter(array $andFilters) |
|
35
|
|
|
{ |
|
36
|
12 |
|
foreach ($andFilters as $filter => $value) { |
|
37
|
12 |
|
$this->applyFilter( |
|
38
|
12 |
|
Objects\FilterObject::fromRawFilter($filter), |
|
39
|
12 |
|
$value, |
|
40
|
12 |
|
Objects\Value::fromFilter($value) |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
12 |
|
} |
|
44
|
|
|
|
|
45
|
12 |
|
private function applyFilter( |
|
46
|
|
|
Objects\FilterObject $filterObject, |
|
47
|
|
|
$value, |
|
48
|
|
|
Objects\Value $filterValue |
|
49
|
|
|
) { |
|
50
|
12 |
|
$whereCondition = $this->entityAlias . '.' . $filterObject->getFieldName() . ' ' |
|
51
|
12 |
|
. $filterObject->getOperatorMeta(); |
|
52
|
|
|
|
|
53
|
12 |
|
if (in_array($filterObject->getFieldName(), $this->fields)) { |
|
54
|
7 |
|
$salt = '_' . random_int(111, 999); |
|
55
|
|
|
|
|
56
|
7 |
|
if ($filterObject->isListType()) { |
|
57
|
1 |
|
$whereCondition .= ' (:field_' . $filterObject->getFieldName() . $salt . ')'; |
|
58
|
6 |
|
} elseif ($filterObject->isFieldEqualityType()) { |
|
59
|
1 |
|
$whereCondition .= ' ' . $this->entityAlias . '.' . $value; |
|
60
|
5 |
|
} elseif ($filterObject->isNullType()) { |
|
61
|
2 |
|
$whereCondition .= ' '; |
|
62
|
|
|
} else { |
|
63
|
3 |
|
$whereCondition .= ' :field_' . $filterObject->getFieldName() . $salt; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
7 |
|
$this->conditions[] = $whereCondition; |
|
67
|
|
|
|
|
68
|
7 |
|
if ($filterObject->haveOperatorSubstitutionPattern()) { |
|
69
|
2 |
|
if ($filterObject->isListType()) { |
|
70
|
1 |
|
$value = explode(',', $value); |
|
71
|
|
|
} else { |
|
72
|
1 |
|
$value = str_replace( |
|
73
|
1 |
|
'{string}', |
|
74
|
1 |
|
$value, |
|
75
|
1 |
|
$filterObject->getOperatorsSubstitutionPattern() |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
7 |
|
if (!$filterObject->isNullType()) { |
|
81
|
5 |
|
$param = []; |
|
82
|
5 |
|
$param['field'] = 'field_' . $filterObject->getFieldName() . $salt; |
|
83
|
5 |
|
$param['value'] = $value; |
|
84
|
7 |
|
$this->parameters[] = $param; |
|
85
|
|
|
} |
|
86
|
|
|
} else { |
|
87
|
5 |
|
if (strpos($filterObject->getFieldName(), 'Embedded.') === false) { |
|
88
|
|
|
$whereCondition .= ' ' . $this->entityAlias . '.' . $value; |
|
89
|
|
|
$this->conditions[] = $whereCondition; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// controllo se il filtro si riferisce ad una relazione dell'entità quindi devo fare dei join |
|
94
|
|
|
// esempio per users: filtering[_embedded.groups.name|eq]=admin |
|
95
|
12 |
|
if (strstr($filterObject->getRawFilter(), '_embedded.')) { |
|
96
|
5 |
|
$this->join->join($filterObject->getRawFilter()); |
|
97
|
5 |
|
$this->relationEntityAlias = $this->join->getRelationEntityAlias(); |
|
98
|
|
|
|
|
99
|
5 |
|
$embeddedFields = explode('.', $filterObject->getFieldName()); |
|
100
|
5 |
|
$embeddedFieldName = $this->parser->camelize($embeddedFields[count($embeddedFields) - 1]); |
|
101
|
|
|
|
|
102
|
5 |
|
$salt = '_' . random_int(111, 999); |
|
103
|
|
|
|
|
104
|
5 |
|
$whereCondition = $this->relationEntityAlias . '.' . $embeddedFieldName . ' ' |
|
105
|
5 |
|
. $filterObject->getOperatorMeta(); |
|
106
|
|
|
|
|
107
|
5 |
|
if ($filterObject->isListType()) { |
|
108
|
1 |
|
$whereCondition .= ' (:field_' . $embeddedFieldName . $salt . ')'; |
|
109
|
4 |
|
} elseif ($filterObject->isNullType()) { |
|
110
|
2 |
|
$whereCondition .= ' '; |
|
111
|
|
|
} else { |
|
112
|
2 |
|
$whereCondition .= ' :field_' . $embeddedFieldName . $salt; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
5 |
|
$this->conditions[] = $whereCondition; |
|
116
|
5 |
|
if ($filterObject->haveOperatorSubstitutionPattern()) { |
|
117
|
2 |
|
if ($filterObject->isListType()) { |
|
118
|
1 |
|
$value = explode(',', $filterValue->getFilter()); |
|
119
|
|
|
} else { |
|
120
|
1 |
|
$value = str_replace( |
|
121
|
1 |
|
'{string}', |
|
122
|
1 |
|
$value, |
|
123
|
1 |
|
$filterObject->getOperatorsSubstitutionPattern() |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
5 |
|
if (!$filterObject->isNullType()) { |
|
129
|
3 |
|
$param = []; |
|
130
|
3 |
|
$param['field'] = 'field_' . $embeddedFieldName . $salt; |
|
131
|
3 |
|
$param['value'] = $value; |
|
132
|
3 |
|
$this->parameters[] = $param; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
12 |
|
} |
|
136
|
|
|
|
|
137
|
12 |
|
public function getConditions() :array |
|
138
|
|
|
{ |
|
139
|
12 |
|
return $this->conditions; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
12 |
|
public function getParameters() :array |
|
143
|
|
|
{ |
|
144
|
12 |
|
return $this->parameters; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
12 |
|
public function getInnerJoin() :array |
|
148
|
|
|
{ |
|
149
|
12 |
|
return $this->join->getInnerJoin(); |
|
150
|
|
|
} |
|
151
|
|
|
} |