generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathIslamicCalendar.php
More file actions
96 lines (77 loc) · 2.83 KB
/
IslamicCalendar.php
File metadata and controls
96 lines (77 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Spatie\Holidays\Calendars;
use Carbon\CarbonImmutable;
use Carbon\CarbonPeriod;
use DateTime;
use DateTimeZone;
use IntlDateFormatter;
use Spatie\Holidays\Countries\Country;
use Spatie\Holidays\Exceptions\InvalidYear;
/** @mixin Country */
trait IslamicCalendar
{
use ResolvesCalendarDates;
protected string $islamicCalendarTimezone = 'UTC';
/** @return array<CarbonPeriod> */
public function eidAlFitr(int $year, int $totalDays = 3): array
{
return $this->getMultiDayHoliday(self::eidAlFitr, $year, $totalDays);
}
/** @return array<CarbonPeriod> */
public function eidAlAdha(int $year, int $totalDays = 4): array
{
return $this->getMultiDayHoliday(self::eidAlAdha, $year, $totalDays);
}
/** @return array<CarbonPeriod> */
protected function ashura(int $year, int $totalDays = 2): array
{
return $this->getMultiDayHoliday(self::ashura, $year, $totalDays);
}
protected function arafat(int $year): CarbonImmutable
{
return $this->getSingleDayHoliday(self::arafat, $year);
}
protected function islamicNewYear(int $year): CarbonImmutable
{
return $this->getSingleDayHoliday(self::islamicNewYear, $year);
}
protected function prophetMuhammadBirthday(int $year): CarbonImmutable
{
return $this->getSingleDayHoliday(self::prophetMuhammadBirthday, $year);
}
public function setIslamicCalendarTimezone(string $islamicCalendarTimezone): static
{
$this->islamicCalendarTimezone = $islamicCalendarTimezone;
return $this;
}
protected function getIslamicCalendarFormatter(): IntlDateFormatter
{
return new IntlDateFormatter(
'en_US@calendar=islamic-civil',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'UTC',
IntlDateFormatter::TRADITIONAL,
'yyyy-MM-dd'
);
}
protected function getHijriYear(int $year, bool $nextYear = false): int
{
$formatter = $this->getIslamicCalendarFormatter();
$formatter->setPattern('yyyy');
$dateTime = DateTime::createFromFormat('d/m/Y', '01/01/'.($nextYear ? $year + 1 : $year));
if (! $dateTime) {
throw InvalidYear::invalidHijriYear();
}
return (int) $formatter->format($dateTime->getTimestamp());
}
protected function islamicToGregorianDate(string $input, int $year, bool $nextYear = false): CarbonImmutable
{
$hijriYear = $this->getHijriYear(year: $year, nextYear: $nextYear);
$formatter = $this->getIslamicCalendarFormatter();
$timestamp = (int) $formatter->parse(sprintf('%s-%s', $hijriYear, $input));
return (new CarbonImmutable)
->setTimeStamp($timestamp)
->setTimezone(new DateTimeZone($this->islamicCalendarTimezone));
}
}