Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| CanHaveMonthIntervals | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
| createFromDateTimeObject | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| toDate | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| withMonth | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| nextMonth | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| previousMonth | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\DateValueObjects\Concerns; |
| 3 | |
| 4 | use DateTimeImmutable; |
| 5 | use DateTimeInterface; |
| 6 | |
| 7 | /** |
| 8 | * Adds method to add month related methods to date value objects. |
| 9 | */ |
| 10 | trait CanHaveMonthIntervals |
| 11 | { |
| 12 | /** |
| 13 | * @see CanCreateInstanceFromDateTimeObject |
| 14 | */ |
| 15 | abstract public static function createFromDateTimeObject(DateTimeInterface $dateTime): self; |
| 16 | |
| 17 | /** |
| 18 | * @see IsDateValueObject |
| 19 | */ |
| 20 | abstract public function toDate(): DateTimeImmutable; |
| 21 | |
| 22 | /** |
| 23 | * @see IsDateValueObject |
| 24 | */ |
| 25 | private int $day; |
| 26 | |
| 27 | /** |
| 28 | * Creates a new date value object, but with a different month value. |
| 29 | * If the day does not exist in this month, it will switch to the last day in that month. |
| 30 | */ |
| 31 | public function withMonth(int $month): self |
| 32 | { |
| 33 | $date = $this->toDate(); |
| 34 | $object = self::createFromDateTimeObject( |
| 35 | $date->setDate((int) $date->format('Y'), $month, $this->day) |
| 36 | ); |
| 37 | $object->day = $this->day; |
| 38 | return $object; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Creates a new date value object, but with the next month. |
| 43 | * If the day does not exist in this month, it will switch to the last day in that month. |
| 44 | */ |
| 45 | public function nextMonth(): self |
| 46 | { |
| 47 | $currentDate = $this->toDate(); |
| 48 | $currentMonth = (int) $currentDate->format('m'); |
| 49 | $nextDate = $currentDate->setDate((int) $currentDate->format('Y'), $currentMonth + 1, $this->day); |
| 50 | $nextMonth = (int) $nextDate->format('m'); |
| 51 | $expectedMonth = $currentMonth === 12 ? 1 : ($currentMonth + 1); |
| 52 | // this means the current month has more days than the next month |
| 53 | if ($nextMonth !==$expectedMonth) { |
| 54 | $nextDate = $currentDate->setDate((int) $currentDate->format('Y'), $currentMonth + 2, 0); |
| 55 | } |
| 56 | |
| 57 | $object = self::createFromDateTimeObject($nextDate); |
| 58 | $object->day = $this->day; |
| 59 | return $object; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Creates a new date value object, but with the previous month. |
| 64 | * If the day does not exist in this month, it will switch to the last day in that month. |
| 65 | */ |
| 66 | public function previousMonth(): self |
| 67 | { |
| 68 | $currentDate = $this->toDate(); |
| 69 | $currentMonth = (int) $currentDate->format('m'); |
| 70 | $previousDate = $currentDate->setDate((int) $currentDate->format('Y'), $currentMonth - 1, $this->day); |
| 71 | $previousMonth = (int) $previousDate->format('m'); |
| 72 | $expectedMonth = $currentMonth === 1 ? 12 : ($currentMonth - 1); |
| 73 | // this means the current month has more days than the previous month |
| 74 | if ($previousMonth !== $expectedMonth) { |
| 75 | $previousDate = $currentDate->setDate((int) $currentDate->format('Y'), $currentMonth, 0); |
| 76 | } |
| 77 | |
| 78 | $object = self::createFromDateTimeObject($previousDate); |
| 79 | $object->day = $this->day; |
| 80 | return $object; |
| 81 | } |
| 82 | } |