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
Push — master ( 55e631...31e435 )
by Sebastian
02:34
created

OpeningHours::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Spatie\OpeningHours;
4
5
use DateTime;
6
use Spatie\OpeningHours\Exceptions\Exception;
7
use Spatie\OpeningHours\Exceptions\InvalidDate;
8
use Spatie\OpeningHours\Exceptions\InvalidDayName;
9
use Spatie\OpeningHours\Helpers\Arr;
10
11
class OpeningHours
12
{
13
    /** @var \Spatie\OpeningHours\Day[] */
14
    protected $openingHours;
15
16
    /** @var array */
17
    protected $exceptions = [];
18
19
    public function __construct()
20
    {
21
        $this->openingHours = Day::mapDays(function () {
22
            return new OpeningHoursForDay();
23
        });
24
    }
25
26
    /**
27
     * @param array $data
28
     *
29
     * @return static
30
     */
31
    public static function create(array $data)
32
    {
33
        return (new static())->fill($data);
34
    }
35
36
    /**
37
     * @param array $data
38
     *
39
     * @return bool
40
     */
41
    public static function isValid(array $data): bool
42
    {
43
        try {
44
            static::create($data);
45
            return true;
46
        } catch (Exception $exception) {
47
            return false;
48
        }
49
    }
50
51
    public function fill(array $data)
52
    {
53
        list($openingHours, $exceptions) = $this->parseOpeningHoursAndExceptions($data);
54
55
        foreach ($openingHours as $day => $openingHours) {
56
            $this->setOpeningHoursFromStrings($day, $openingHours);
57
        }
58
59
        $this->setExceptionsFromStrings($exceptions);
60
61
        return $this;
62
    }
63
64
    public function forWeek(): array
65
    {
66
        return $this->openingHours;
67
    }
68
69
    public function forDay(string $day): OpeningHoursForDay
70
    {
71
        $day = $this->normalizeDayName($day);
72
73
        return $this->openingHours[$day];
74
    }
75
76
    public function forDate(DateTime $date): OpeningHoursForDay
77
    {
78
        return $this->exceptions[$date->format('Y-m-d')] ?? $this->forDay(Day::onDateTime($date));
79
    }
80
81
    public function exceptions(): array
82
    {
83
        return $this->exceptions;
84
    }
85
86
    public function isOpenOn(string $day): bool
87
    {
88
        return count($this->forDay($day)) > 0;
89
    }
90
91
    public function isClosedOn(string $day): bool
92
    {
93
        return $this->isOpenOn($day);
94
    }
95
96
    public function isOpenAt(DateTime $dateTime): bool
97
    {
98
        $openingHoursForDay = $this->forDate($dateTime);
99
100
        return $openingHoursForDay->isOpenAt(Time::fromDateTime($dateTime));
101
    }
102
103
    public function isClosedAt(DateTime $dateTime): bool
104
    {
105
        return ! $this->isOpenAt($dateTime);
106
    }
107
108
    public function isOpen(): bool
109
    {
110
        return $this->isOpenAt(new DateTime());
111
    }
112
113
    public function isClosed(): bool
114
    {
115
        return $this->isClosedAt(new DateTime());
116
    }
117
118
    protected function parseOpeningHoursAndExceptions(array $data): array
119
    {
120
        $exceptions = Arr::pull($data, 'exceptions', []);
121
        $openingHours = [];
122
123
        foreach ($data as $day => $openingHoursData) {
124
            $openingHours[$this->normalizeDayName($day)] = $openingHoursData;
125
        }
126
127
        return [$openingHours, $exceptions];
128
    }
129
130
    protected function setOpeningHoursFromStrings(string $day, array $openingHours)
131
    {
132
        $day = $this->normalizeDayName($day);
133
134
        $this->openingHours[$day] = OpeningHoursForDay::fromStrings($openingHours);
135
    }
136
137
    protected function setExceptionsFromStrings(array $exceptions)
138
    {
139
        $this->exceptions = Arr::map($exceptions, function (array $openingHours, string $date) {
140
            $dateTime = DateTime::createFromFormat('Y-m-d', $date);
141
142
            if ($dateTime === false || $dateTime->format('Y-m-d') !== $date) {
143
                throw InvalidDate::invalidDate($date);
144
            }
145
146
            return OpeningHoursForDay::fromStrings($openingHours);
147
        });
148
    }
149
150
    protected function normalizeDayName(string $day)
151
    {
152
        $day = strtolower($day);
153
154
        if (! Day::isValid($day)) {
155
            throw new InvalidDayName();
156
        }
157
158
        return $day;
159
    }
160
}
161