1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB\Query; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Comparable\ComparatorInterface; |
14
|
|
|
use Cubiche\Core\Specification\SpecificationInterface; |
15
|
|
|
use Cubiche\Core\Delegate\Delegate; |
16
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
17
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Query Builder Class. |
21
|
|
|
* |
22
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class QueryBuilder extends Builder |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var SpecificationVisitorFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
protected $specificationVisitorFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ComparatorVisitorFactoryInterface |
33
|
|
|
*/ |
34
|
|
|
protected $comparatorVisitorFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Delegate |
38
|
|
|
*/ |
39
|
|
|
private $factory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param DocumentManager $dm |
43
|
|
|
* @param string $documentName |
44
|
|
|
* @param SpecificationVisitorFactoryInterface $specificationVisitorFactory |
45
|
|
|
* @param ComparatorVisitorFactoryInterface $comparatorVisitorFactory |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
DocumentManager $dm, |
49
|
|
|
$documentName = null, |
50
|
|
|
SpecificationVisitorFactoryInterface $specificationVisitorFactory = null, |
51
|
|
|
ComparatorVisitorFactoryInterface $comparatorVisitorFactory = null |
52
|
|
|
) { |
53
|
|
|
parent::__construct($dm, $documentName); |
54
|
|
|
|
55
|
|
|
if ($specificationVisitorFactory === null) { |
56
|
|
|
$specificationVisitorFactory = new SpecificationVisitorFactory(); |
57
|
|
|
} |
58
|
|
|
if ($comparatorVisitorFactory === null) { |
59
|
|
|
$comparatorVisitorFactory = new ComparatorVisitorFactory(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->specificationVisitorFactory = $specificationVisitorFactory; |
63
|
|
|
$this->comparatorVisitorFactory = $comparatorVisitorFactory; |
64
|
|
|
|
65
|
|
|
$this->factory = Delegate::fromClosure( |
66
|
|
|
function () use ($dm, $documentName) { |
67
|
|
|
return new static($dm, $documentName); |
68
|
|
|
} |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param SpecificationInterface $criteria |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function addSearchCriteria(SpecificationInterface $criteria) |
78
|
|
|
{ |
79
|
|
|
$criteria->accept($this->specificationVisitorFactory->create($this)); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param ComparatorInterface $criteria |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function addSortCriteria(ComparatorInterface $criteria) |
90
|
|
|
{ |
91
|
|
|
$criteria->accept($this->comparatorVisitorFactory->create($this)); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return \Doctrine\ODM\MongoDB\Query\Expr |
98
|
|
|
*/ |
99
|
|
|
public function getExpr() |
100
|
|
|
{ |
101
|
|
|
return $this->expr; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return static |
106
|
|
|
*/ |
107
|
|
|
public function createQueryBuilder() |
108
|
|
|
{ |
109
|
|
|
return $this->factory->__invoke(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|