Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| RomeoQueryBuilder | |
0.00% |
0 / 35 |
|
0.00% |
0 / 8 |
342 | |
0.00% |
0 / 1 |
| hasAppends | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addAppendsToModel | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| transformValue | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
56 | |||
| filterBySkip | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| withRelation | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| withRelationCount | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| latest | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Helpers\QueryBuilder; |
| 4 | |
| 5 | use DateTime; |
| 6 | use Exception; |
| 7 | use MongoDB\BSON\UTCDateTime; |
| 8 | use Unlu\Laravel\Api\QueryBuilder; |
| 9 | |
| 10 | class RomeoQueryBuilder extends QueryBuilder |
| 11 | { |
| 12 | private function hasAppends() |
| 13 | { |
| 14 | return count($this->appends) > 0; |
| 15 | } |
| 16 | |
| 17 | private function addAppendsToModel($result) |
| 18 | { |
| 19 | $result->map(function ($item) { |
| 20 | $item->append($this->appends); |
| 21 | |
| 22 | return $item; |
| 23 | }); |
| 24 | |
| 25 | return $result; |
| 26 | } |
| 27 | |
| 28 | public function get() |
| 29 | { |
| 30 | foreach ($this->query->getQuery()->wheres as $key => $param) { |
| 31 | if ($param['type'] == 'In') { |
| 32 | foreach ($param['values'] as $k => $value) { |
| 33 | $this->query->getQuery()->wheres[$key]['values'][$k] = $this->transformValue($value); |
| 34 | } |
| 35 | } else { |
| 36 | $this->query->getQuery()->wheres[$key]['value'] = $this->transformValue($param['value']); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | $result = $this->query->get(); |
| 41 | |
| 42 | if ($this->hasAppends()) { |
| 43 | $result = $this->addAppendsToModel($result); |
| 44 | } |
| 45 | |
| 46 | return $result; |
| 47 | } |
| 48 | |
| 49 | private function transformValue($value) |
| 50 | { |
| 51 | if ($value === 'true') { |
| 52 | $value = true; |
| 53 | } |
| 54 | |
| 55 | if ($value === 'false') { |
| 56 | $value = false; |
| 57 | } |
| 58 | |
| 59 | try { |
| 60 | $obj = new DateTime('@'.$value); |
| 61 | $value = new UTCDateTime($obj->getTimestamp() * 1000); |
| 62 | } catch (Exception $e) { |
| 63 | } |
| 64 | |
| 65 | if (is_numeric($value)) { |
| 66 | if (intval($value)) { |
| 67 | $value = (int) $value; |
| 68 | } |
| 69 | |
| 70 | if (floatval($value)) { |
| 71 | $value = (float) $value; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return $value; |
| 76 | } |
| 77 | |
| 78 | public function filterBySkip($query, $skip) |
| 79 | { |
| 80 | return $query->skip((int) $skip); |
| 81 | } |
| 82 | |
| 83 | public function withRelation($relations) |
| 84 | { |
| 85 | $this->query->with($relations); |
| 86 | |
| 87 | return $this; |
| 88 | } |
| 89 | |
| 90 | public function withRelationCount($relations) |
| 91 | { |
| 92 | $this->query->withCount($relations); |
| 93 | |
| 94 | return $this; |
| 95 | } |
| 96 | |
| 97 | public function latest() |
| 98 | { |
| 99 | $this->query->orderBy('created_at', 'desc'); |
| 100 | |
| 101 | return $this; |
| 102 | } |
| 103 | } |