|
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\Stage\Bucket; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Builder; |
|
23
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Expr; |
|
24
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Stage; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Fluent interface for adding an output specification to a bucket stage. |
|
28
|
|
|
* |
|
29
|
|
|
* @author alcaeus <[email protected]> |
|
30
|
|
|
* @since 1.5 |
|
31
|
|
|
*/ |
|
32
|
|
|
class BucketOutput extends AbstractOutput |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @param Builder $builder |
|
36
|
|
|
* @param Stage\Bucket $bucket |
|
37
|
|
|
*/ |
|
38
|
3 |
|
public function __construct(Builder $builder, Stage\Bucket $bucket) |
|
39
|
|
|
{ |
|
40
|
3 |
|
parent::__construct($builder, $bucket); |
|
41
|
3 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* An expression to group documents by. To specify a field path, prefix the |
|
45
|
|
|
* field name with a dollar sign $ and enclose it in quotes. |
|
46
|
|
|
* |
|
47
|
|
|
* @param mixed|Expr $expression |
|
48
|
|
|
* @return Stage\Bucket |
|
49
|
|
|
*/ |
|
50
|
|
|
public function groupBy($expression) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->bucket->groupBy($expression); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* An array of values based on the groupBy expression that specify the |
|
57
|
|
|
* boundaries for each bucket. |
|
58
|
|
|
* |
|
59
|
|
|
* Each adjacent pair of values acts as the inclusive lower boundary and the |
|
60
|
|
|
* exclusive upper boundary for the bucket. You must specify at least two |
|
61
|
|
|
* boundaries. The specified values must be in ascending order and all of |
|
62
|
|
|
* the same type. The exception is if the values are of mixed numeric types. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array ...$boundaries |
|
65
|
|
|
* |
|
66
|
|
|
* @return Stage\Bucket |
|
67
|
|
|
*/ |
|
68
|
|
|
public function boundaries(...$boundaries) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->bucket->boundaries(...$boundaries); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* A literal that specifies the _id of an additional bucket that contains |
|
75
|
|
|
* all documents whose groupBy expression result does not fall into a bucket |
|
76
|
|
|
* specified by boundaries. |
|
77
|
|
|
* |
|
78
|
|
|
* @param mixed $default |
|
79
|
|
|
* |
|
80
|
|
|
* @return Stage\Bucket |
|
81
|
|
|
*/ |
|
82
|
|
|
public function defaultBucket($default) |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->bucket->defaultBucket($default); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: