Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
CanHaveYearIntervals
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
3
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
 withYear
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 nextYear
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 previousYear
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
8trait CanHaveYearIntervals
9{
10    /**
11     * @see CanCreateInstanceFromDateTimeObject
12     */
13    abstract public static function createFromDateTimeObject(DateTimeInterface $dateTime): self;
14
15    /**
16     * @see IsDateValueObject
17     */
18    abstract public function toDate(): DateTimeImmutable;
19
20    public function withYear(int $year): self
21    {
22        $date = $this->toDate();
23        return self::createFromDateTimeObject(
24            $date->setDate($year, (int) $date->format('m'), (int) $date->format('d'))
25        );
26    }
27
28    public function nextYear(): self
29    {
30        $date = $this->toDate();
31        return self::createFromDateTimeObject($date->add(new DateInterval('P1Y')));
32    }
33
34    public function previousYear(): self
35    {
36        $date = $this->toDate();
37        return self::createFromDateTimeObject($date->sub(new DateInterval('P1Y')));
38    }
39}