Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
10 / 10 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
CanHaveDayIntervals | |
100.00% |
10 / 10 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
createFromDateTimeObject | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
toDate | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
withDay | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
tomorrow | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
yesterday | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Apie\DateValueObjects\Concerns; |
3 | |
4 | use DateInterval; |
5 | use DateTimeImmutable; |
6 | use DateTimeInterface; |
7 | |
8 | /** |
9 | * Adds method to add day related methods to date value objects. |
10 | */ |
11 | trait CanHaveDayIntervals |
12 | { |
13 | /** |
14 | * @see CanCreateInstanceFromDateTimeObject |
15 | */ |
16 | abstract public static function createFromDateTimeObject(DateTimeInterface $dateTime): self; |
17 | |
18 | /** |
19 | * @see IsDateValueObject |
20 | */ |
21 | abstract public function toDate(): DateTimeImmutable; |
22 | |
23 | /** |
24 | * @see IsDateValueObject |
25 | */ |
26 | private int $day; |
27 | |
28 | /** |
29 | * Creates a new date value object, but with a different day. |
30 | */ |
31 | public function withDay(int $day): self |
32 | { |
33 | $date = $this->toDate(); |
34 | $object = self::createFromDateTimeObject( |
35 | $date->setDate((int) $date->format('Y'), (int) $date->format('m'), $day) |
36 | ); |
37 | $object->day = $day; |
38 | return $object; |
39 | } |
40 | |
41 | /** |
42 | * Creates a new date value object with the next day. |
43 | */ |
44 | public function tomorrow(): self |
45 | { |
46 | $date = $this->toDate(); |
47 | return self::createFromDateTimeObject($date->add(new DateInterval('P1D'))); |
48 | } |
49 | |
50 | /** |
51 | * Creates a new date value object with the previous day. |
52 | */ |
53 | public function yesterday(): self |
54 | { |
55 | $date = $this->toDate(); |
56 | return self::createFromDateTimeObject($date->sub(new DateInterval('P1D'))); |
57 | } |
58 | } |