Completed
Pull Request — master (#144)
by Markus
01:01
created

Date::fromDateTimeInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the eluceo/iCal package.
5
 *
6
 * (c) 2019 Markus Poerschke <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Eluceo\iCal\Domain\ValueObject;
13
14
use DateInterval;
15
use DateTimeImmutable as PhpDateTimeImmutable;
16
use DateTimeInterface as PhpDateTimeInterface;
17
use InvalidArgumentException;
18
19
final class Date extends PointInTime
20
{
21
    public static function fromDateTimeInterface(PhpDateTimeInterface $dateTime): self
22
    {
23
        return new static(
24
            PhpDateTimeImmutable::createFromFormat(
25
                PhpDateTimeInterface::ATOM,
26
                $dateTime->format(PhpDateTimeInterface::ATOM), $dateTime->getTimezone()
27
            )
28
        );
29
    }
30
31
    public static function fromCurrentDay(): self
32
    {
33
        return static::fromDateTimeInterface(new PhpDateTimeImmutable());
34
    }
35
36
    public function add(DateInterval $interval): self
37
    {
38
        if (
39
            $interval->h > 0
40
            || $interval->i > 0
41
            || $interval->s > 0
42
            || $interval->f > 0
0 ignored issues
show
Bug introduced by
The property f does not seem to exist in DateInterval.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
43
        ) {
44
            throw new InvalidArgumentException('Cannot add time interval to a date.');
45
        }
46
47
        $new = parent::add($interval);
48
        assert($new instanceof static);
49
50
        return $new;
51
    }
52
}
53