Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
CanHaveTimeIntervals
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
7 / 7
7
100.00% covered (success)
100.00%
1 / 1
 createFromDateTimeObject
n/a
0 / 0
n/a
0 / 0
0
 toDate
n/a
0 / 0
n/a
0 / 0
0
 withTime
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 nextHour
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 previousHour
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 nextMinute
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 previousMinute
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 nextSecond
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 previousSecond
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\DateValueObjects\Concerns;
3
4use DateInterval;
5use DateTimeImmutable;
6use DateTimeInterface;
7
8/**
9 * Adds method to add time related methods to date value objects.
10 */
11trait CanHaveTimeIntervals
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     * Creates a new date value object, but with a different time.
25     */
26    public function withTime(
27        ?int $hour = null,
28        ?int $minute = null,
29        ?int $second = null,
30        ?int $micro = null
31    ): self {
32        $date = $this->toDate();
33        return self::createFromDateTimeObject(
34            $date->setTime(
35                $hour ?? $date->format('H'),
36                $minute ?? $date->format('i'),
37                $second ?? $date->format('s'),
38                $micro ?? $date->format('u')
39            )
40        );
41    }
42
43    /**
44     * Creates a new date value object, but with a hour later.
45     */
46    public function nextHour(): self
47    {
48        $date = $this->toDate();
49        return self::createFromDateTimeObject($date->add(new DateInterval('PT1H')));
50    }
51
52    /**
53     * Creates a new date value object, but with a hour earlier.
54     */
55    public function previousHour(): self
56    {
57        $date = $this->toDate();
58        return self::createFromDateTimeObject($date->sub(new DateInterval('PT1H')));
59    }
60
61    /**
62     * Creates a new date value object, but with a minute later.
63     */
64    public function nextMinute(): self
65    {
66        $date = $this->toDate();
67        return self::createFromDateTimeObject($date->add(new DateInterval('PT1M')));
68    }
69
70    /**
71     * Creates a new date value object, but with a minute earlier.
72     */
73    public function previousMinute(): self
74    {
75        $date = $this->toDate();
76        return self::createFromDateTimeObject($date->sub(new DateInterval('PT1M')));
77    }
78
79    /**
80     * Creates a new date value object, but with a second later.
81     */
82    public function nextSecond(): self
83    {
84        $date = $this->toDate();
85        return self::createFromDateTimeObject($date->add(new DateInterval('PT1S')));
86    }
87
88    /**
89     * Creates a new date value object, but with a second earlier.
90     */
91    public function previousSecond(): self
92    {
93        $date = $this->toDate();
94        return self::createFromDateTimeObject($date->sub(new DateInterval('PT1S')));
95    }
96}