|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ORM\Testing\EntityFetcherMock; |
|
4
|
|
|
|
|
5
|
|
|
use ORM\Entity; |
|
6
|
|
|
use ORM\EntityFetcher; |
|
7
|
|
|
use ORM\QueryBuilder\QueryBuilder; |
|
8
|
|
|
|
|
9
|
|
|
class Result extends EntityFetcher |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var Entity[] */ |
|
12
|
|
|
protected $entities = []; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string[] */ |
|
15
|
|
|
protected $regularExpressions = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Check if $fetcher matches the current query |
|
19
|
|
|
* |
|
20
|
|
|
* Returns the score for the given EntityFetcher. The more conditions match the higher the score: |
|
21
|
|
|
* - 0 = the query does not match one of the conditions |
|
22
|
|
|
* - 1 = no conditions required to match the query |
|
23
|
|
|
* - n = n-1 conditions matched the query |
|
24
|
|
|
* |
|
25
|
|
|
* @param EntityFetcher $fetcher |
|
26
|
|
|
* @return int |
|
27
|
|
|
*/ |
|
28
|
|
|
public function compare(EntityFetcher $fetcher) |
|
29
|
|
|
{ |
|
30
|
|
|
$result = 1; |
|
31
|
|
|
|
|
32
|
|
|
// check if joins match |
|
33
|
|
|
foreach ($this->joins as $condition) { |
|
34
|
|
|
if (!in_array($condition, $fetcher->joins)) { |
|
35
|
|
|
return 0; |
|
36
|
|
|
} |
|
37
|
|
|
$result++; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// check if where conditions match |
|
41
|
|
|
foreach ($this->where as $condition) { |
|
42
|
|
|
if (!in_array($condition, $fetcher->where)) { |
|
43
|
|
|
return 0; |
|
44
|
|
|
} |
|
45
|
|
|
$result++; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// check if grouping matches |
|
49
|
|
|
foreach ($this->groupBy as $condition) { |
|
50
|
|
|
if (!in_array($condition, $fetcher->groupBy)) { |
|
51
|
|
|
return 0; |
|
52
|
|
|
} |
|
53
|
|
|
$result++; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// check if order matches |
|
57
|
|
|
foreach ($this->orderBy as $condition) { |
|
58
|
|
|
if (!in_array($condition, $fetcher->orderBy)) { |
|
59
|
|
|
return 0; |
|
60
|
|
|
} |
|
61
|
|
|
$result++; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// check if limit and offset matches |
|
65
|
|
|
if ($this->limit) { |
|
66
|
|
|
if ($this->limit !== $fetcher->limit || $this->offset !== $fetcher->offset) { |
|
67
|
|
|
return 0; |
|
68
|
|
|
} |
|
69
|
|
|
$result++; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// check if regular expressions match |
|
73
|
|
|
foreach ($this->regularExpressions as $expression) { |
|
74
|
|
|
if (!preg_match($expression, $fetcher->getQuery())) { |
|
75
|
|
|
return 0; |
|
76
|
|
|
} |
|
77
|
|
|
$result++; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $result; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Add a regular expression that has to match |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $expression |
|
87
|
|
|
* @return $this |
|
88
|
|
|
* @codeCoverageIgnore trivial code |
|
89
|
|
|
*/ |
|
90
|
|
|
public function matches($expression) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->regularExpressions[] = $expression; |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Add entities to the result |
|
98
|
|
|
* |
|
99
|
|
|
* @param Entity ...$entities |
|
100
|
|
|
* @return $this |
|
101
|
|
|
* @codeCoverageIgnore trivial code |
|
102
|
|
|
*/ |
|
103
|
|
|
public function addEntities(Entity ...$entities) |
|
104
|
|
|
{ |
|
105
|
|
|
array_push($this->entities, ...$entities); |
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get the entites for this result |
|
111
|
|
|
* |
|
112
|
|
|
* @return Entity[] |
|
113
|
|
|
* @codeCoverageIgnore trivial code |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getEntities() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->entities; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|