|
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; |
|
21
|
|
|
|
|
22
|
|
|
class Bucket extends AbstractBucket |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private $boundaries; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
private $default; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* An array of values based on the groupBy expression that specify the |
|
36
|
|
|
* boundaries for each bucket. |
|
37
|
|
|
* |
|
38
|
|
|
* Each adjacent pair of values acts as the inclusive lower boundary and the |
|
39
|
|
|
* exclusive upper boundary for the bucket. You must specify at least two |
|
40
|
|
|
* boundaries. The specified values must be in ascending order and all of |
|
41
|
|
|
* the same type. The exception is if the values are of mixed numeric types. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array ...$boundaries |
|
44
|
|
|
* |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
4 |
|
public function boundaries(...$boundaries) |
|
48
|
|
|
{ |
|
49
|
4 |
|
$this->boundaries = $boundaries; |
|
50
|
4 |
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* A literal that specifies the _id of an additional bucket that contains |
|
55
|
|
|
* all documents whose groupBy expression result does not fall into a bucket |
|
56
|
|
|
* specified by boundaries. |
|
57
|
|
|
* |
|
58
|
|
|
* @param mixed $default |
|
59
|
|
|
* |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function defaultBucket($default) |
|
63
|
|
|
{ |
|
64
|
3 |
|
$this->default = $default; |
|
65
|
3 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* A document that specifies the fields to include in the output documents |
|
70
|
|
|
* in addition to the _id field. To specify the field to include, you must |
|
71
|
|
|
* use accumulator expressions. |
|
72
|
|
|
* |
|
73
|
|
|
* @return Bucket\BucketOutput |
|
74
|
|
|
*/ |
|
75
|
3 |
|
public function output() |
|
76
|
|
|
{ |
|
77
|
3 |
|
if (! $this->output) { |
|
78
|
3 |
|
$this->output = new Bucket\BucketOutput($this->builder, $this); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
return $this->output; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
4 |
|
protected function getExtraPipelineFields() |
|
85
|
|
|
{ |
|
86
|
4 |
|
$fields = ['boundaries' => $this->boundaries]; |
|
87
|
4 |
|
if ($this->default !== null) { |
|
88
|
3 |
|
$fields['default'] = $this->default; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
4 |
|
return $fields; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
4 |
|
protected function getStageName() |
|
95
|
|
|
{ |
|
96
|
4 |
|
return '$bucket'; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|