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