| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the PHPMongo package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Dmytro Sokil <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Sokil\Mongo\Pipeline; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Sokil\Mongo\ArrayableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Group stage accumulators | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @link http://docs.mongodb.org/manual/meta/aggregation-quick-reference/#accumulators | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author Dmytro Sokil <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | class GroupStage implements ArrayableInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     private $stage = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function setId($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->stage['_id'] = $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * Sum accumulator | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * Calculates and returns the sum of all the numeric values that result | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * from applying a specified expression to each document in a group of | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * documents that share the same group by key. $sum ignores | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * non-numeric values. | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @link http://docs.mongodb.org/manual/reference/operator/aggregation/sum | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param string $field | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param literal|callable|\Sokil\Mongo\Pipeline\Expression $expression Expression | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @return \Sokil\Mongo\Pipeline\GroupStage | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function sum($field, $expression) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->stage[$field]['$sum'] = Expression::normalize($expression); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * Add to set accumulator | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * Returns an array of all unique values that results from applying an | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * expression to each document in a group of documents that share the | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * same group by key. Order of the elements in the output | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * array is unspecified. | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @link http://docs.mongodb.org/manual/reference/operator/aggregation/addToSet | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @param string $field | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @param literal|callable|\Sokil\Mongo\Pipeline\Expression $expression Expression | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @return \Sokil\Mongo\Pipeline\GroupStage | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function addToSet($field, $expression) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->stage[$field]['$addToSet'] = Expression::normalize($expression); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * Average accumulator | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * Returns the average value of the numeric values that result from | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * applying a specified expression to each document in a group of | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * documents that share the same group by key. $avg ignores | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * non-numeric values. | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @link http://docs.mongodb.org/manual/reference/operator/aggregation/avg | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * @param string $field | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @param literal|callable|\Sokil\Mongo\Pipeline\Expression $expression Expression | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @return \Sokil\Mongo\Pipeline\GroupStage | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function avg($field, $expression) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $this->stage[$field]['$avg'] = Expression::normalize($expression); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * Returns the value that results from applying an expression | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      * to the first document in a group of documents that share the same | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * group by key. Only meaningful when documents are in a defined order. | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * @link http://docs.mongodb.org/manual/reference/operator/aggregation/first | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * @param string $field | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @param literal|callable|\Sokil\Mongo\Pipeline\Expression $expression Expression | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @return \Sokil\Mongo\Pipeline\GroupStage | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function first($field, $expression) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->stage[$field]['$first'] = Expression::normalize($expression); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * Returns the value that results from applying an expression to the last | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * document in a group of documents that share the same group by a field. | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      * Only meaningful when documents are in a defined order. | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      * @link http://docs.mongodb.org/manual/reference/operator/aggregation/last | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |      * @param string $field | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |      * @param literal|callable|\Sokil\Mongo\Pipeline\Expression $expression Expression | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |      * @return \Sokil\Mongo\Pipeline\GroupStage | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     public function last($field, $expression) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $this->stage[$field]['$last'] = Expression::normalize($expression); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      * Returns the highest value that results from applying an expression | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |      * to each document in a group of documents that share the same group by key. | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |      * @link http://docs.mongodb.org/manual/reference/operator/aggregation/max | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      * @param string $field | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      * @param literal|callable|\Sokil\Mongo\Pipeline\Expression $expression Expression | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * @return \Sokil\Mongo\Pipeline\GroupStage | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     public function max($field, $expression) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $this->stage[$field]['$max'] = Expression::normalize($expression); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |      * Returns the lowest value that results from applying an expression | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |      * to each document in a group of documents that share the same group by key. | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |      * @link http://docs.mongodb.org/manual/reference/operator/aggregation/min | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |      * @param string $field | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |      * @param literal|callable|\Sokil\Mongo\Pipeline\Expression $expression Expression | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |      * @return \Sokil\Mongo\Pipeline\GroupStage | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     public function min($field, $expression) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $this->stage[$field]['$min'] = Expression::normalize($expression); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |      * Returns an array of all values that result from applying an expression | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |      * to each document in a group of documents that share the same group by key. | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |      * @link http://docs.mongodb.org/manual/reference/operator/aggregation/push | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |      * @param string $field | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |      * @param literal|callable|\Sokil\Mongo\Pipeline\Expression $expression Expression | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |      * @return \Sokil\Mongo\Pipeline\GroupStage | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     public function push($field, $expression) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         $this->stage[$field]['$push'] = Expression::normalize($expression); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     public function toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         return $this->stage; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 187 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 188 |  |  |  |