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 | |
| 13 | private function hasAppends() |
| 14 | { |
| 15 | return (count($this->appends) > 0); |
| 16 | } |
| 17 | |
| 18 | private function addAppendsToModel($result) |
| 19 | { |
| 20 | $result->map(function ($item) { |
| 21 | $item->append($this->appends); |
| 22 | |
| 23 | return $item; |
| 24 | }); |
| 25 | |
| 26 | return $result; |
| 27 | } |
| 28 | |
| 29 | public function get() |
| 30 | { |
| 31 | foreach ($this->query->getQuery()->wheres as $key => $param) { |
| 32 | if ($param['type'] == 'In') { |
| 33 | foreach ($param['values'] as $k => $value) { |
| 34 | $this->query->getQuery()->wheres[$key]['values'][$k] = $this->transformValue($value); |
| 35 | } |
| 36 | } else { |
| 37 | $this->query->getQuery()->wheres[$key]['value'] = $this->transformValue($param['value']); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | $result = $this->query->get(); |
| 42 | |
| 43 | if ($this->hasAppends()) { |
| 44 | $result = $this->addAppendsToModel($result); |
| 45 | } |
| 46 | |
| 47 | return $result; |
| 48 | } |
| 49 | |
| 50 | private function transformValue($value) |
| 51 | { |
| 52 | if ($value === 'true') { |
| 53 | $value = true; |
| 54 | } |
| 55 | |
| 56 | if ($value === 'false') { |
| 57 | $value = false; |
| 58 | } |
| 59 | |
| 60 | try { |
| 61 | $obj = new DateTime('@'.$value); |
| 62 | $value = new UTCDateTime($obj->getTimestamp() * 1000); |
| 63 | } catch (Exception $e) { |
| 64 | } |
| 65 | |
| 66 | if (is_numeric($value)) { |
| 67 | if (intval($value)) { |
| 68 | $value = (int) $value; |
| 69 | } |
| 70 | |
| 71 | if (doubleval($value)) { |
| 72 | $value = (float)$value; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return $value; |
| 77 | } |
| 78 | |
| 79 | public function filterBySkip($query, $skip) |
| 80 | { |
| 81 | return $query->skip((int)$skip); |
| 82 | } |
| 83 | |
| 84 | public function withRelation($relations) |
| 85 | { |
| 86 | $this->query->with($relations); |
| 87 | |
| 88 | return $this; |
| 89 | } |
| 90 | |
| 91 | public function withRelationCount($relations) |
| 92 | { |
| 93 | $this->query->withCount($relations); |
| 94 | |
| 95 | return $this; |
| 96 | } |
| 97 | |
| 98 | public function latest() |
| 99 | { |
| 100 | $this->query->orderBy('created_at', 'desc'); |
| 101 | |
| 102 | return $this; |
| 103 | } |
| 104 | } |