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\Mapping\Type; |
16
|
|
|
use DoctrineElastic\Query\Walker\Helper\WalkerHelper; |
17
|
|
|
use DoctrineElastic\Query\Walker\WhereWalker; |
18
|
|
|
use DoctrineElastic\Service\ElasticSearchService; |
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
|
|
|
$type = $this->getEntityElasticType($this->_className); |
55
|
|
|
|
56
|
|
|
$searchParams->setType($type->getName()); |
57
|
|
|
$searchParams->setIndex($type->getIndex()); |
58
|
|
|
|
59
|
|
|
$this->walkSelectClause($this->_ast->selectClause, $searchParams); |
|
|
|
|
60
|
|
|
$this->walkFromClause($this->_ast->fromClause, $searchParams); |
|
|
|
|
61
|
|
|
|
62
|
|
|
if ($this->_ast->whereClause) { |
63
|
|
|
$this->walkWhereClause($this->_ast->whereClause, $searchParams); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($this->_ast->groupByClause) { |
67
|
|
|
$this->walkGroupByClause($this->_ast->groupByClause, $searchParams); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($this->_ast->havingClause) { |
71
|
|
|
$this->walkHavingClause($this->_ast->havingClause, $searchParams); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($this->_ast->orderByClause) { |
75
|
|
|
$this->walkOrderByClause($this->_ast->orderByClause, $searchParams); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$searchParams->setSize($size); |
79
|
|
|
$searchParams->setFrom($offset); |
80
|
|
|
|
81
|
|
|
return $searchParams; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function walkSelectClause(SelectClause $selectClause, SearchParams $searchParams) { |
|
|
|
|
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function walkFromClause(FromClause $fromClause, SearchParams $searchParams) { |
|
|
|
|
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function walkWhereClause(WhereClause $whereClause, SearchParams $searchParams) { |
93
|
|
|
$whereWalker = new WhereWalker($this->query, $this->_className, $this->walkerHelper); |
94
|
|
|
$whereWalker->walk($whereClause, $searchParams); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function walkGroupByClause(GroupByClause $groupByClause, SearchParams $searchParams) { |
|
|
|
|
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function walkHavingClause(HavingClause $havingClause, SearchParams $searchParams) { |
|
|
|
|
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function walkOrderByClause(OrderByClause $orderByClause, SearchParams $searchParams) { |
106
|
|
|
/** @var OrderByItem $item */ |
107
|
|
|
foreach ($orderByClause->orderByItems as $item) { |
108
|
|
|
$order = $item->type; |
109
|
|
|
$propertyName = $item->expression->field; |
110
|
|
|
$ESField = $this->walkerHelper->getEntityElasticField($propertyName, $this->_className, $this->query); |
111
|
|
|
|
112
|
|
|
$searchParams->setSort([$ESField->name => strtolower($order)]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $className |
118
|
|
|
* @return Type |
119
|
|
|
*/ |
120
|
|
|
private function getEntityElasticType($className) { |
121
|
|
|
$classMetadata = $this->query->getEntityManager()->getClassMetadata($className); |
122
|
|
|
|
123
|
|
|
$entityPersister = $this->query->getEntityManager()->getUnitOfWork() |
124
|
|
|
->getEntityPersister($className); |
125
|
|
|
/** @var Type $type */ |
126
|
|
|
$type = $entityPersister->getAnnotionReader() |
127
|
|
|
->getClassAnnotation($classMetadata->getReflectionClass(), Type::class); |
128
|
|
|
|
129
|
|
|
return $type; |
130
|
|
|
} |
131
|
|
|
} |
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: