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 (#129)
by Kyle
01:11
created

OpeningHoursForDay::getHoursFromRange()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
nc 1
nop 1
1
<?php
2
3
namespace Spatie\OpeningHours;
4
5
use Countable;
6
use ArrayAccess;
7
use ArrayIterator;
8
use IteratorAggregate;
9
use Spatie\OpeningHours\Helpers\Arr;
10
use Spatie\OpeningHours\Helpers\DataTrait;
11
use Spatie\OpeningHours\Exceptions\NonMutableOffsets;
12
use Spatie\OpeningHours\Exceptions\OverlappingTimeRanges;
13
14
class OpeningHoursForDay implements ArrayAccess, Countable, IteratorAggregate
15
{
16
    use DataTrait;
17
18
    /** @var \Spatie\OpeningHours\TimeRange[] */
19
    protected $openingHours = [];
20
21
    public static function fromStrings(array $strings)
22
    {
23
        if (isset($strings['hours'])) {
24
            return static::fromStrings($strings['hours'])->setData($strings['data'] ?? null);
25
        }
26
27
        $openingHoursForDay = new static();
28
29
        if (isset($strings['data'])) {
30
            $openingHoursForDay->setData($strings['data'] ?? null);
31
            unset($strings['data']);
32
        }
33
34
        uasort($strings, function ($a, $b) {
35
            return strcmp(static::getHoursFromRange($a), static::getHoursFromRange($b));
36
        });
37
38
        $timeRanges = Arr::map($strings, function ($string) {
39
            return TimeRange::fromDefinition($string);
40
        });
41
42
        $openingHoursForDay->guardAgainstTimeRangeOverlaps($timeRanges);
43
44
        $openingHoursForDay->openingHours = $timeRanges;
45
46
        return $openingHoursForDay;
47
    }
48
49
    public function isOpenAt(Time $time)
50
    {
51
        foreach ($this->openingHours as $timeRange) {
52
            if ($timeRange->containsTime($time)) {
53
                return true;
54
            }
55
        }
56
57
        return false;
58
    }
59
60
    public function isOpenAtNight(Time $time)
61
    {
62
        foreach ($this->openingHours as $timeRange) {
63
            if ($timeRange->overflowsNextDay() && TimeRange::fromMidnight($timeRange->end())->containsTime($time)) {
64
                return true;
65
            }
66
        }
67
68
        return false;
69
    }
70
71
    /**
72
     * @param callable[] $filters
73
     *
74
     * @return Time|bool
75
     */
76
    public function openingHoursFilter(array $filters)
77
    {
78
        foreach ($this->openingHours as $timeRange) {
79
            foreach ($filters as $filter) {
80
                if ($result = $filter($timeRange)) {
81
                    reset($timeRange);
82
83
                    return $result;
84
                }
85
            }
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * @param Time $time
93
     *
94
     * @return bool|Time
95
     */
96
    public function nextOpen(Time $time)
97
    {
98
        return $this->openingHoursFilter([
99
            function ($timeRange) use ($time) {
100
                return $this->findNextOpenInFreeTime($time, $timeRange);
101
            },
102
        ]);
103
    }
104
105
    /**
106
     * @param Time $time
107
     *
108
     * @return bool|Time
109
     */
110
    public function nextClose(Time $time)
111
    {
112
        return $this->openingHoursFilter([
113
            function ($timeRange) use ($time) {
114
                return $this->findNextCloseInWorkingHours($time, $timeRange);
115
            },
116
            function ($timeRange) use ($time) {
117
                return $this->findNextCloseInFreeTime($time, $timeRange);
118
            },
119
        ]);
120
    }
121
122
    protected function findNextOpenInFreeTime(Time $time, TimeRange $timeRange)
123
    {
124
        if (TimeRange::fromString('00:00-'.$timeRange->start())->containsTime($time)) {
125
            return $timeRange->start();
126
        }
127
    }
128
129
    protected function findNextCloseInWorkingHours(Time $time, TimeRange $timeRange)
130
    {
131
        if ($timeRange->containsTime($time)) {
132
            return next($timeRange);
133
        }
134
    }
135
136
    protected function findNextCloseInFreeTime(Time $time, TimeRange $timeRange)
137
    {
138
        if (TimeRange::fromString('00:00-'.$timeRange->start())->containsTime($time)) {
139
            return $timeRange->end();
140
        }
141
    }
142
143
    protected static function getHoursFromRange($range)
144
    {
145
        return strval((is_array($range)
146
            ? ($range['hours'] ?? array_values($range)[0] ?? null)
147
            : null
148
        ) ?: $range);
149
    }
150
151
    public function offsetExists($offset): bool
152
    {
153
        return isset($this->openingHours[$offset]);
154
    }
155
156
    public function offsetGet($offset)
157
    {
158
        return $this->openingHours[$offset];
159
    }
160
161
    public function offsetSet($offset, $value)
162
    {
163
        throw NonMutableOffsets::forClass(static::class);
164
    }
165
166
    public function offsetUnset($offset)
167
    {
168
        unset($this->openingHours[$offset]);
169
    }
170
171
    public function count(): int
172
    {
173
        return count($this->openingHours);
174
    }
175
176
    public function getIterator()
177
    {
178
        return new ArrayIterator($this->openingHours);
179
    }
180
181
    public function isEmpty(): bool
182
    {
183
        return empty($this->openingHours);
184
    }
185
186
    public function map(callable $callback): array
187
    {
188
        return Arr::map($this->openingHours, $callback);
189
    }
190
191
    protected function guardAgainstTimeRangeOverlaps(array $openingHours)
192
    {
193
        foreach (Arr::createUniquePairs($openingHours) as $timeRanges) {
194
            if ($timeRanges[0]->overlaps($timeRanges[1])) {
195
                throw OverlappingTimeRanges::forRanges($timeRanges[0], $timeRanges[1]);
196
            }
197
        }
198
    }
199
200
    public function __toString()
201
    {
202
        $values = [];
203
        foreach ($this->openingHours as $openingHour) {
204
            $values[] = (string) $openingHour;
205
        }
206
207
        return implode(',', $values);
208
    }
209
}
210