|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DoctrineElastic\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query\AST\FromClause; |
|
6
|
|
|
use Doctrine\ORM\Query\AST\GroupByClause; |
|
7
|
|
|
use Doctrine\ORM\Query\AST\HavingClause; |
|
8
|
|
|
use Doctrine\ORM\Query\AST\OrderByClause; |
|
9
|
|
|
use Doctrine\ORM\Query\AST\OrderByItem; |
|
10
|
|
|
use Doctrine\ORM\Query\AST\SelectClause; |
|
11
|
|
|
use Doctrine\ORM\Query\AST\SelectStatement; |
|
12
|
|
|
use Doctrine\ORM\Query\AST\WhereClause; |
|
13
|
|
|
use DoctrineElastic\Elastic\ElasticQuery; |
|
14
|
|
|
use DoctrineElastic\Elastic\SearchParams; |
|
15
|
|
|
use DoctrineElastic\Hydrate\AnnotationEntityHydrator; |
|
16
|
|
|
use DoctrineElastic\Mapping\Field; |
|
17
|
|
|
use DoctrineElastic\Query\Walker\Helper\WalkerHelper; |
|
18
|
|
|
use DoctrineElastic\Query\Walker\WhereWalker; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Main walker for queries |
|
22
|
|
|
* |
|
23
|
|
|
* @uahtor Ands |
|
24
|
|
|
*/ |
|
25
|
|
|
class ElasticWalker { |
|
26
|
|
|
|
|
27
|
|
|
/** @var ElasticQuery */ |
|
28
|
|
|
protected $query; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $_className; |
|
32
|
|
|
|
|
33
|
|
|
/** @var WalkerHelper */ |
|
34
|
|
|
private $walkerHelper; |
|
35
|
|
|
|
|
36
|
|
|
/** @var SelectStatement */ |
|
37
|
|
|
private $_ast; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(ElasticQuery $query, SelectStatement $AST, $className) { |
|
40
|
|
|
$this->query = $query; |
|
41
|
|
|
$this->walkerHelper = new WalkerHelper(); |
|
42
|
|
|
$this->_ast = $AST; |
|
43
|
|
|
$this->_className = $className; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return SearchParams |
|
48
|
|
|
*/ |
|
49
|
|
|
public function walkSelectStatement() { |
|
50
|
|
|
$searchParams = new SearchParams(); |
|
51
|
|
|
$size = $this->query->getMaxResults(); |
|
52
|
|
|
$offset = $this->query->getFirstResult(); |
|
53
|
|
|
|
|
54
|
|
|
$persister = $this->query->getEntityManager()->getUnitOfWork()->getEntityPersister($this->_className); |
|
55
|
|
|
$type = $persister->getEntityType(); |
|
56
|
|
|
|
|
57
|
|
|
$searchParams->setType($type->getName()); |
|
58
|
|
|
$searchParams->setIndex($type->getIndex()); |
|
59
|
|
|
|
|
60
|
|
|
$this->walkSelectClause($this->_ast->selectClause, $searchParams); |
|
|
|
|
|
|
61
|
|
|
$this->walkFromClause($this->_ast->fromClause, $searchParams); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
if ($this->_ast->whereClause) { |
|
64
|
|
|
$this->walkWhereClause($this->_ast->whereClause, $searchParams); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($this->_ast->groupByClause) { |
|
68
|
|
|
$this->walkGroupByClause($this->_ast->groupByClause, $searchParams); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($this->_ast->havingClause) { |
|
72
|
|
|
$this->walkHavingClause($this->_ast->havingClause, $searchParams); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($this->_ast->orderByClause) { |
|
76
|
|
|
$this->walkOrderByClause($this->_ast->orderByClause, $searchParams); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$searchParams->setSize($size); |
|
80
|
|
|
$searchParams->setFrom($offset); |
|
81
|
|
|
|
|
82
|
|
|
return $searchParams; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function walkSelectClause(SelectClause $selectClause, SearchParams $searchParams) { |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function walkFromClause(FromClause $fromClause, SearchParams $searchParams) { |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function walkWhereClause(WhereClause $whereClause, SearchParams $searchParams) { |
|
94
|
|
|
$whereWalker = new WhereWalker($this->query, $this->_className, $this->walkerHelper); |
|
95
|
|
|
$whereWalker->walk($whereClause, $searchParams); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function walkGroupByClause(GroupByClause $groupByClause, SearchParams $searchParams) { |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function walkHavingClause(HavingClause $havingClause, SearchParams $searchParams) { |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function walkOrderByClause(OrderByClause $orderByClause, SearchParams $searchParams) { |
|
107
|
|
|
$sort = []; |
|
108
|
|
|
|
|
109
|
|
|
/** @var OrderByItem $item */ |
|
110
|
|
|
foreach ($orderByClause->orderByItems as $item) { |
|
111
|
|
|
$order = $item->type; |
|
112
|
|
|
$propertyName = $item->expression->field; |
|
113
|
|
|
$ESField = $this->getEntityElasticField($propertyName, $this->_className); |
|
114
|
|
|
|
|
115
|
|
|
if (is_null($ESField)) { |
|
116
|
|
|
continue; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$sort[$ESField->name] = strtolower($order); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if (!empty($sort)) { |
|
123
|
|
|
$searchParams->setSort($sort); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param $propertyName |
|
129
|
|
|
* @param $className |
|
130
|
|
|
* @return Field |
|
131
|
|
|
*/ |
|
132
|
|
|
private function getEntityElasticField($propertyName, $className) { |
|
133
|
|
|
/** @var Field[] $fields */ |
|
134
|
|
|
$fields = (new AnnotationEntityHydrator())->extractSpecAnnotations($className, Field::class); |
|
135
|
|
|
|
|
136
|
|
|
return isset($fields[$propertyName]) ? $fields[$propertyName]: null; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: