1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ODM\MongoDB\Aggregation; |
21
|
|
|
|
22
|
|
|
use Doctrine\MongoDB\Aggregation\Builder as BaseBuilder; |
23
|
|
|
use Doctrine\MongoDB\CommandCursor as BaseCommandCursor; |
24
|
|
|
use Doctrine\ODM\MongoDB\CommandCursor; |
25
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
26
|
|
|
use Doctrine\ODM\MongoDB\Query\Expr as QueryExpr; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Fluent interface for building aggregation pipelines. |
30
|
|
|
* |
31
|
|
|
* @author alcaeus <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class Builder extends BaseBuilder |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* The DocumentManager instance for this query |
37
|
|
|
* |
38
|
|
|
* @var DocumentManager |
39
|
|
|
*/ |
40
|
|
|
private $dm; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The ClassMetadata instance. |
44
|
|
|
* |
45
|
|
|
* @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
46
|
|
|
*/ |
47
|
|
|
private $class; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $hydrationClass; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Create a new aggregation builder. |
56
|
|
|
* |
57
|
|
|
* @param DocumentManager $dm |
58
|
|
|
* @param string $documentName |
59
|
|
|
*/ |
60
|
3 |
|
public function __construct(DocumentManager $dm, $documentName) |
61
|
|
|
{ |
62
|
3 |
|
$this->dm = $dm; |
63
|
3 |
|
$this->class = $this->dm->getClassMetadata($documentName); |
64
|
|
|
|
65
|
3 |
|
parent::__construct($this->dm->getDocumentCollection($documentName)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function execute($options = array()) |
72
|
|
|
{ |
73
|
|
|
// Force cursor to be used |
74
|
|
|
$options = array_merge($options, array('cursor' => true)); |
75
|
|
|
|
76
|
|
|
$cursor = parent::execute($options); |
77
|
|
|
|
78
|
|
|
return $this->prepareCursor($cursor); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $className |
83
|
|
|
* |
84
|
|
|
* @return self |
85
|
|
|
*/ |
86
|
|
|
public function hydrate($className) |
87
|
|
|
{ |
88
|
|
|
$this->hydrationClass = $className; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return QueryExpr |
95
|
|
|
*/ |
96
|
|
|
public function matchExpr() |
97
|
|
|
{ |
98
|
|
|
$expr = new QueryExpr($this->dm); |
99
|
|
|
$expr->setClassMetadata($this->class); |
100
|
|
|
|
101
|
|
|
return $expr; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Expr |
106
|
|
|
*/ |
107
|
|
|
public function expr() |
108
|
|
|
{ |
109
|
|
|
return new Expr($this->dm, $this->class); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param BaseCommandCursor $cursor |
114
|
|
|
* |
115
|
|
|
* @return CommandCursor |
116
|
|
|
*/ |
117
|
|
|
protected function prepareCursor(BaseCommandCursor $cursor) |
118
|
|
|
{ |
119
|
|
|
$class = null; |
120
|
|
|
if ($this->hydrationClass) { |
121
|
|
|
$class = $this->dm->getClassMetadata($this->hydrationClass); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return new CommandCursor($cursor, $this->dm->getUnitOfWork(), $class); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.