Completed
Pull Request — master (#296)
by Luc
05:01
created

OpeningTime::fromNativeString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Calendar;
4
5
use ValueObjects\DateTime\Hour;
6
use ValueObjects\DateTime\Minute;
7
8
class OpeningTime
9
{
10
    /**
11
     * @var Hour
12
     */
13
    private $hour;
14
15
    /**
16
     * @var Minute
17
     */
18
    private $minute;
19
20
    /**
21
     * Custom value object for opening times without seconds.
22
     *
23
     * @param Hour $hour
24
     * @param Minute $minute
25
     */
26
    public function __construct(Hour $hour, Minute $minute)
27
    {
28
        $this->hour = $hour;
29
        $this->minute = $minute;
30
    }
31
32
    /**
33
     * @param \DateTimeInterface $dateTime
34
     * @return OpeningTime
35
     */
36
    public static function fromNativeDateTime(\DateTimeInterface $dateTime)
37
    {
38
        $hour = new Hour(\intval($dateTime->format('H')));
39
        $minute = new Minute(\intval($dateTime->format('i')));
40
41
        return new OpeningTime($hour, $minute);
42
    }
43
44
    /**
45
     * The supported string format is H:i
46
     *
47
     * @param string $time
48
     * @return OpeningTime
49
     */
50
    public static function fromNativeString($time)
51
    {
52
        return self::fromNativeDateTime(
53
            \DateTime::createFromFormat('H:i', $time)
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFormat('H:i', $time) targeting DateTime::createFromFormat() can also be of type false; however, CultuurNet\UDB3\Calendar...e::fromNativeDateTime() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?
Loading history...
54
        );
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function toNativeString()
61
    {
62
        return (string) $this;
63
    }
64
65
    /**
66
     * @return Hour
67
     */
68
    public function getHour()
69
    {
70
        return $this->hour;
71
    }
72
73
    /**
74
     * @return Minute
75
     */
76
    public function getMinute()
77
    {
78
        return $this->minute;
79
    }
80
81
    /**
82
     * @param OpeningTime $time
83
     * @return bool
84
     */
85
    public function sameValueAs(OpeningTime $time)
86
    {
87
        return $this->getHour()->sameValueAs($time->getHour()) &&
88
            $this->getMinute()->sameValueAs($time->getMinute());
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    function __toString()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
95
    {
96
        return $this->toNativeDateTime()->format('H:i');
97
    }
98
99
    /**
100
     * @return \DateTimeInterface
101
     */
102
    private function toNativeDateTime()
103
    {
104
        $hour   = $this->getHour()->toNative();
105
        $minute = $this->getMinute()->toNative();
106
107
        $time = new \DateTime('now');
108
        $time->setTime($hour, $minute);
109
110
        return $time;
111
    }
112
}
113