|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Artprima\QueryFilterBundle\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Artprima\QueryFilterBundle\Query\Condition\ConditionInterface; |
|
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ConditionManager |
|
10
|
|
|
* |
|
11
|
|
|
* @author Denis Voytyuk <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* @package Artprima\QueryFilterBundle\Query |
|
14
|
|
|
*/ |
|
15
|
|
|
class ConditionManager implements \ArrayAccess, \Iterator |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ConditionInterface[] |
|
19
|
|
|
*/ |
|
20
|
|
|
private $conditions = []; |
|
21
|
|
|
|
|
22
|
1 |
|
public function wrapQueryBuilder(QueryBuilder $qb): ProxyQueryBuilder |
|
23
|
|
|
{ |
|
24
|
1 |
|
return new ProxyQueryBuilder($qb, $this); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
4 |
|
public function add(ConditionInterface $condition, string $name): void |
|
28
|
|
|
{ |
|
29
|
4 |
|
$this->conditions[$name] = $condition; |
|
30
|
4 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return ConditionInterface[] |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function all(): array |
|
36
|
|
|
{ |
|
37
|
1 |
|
return $this->conditions; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
public function offsetExists($offset) |
|
41
|
|
|
{ |
|
42
|
5 |
|
return array_key_exists($offset, $this->conditions); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
5 |
|
public function offsetGet($offset) |
|
46
|
|
|
{ |
|
47
|
5 |
|
return $this->offsetExists($offset) ? $this->conditions[$offset] : null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function offsetSet($offset, $value) |
|
51
|
|
|
{ |
|
52
|
1 |
|
if ($offset === null) { |
|
53
|
|
|
$this->conditions[] = $value; |
|
54
|
|
|
} else { |
|
55
|
1 |
|
$this->conditions[$offset] = $value; |
|
56
|
|
|
} |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function offsetUnset($offset) { |
|
60
|
1 |
|
unset($this->conditions[$offset]); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function rewind() { |
|
64
|
1 |
|
return reset($this->conditions); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public function current() { |
|
68
|
1 |
|
return current($this->conditions); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function key() { |
|
72
|
1 |
|
return key($this->conditions); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public function next() { |
|
76
|
1 |
|
return next($this->conditions); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public function valid() { |
|
80
|
1 |
|
return key($this->conditions) !== null; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|