Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
27 / 27 |
| Date | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
11 | |
100.00% |
27 / 27 |
| handle | |
100.00% |
1 / 1 |
3 | |
100.00% |
12 / 12 |
|||
| carbonBuilder | |
100.00% |
1 / 1 |
3 | |
100.00% |
11 / 11 |
|||
| setCarbonTimeOfTheDay | |
100.00% |
1 / 1 |
3 | |
100.00% |
3 / 3 |
|||
| unpluralize | |
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\MongoJsExecutor\Replacers; |
| 4 | |
| 5 | use Carbon\Carbon; |
| 6 | use Qmp\Laravel\MongoJsExecutor\Helpers\DateFormatter; |
| 7 | |
| 8 | class Date extends MongoJsParserReplacer |
| 9 | { |
| 10 | /** |
| 11 | * Undocumented variable |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | protected $search = ["/new Date\(.*?\)/", "/[\"']?@DATE[-A-Z0-9\|]*[\"']?/"]; |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * Undocumented variable |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $matchPattern = "/\((?:'|\")(.*)(?:'|\")\)/"; |
| 24 | |
| 25 | /** |
| 26 | * Undocumented variable |
| 27 | * |
| 28 | * @var array |
| 29 | */ |
| 30 | protected $carbonMethods = ['startof' => 'startOfDay', 'endof' => 'endOfDay']; |
| 31 | |
| 32 | /** |
| 33 | * Undocumented function |
| 34 | * |
| 35 | * @return string |
| 36 | */ |
| 37 | public function handle($json) |
| 38 | { |
| 39 | return $this->prc($this->search, function ($date) { |
| 40 | $tempdate = str_replace('"', '', $date[0]); |
| 41 | |
| 42 | if (substr($tempdate, 0, 5) === '@DATE') { |
| 43 | $type = explode('-', $tempdate); |
| 44 | array_shift($type); |
| 45 | $type = implode('-', $type); |
| 46 | $carbon = $this->carbonBuilder(strtolower($type)); |
| 47 | |
| 48 | } else { |
| 49 | $dateValue = []; |
| 50 | preg_match($this->matchPattern, $date[0], $dateValue); |
| 51 | $carbon = count($dateValue) ? new Carbon($dateValue[1]) : DateFormatter::carbonDate('now'); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | return DateFormatter::toJson($carbon); |
| 58 | }, $json); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Undocumented function |
| 63 | * |
| 64 | * @param string $type |
| 65 | * @return Carbon |
| 66 | */ |
| 67 | protected function carbonBuilder($type) |
| 68 | { |
| 69 | $query = explode('-', $type); |
| 70 | $startEnd = null; |
| 71 | |
| 72 | if (in_array(strtolower($query[0]), array_keys($this->carbonMethods))) { |
| 73 | $startEnd = $this->carbonMethods[strtolower($query[0])]; |
| 74 | array_shift($query); |
| 75 | } |
| 76 | |
| 77 | $carbon = DateFormatter::carbonDate($query[0]); |
| 78 | |
| 79 | if (count($query) < 2) { |
| 80 | return $this->setCarbonTimeOfTheDay($carbon, $startEnd); |
| 81 | } |
| 82 | |
| 83 | $method = $query[1] . ucfirst($this->unpluralize($query[3])); |
| 84 | $carbon = $carbon->$method($query[2]); |
| 85 | |
| 86 | return $this->setCarbonTimeOfTheDay($carbon, $startEnd); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Undocumented function |
| 91 | * |
| 92 | * @param Carbon $carbon |
| 93 | * @param String|null $method |
| 94 | * @return Carbon |
| 95 | */ |
| 96 | protected function setCarbonTimeOfTheDay($carbon, $method) |
| 97 | { |
| 98 | if ($method && in_array($method, array_values($this->carbonMethods))) { |
| 99 | return $carbon->$method(); |
| 100 | } |
| 101 | |
| 102 | return $carbon; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Undocumented function |
| 107 | * |
| 108 | * @param string $str |
| 109 | * @return string |
| 110 | */ |
| 111 | protected function unpluralize($str) |
| 112 | { |
| 113 | return substr($str, -1, 1) === 's' ? substr($str, 0, -1) : $str; |
| 114 | } |
| 115 | } |