|
1 | 1 | <?php namespace Controllers\API; |
2 | 2 |
|
| 3 | +use Carbon\Carbon; |
3 | 4 | use \Locker\Repository\Query\QueryRepository as QueryRepository; |
4 | 5 | use \Locker\Helpers\Exceptions as Exceptions; |
5 | 6 |
|
@@ -29,7 +30,7 @@ public function where() { |
29 | 30 | * @return Aggregate http://php.net/manual/en/mongocollection.aggregate.php#refsect1-mongocollection.aggregate-examples |
30 | 31 | */ |
31 | 32 | public function aggregate() { |
32 | | - $pipeline = $this->getParam('pipeline'); |
| 33 | + $pipeline = $this->getPipeline(); |
33 | 34 | return \Response::json($this->query->aggregate($this->getOptions(), $pipeline)); |
34 | 35 | } |
35 | 36 |
|
@@ -82,9 +83,43 @@ public function void() { |
82 | 83 | return \Response::json($this->query->void($match, $this->getOptions())); |
83 | 84 | } |
84 | 85 |
|
| 86 | + private function convertDte($value) { |
| 87 | + if(is_array($value)) { |
| 88 | + if(isset($value['$dte'])) { |
| 89 | + $date = $value['$dte']; |
| 90 | + $parsedDate = new Carbon($date); |
| 91 | + if($parsedDate) return new \MongoDate($parsedDate->timestamp, $parsedDate->micro); |
| 92 | + else throw new Exceptions\Exception("`$date` is not a valid date."); |
| 93 | + } |
| 94 | + else |
| 95 | + return array_map([$this, __FUNCTION__], $value); // recursively apply this function to whole pipeline |
| 96 | + } |
| 97 | + |
| 98 | + return $value; |
| 99 | + } |
| 100 | + |
| 101 | + private function convertOid($value) { |
| 102 | + if(is_array($value)) { |
| 103 | + if(isset($value['$oid'])) |
| 104 | + return new \MongoId($value['$oid']); |
| 105 | + else |
| 106 | + return array_map([$this, __FUNCTION__], $value); // recursively apply this function to whole pipeline |
| 107 | + } |
| 108 | + |
| 109 | + return $value; |
| 110 | + } |
| 111 | + |
| 112 | + private function getPipeline() { |
| 113 | + $pipeline = $this->getParam('pipeline'); |
| 114 | + $pipeline = $this->convertDte($pipeline); |
| 115 | + $pipeline = $this->convertOid($pipeline); |
| 116 | + return $pipeline; |
| 117 | + } |
| 118 | + |
85 | 119 | private function getParam($param) { |
86 | 120 | $param_value = \LockerRequest::getParam($param); |
87 | 121 | $value = json_decode($param_value, true); |
| 122 | + |
88 | 123 | if ($value === null && $param_value === null) { |
89 | 124 | throw new Exceptions\Exception("Expected `$param` to be defined as a URL parameter."); |
90 | 125 | } else if ($value === null) { |
|
0 commit comments