| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Carbon\Traits; |
| 15 |
|
| 16 |
use Carbon\Callback; |
| 17 |
use Carbon\Carbon; |
| 18 |
use Carbon\CarbonImmutable; |
| 19 |
use Carbon\CarbonInterface; |
| 20 |
use Closure; |
| 21 |
use DateTimeImmutable; |
| 22 |
use DateTimeInterface; |
| 23 |
|
| 24 |
trait IntervalStep |
| 25 |
{ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
protected $step; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function getStep(): ?Closure |
| 39 |
{ |
| 40 |
return $this->step; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function setStep(?Closure $step): void |
| 51 |
{ |
| 52 |
$this->step = $step; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function convertDate(DateTimeInterface $dateTime, bool $negated = false): CarbonInterface |
| 66 |
{ |
| 67 |
|
| 68 |
$carbonDate = $dateTime instanceof CarbonInterface ? $dateTime : $this->resolveCarbon($dateTime); |
| 69 |
|
| 70 |
if ($this->step) { |
| 71 |
$carbonDate = Callback::parameter($this->step, $carbonDate->avoidMutation()); |
| 72 |
|
| 73 |
return $carbonDate->modify(($this->step)($carbonDate, $negated)->format('Y-m-d H:i:s.u e O')); |
| 74 |
} |
| 75 |
|
| 76 |
if ($negated) { |
| 77 |
return $carbonDate->rawSub($this); |
| 78 |
} |
| 79 |
|
| 80 |
return $carbonDate->rawAdd($this); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private function resolveCarbon(DateTimeInterface $dateTime): Carbon|CarbonImmutable |
| 87 |
{ |
| 88 |
if ($dateTime instanceof DateTimeImmutable) { |
| 89 |
return CarbonImmutable::instance($dateTime); |
| 90 |
} |
| 91 |
|
| 92 |
return Carbon::instance($dateTime); |
| 93 |
} |
| 94 |
} |