GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#66)
by Nikita
02:03 queued 52s
created

Time::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\OpeningHours;
4
5
use DateTime;
6
use DateTimeInterface;
7
use Spatie\OpeningHours\Exceptions\InvalidTimeString;
8
9
class Time
10
{
11
    /** @var int */
12
    protected $hours;
13
14
    /** @var int */
15
    protected $minutes;
16
17
    protected function __construct(int $hours, int $minutes)
18
    {
19
        $this->hours = $hours;
20
        $this->minutes = $minutes;
21
    }
22
23
    public static function fromString(string $string): self
24
    {
25
        if (! preg_match('/^([0-1][0-9])|(2[0-4]):[0-5][0-9]$/', $string)) {
26
            throw InvalidTimeString::forString($string);
27
        }
28
29
        list($hours, $minutes) = explode(':', $string);
30
31
        return new self($hours, $minutes);
32
    }
33
34
    public function hours(): int
35
    {
36
        return $this->hours;
37
    }
38
39
    public function minutes(): int
40
    {
41
        return $this->minutes;
42
    }
43
44
    public static function fromDateTime(DateTimeInterface $dateTime): self
45
    {
46
        return self::fromString($dateTime->format('H:i'));
47
    }
48
49
    public function isSame(self $time): bool
50
    {
51
        return (string) $this === (string) $time;
52
    }
53
54
    public function isAfter(self $time): bool
55
    {
56
        if ($this->isSame($time)) {
57
            return false;
58
        }
59
60
        if ($this->hours > $time->hours) {
61
            return true;
62
        }
63
64
        return $this->hours === $time->hours && $this->minutes >= $time->minutes;
65
    }
66
67
    public function isBefore(self $time): bool
68
    {
69
        if ($this->isSame($time)) {
70
            return false;
71
        }
72
73
        return ! $this->isAfter($time);
74
    }
75
76
    public function isSameOrAfter(self $time): bool
77
    {
78
        return $this->isSame($time) || $this->isAfter($time);
79
    }
80
81
    public function diffInMinutes(self $time): int
82
    {
83
        return ($time->hours() * 60 + $time->minutes()) - ($this->hours() * 60 + $this->minutes());
84
    }
85
86
    public function toDateTime(DateTime $date = null): DateTime
87
    {
88
        return ($date ?? new DateTime('1970-01-01 00:00:00'))->setTime($this->hours, $this->minutes);
89
    }
90
91
    public function format(string $format = 'H:i'): string
92
    {
93
        return $this->toDateTime()->format($format);
94
    }
95
96
    public function __toString(): string
97
    {
98
        return $this->format();
99
    }
100
}
101