Skip to content

Commit

Permalink
Fixed phpstan errors
Browse files Browse the repository at this point in the history
for #220
  • Loading branch information
sonali-webmavens committed Apr 16, 2024
1 parent 03dcb38 commit 4ad9945
Show file tree
Hide file tree
Showing 71 changed files with 514 additions and 369 deletions.
132 changes: 132 additions & 0 deletions src/Calendars/IndianCalender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace Spatie\Holidays\Calendars;

use Carbon\CarbonImmutable;
use Carbon\CarbonPeriod;
use Carbon\Exceptions\InvalidFormatException;
use Spatie\Holidays\Countries\Country;
use Spatie\Holidays\Exceptions\InvalidYear;

/** @mixin Country */
trait IndianCalender
{
/** @return array<CarbonPeriod> */
protected function ashura(int $year, int $totalDays = 1): array
{
return $this->getMultiDayHoliday(self::ashura, $year, $totalDays);
}

/** @return array<CarbonPeriod> */
protected function miladHolidays(int $year, int $totalDays = 1): array
{
return $this->getMultiDayHoliday(self::miladHolidays, $year, $totalDays);
}

/** @return array<CarbonPeriod> */
protected function bakridHolidays(int $year, int $totalDays = 1): array
{
return $this->getMultiDayHoliday(self::bakridHolidays, $year, $totalDays);
}

/** @return array<CarbonPeriod> */
protected function ramzanIdHolidays(int $year, int $totalDays = 1): array
{
return $this->getMultiDayHoliday(self::ramzanIdHolidays, $year, $totalDays);
}

/** @param array<string> $collection */
protected function getSingleDayHoliday(array $collection, int $year): CarbonImmutable
{
$date = $collection[$year] ?? null;

if ($date === null) {
throw InvalidYear::range($this->countryCode(), 1970, 2037);
}

$date = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")?->startOfDay();

if ($date === null) {
throw new InvalidFormatException('Invalid date for holiday');
}

return $date;
}

/**
* @param array<string|array<string>> $collection
* @return array<CarbonPeriod>
*/
protected function getMultiDayHoliday(array $collection, int $year, int $totalDays): array
{
$date = $collection[$year] ?? null;

if ($date === null) {
throw InvalidYear::range($this->countryCode(), 1970, 2037);
}

$overlap = $this->getOverlapping($collection, $year, $totalDays);

if ($overlap) {
$period = $this->createPeriod($overlap, $year - 1, $totalDays);

$date = [$period, $date];
}

if (! is_array($date)) {
return [$this->createPeriod($date, $year, $totalDays)];
}

// Twice a year
$periods = [];
$dates = $date;

/** @var CarbonPeriod|string $date */
foreach ($dates as $date) {
if ($date instanceof CarbonPeriod) {
$periods[] = $date;

continue;
}

$periods[] = $this->createPeriod($date, $year, $totalDays);
}

return $periods;
}

protected function createPeriod(string $date, int $year, int $totalDays): CarbonPeriod
{
$start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")?->startOfDay();
$end = $start->addDays($totalDays - 1)->startOfDay();

return CarbonPeriod::create($start, '1 day', $end);
}

/** @param array<string|array<string>> $collection */
protected function getOverlapping(array $collection, int $year, int $totalDays): ?string
{
if ($year === 1970) {
return null;
}

$date = $collection[$year - 1] ?? null;

if ($date === null) {
throw InvalidYear::range($this->countryCode(), 1970, 2037);
}

if (is_array($date)) {
$date = end($date);
}

$start = CarbonImmutable::createFromFormat('Y-m-d', "{$year}-{$date}")?->startOfDay();
$end = $start->addDays($totalDays - 1)->startOfDay();

if ($end->year !== $year) {
return (string) $date;
}

return null;
}
}
149 changes: 62 additions & 87 deletions src/Countries/India.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Carbon\CarbonImmutable;
use RuntimeException;
use Spatie\Holidays\Calendars\IndianCalender;

class India extends Country
class India extends Country
{
use IndianCalender;
/**
* No library or built-in php intl functions convert dates properly for all years or all country including
* “geniusts/hijri-dates”. It is most logical to prepare the dates between 1970 and 2037 as a constant property
Expand Down Expand Up @@ -523,7 +525,7 @@ class India extends Country
2037 => '01-27',
];

public const muharramHolidays = [
public const ashura = [
1970 => '03-18',
1971 => '03-08',
1972 => '02-25',
Expand Down Expand Up @@ -1104,96 +1106,69 @@ public function countryCode(): string
}

protected function allHolidays(int $year): array
{
return array_merge([
'Republic Day' => '01-26',
'Independence Day' => '08-15',
'Mahatma Gandhi Jayanti' => '10-02',
'Christmas' => '12-25',
], $this->variableHolidays($year));
}
{
$new_holidays = [
'Holi' => self::holiHolidays[$year],
'Good Friday' => self::goodFridayHolidays[$year],
'Rama Navami' => self::ramanavamiHolidays[$year],
'Mahavir Jayanti' => self::mahavirJayantiHolidays[$year],
'Buddha Purnima/Vesak' => self::buddhaPurnimaHolidays[$year],
'Raksha Bandhan' => self::rakshabandhanHolidays[$year],
'Janmashtami' => self::janmashtamiHolidays[$year],
'Dussehra' => self::dussehraHolidays[$year],
'Diwali/Deepavali' => self::diwaliHolidays[$year],
'Bhai Dooj' => self::bhaiDujHolidays[$year],
'Guru Nanak Jayanti' => self::guruNanakHolidays[$year],
];

/** @return array<string, CarbonImmutable> */
public function variableHolidays(int $year): array
{
$holidays = [
'Holi' => self::holiHolidays[$year],
'Good Friday' => self::goodFridayHolidays[$year],
'Ramzan Id\/Eid-ul-Fitar 1' => self::ramzanIdHolidays[$year],
'Ramzan Id\/Eid-ul-Fitar 2' => '',
'Rama Navami' => self::ramanavamiHolidays[$year],
'Mahavir Jayanti' => self::mahavirJayantiHolidays[$year],
'Buddha Purnima/Vesak' => self::buddhaPurnimaHolidays[$year],
'Bakrid/Eid ul-Adha 1' => self::bakridHolidays[$year],
'Bakrid/Eid ul-Adha 2' => '',
'Muharram/Ashura 1' => self::muharramHolidays[$year],
'Muharram/Ashura 2' => '',
'Raksha Bandhan' => self::rakshabandhanHolidays[$year],
'Janmashtami' => self::janmashtamiHolidays[$year],
'Milad un-Nabi/Id-e-Milad 1' => self::miladHolidays[$year],
'Milad un-Nabi/Id-e-Milad 2' => '',
'Dussehra' => self::dussehraHolidays[$year],
'Diwali/Deepavali' => self::diwaliHolidays[$year],
'Bhai Dooj' => self::bhaiDujHolidays[$year],
'Guru Nanak Jayanti' => self::guruNanakHolidays[$year],
];
return array_merge([
'Republic Day' => '01-26',
'Independence Day' => '08-15',
'Mahatma Gandhi Jayanti' => '10-02',
'Christmas' => '12-25',
],$new_holidays, $this->otherHolidays($year));
}

/** @return array<string, CarbonImmutable> */
public function otherHolidays(int $year): array
{
$ashura = $this->ashura($year);
$miladHolidays = $this->miladHolidays($year);
$bakridHolidays = $this->bakridHolidays($year);
$ramzanIdHolidays = $this->ramzanIdHolidays($year);

$ramzan = self::ramzanIdHolidays[$year];
if (is_array($ramzan)) {
foreach ($ramzan as $key => $date) {
if($key == 0){
$holidays['Ramzan Id\/Eid-ul-Fitar 1'] = $date;
}
else{
$holidays['Ramzan Id\/Eid-ul-Fitar 2'] = $date;
}
}
} else {
unset($holidays['Ramzan Id\/Eid-ul-Fitar 2']);
$holidays = array_merge(
$this->convertPeriods('Milad un-Nabi/Id-e-Milad', $year, $miladHolidays[0], includeEve: false),
$this->convertPeriods('Muharram/Ashura', $year, $ashura[0], includeEve: false),
$this->convertPeriods('Bakrid/Eid ul-Adha', $year, $bakridHolidays[0], includeEve: false),
$this->convertPeriods('Ramzan Id\/Eid-ul-Fitar', $year, $ramzanIdHolidays[0], includeEve: false),
);

if (count($miladHolidays) > 1) {
$holidays = array_merge($holidays,
$this->convertPeriods('2. Milad un-Nabi/Id-e-Milad', $year, $miladHolidays[1], includeEve: false),
);
}

$milad = self::miladHolidays[$year];
if (is_array($milad)) {
foreach ($milad as $key => $date) {
if($key == 0){
$holidays['Milad un-Nabi/Id-e-Milad 1'] = $date;
}
else{
$holidays['Milad un-Nabi/Id-e-Milad 2'] = $date;
}
}
} else {
unset($holidays['Milad un-Nabi/Id-e-Milad 2']);

if (count($ashura) > 1) {
$holidays = array_merge($holidays,
$this->convertPeriods('2. Muharram/Ashura', $year, $ashura[1], includeEve: false),
);
}

$muhrram = self::muharramHolidays[$year];
if (is_array($muhrram)) {
foreach ($muhrram as $key => $date) {
if($key == 0){
$holidays['Muharram/Ashura 1'] = $date;
}
else{
$holidays['Muharram/Ashura 2'] = $date;
}
}
} else {
unset($holidays['Muharram/Ashura 2']);
}

$bakrid = self::bakridHolidays[$year];
if (is_array($bakrid)) {
foreach ($bakrid as $key => $date) {
if($key == 0){
$holidays['Bakrid/Eid ul-Adha 1'] = $date;
}
else{
$holidays['Bakrid/Eid ul-Adha 2'] = $date;
}
}
} else {
unset($holidays['Bakrid/Eid ul-Adha 2']);
}
if (count($bakridHolidays) > 1) {
$holidays = array_merge($holidays,
$this->convertPeriods('2. Bakrid/Eid ul-Adha', $year, $bakridHolidays[1], includeEve: false),
);
}

if (count($ramzanIdHolidays) > 1) {
$holidays = array_merge($holidays,
$this->convertPeriods('2. Ramzan Id\/Eid-ul-Fitar', $year, $ramzanIdHolidays[1], includeEve: false),
);
}

return $holidays;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"date": "1970-01-26"
},
{
"name": "Bakrid\/Eid ul-Adha 1",
"name": "Bakrid\/Eid ul-Adha",
"date": "1970-02-17"
},
{
"name": "Muharram\/Ashura 1",
"name": "Muharram\/Ashura",
"date": "1970-03-18"
},
{
Expand All @@ -28,7 +28,7 @@
"date": "1970-04-19"
},
{
"name": "Milad un-Nabi\/Id-e-Milad 1",
"name": "Milad un-Nabi\/Id-e-Milad",
"date": "1970-05-19"
},
{
Expand Down Expand Up @@ -68,7 +68,7 @@
"date": "1970-11-13"
},
{
"name": "Ramzan Id\\\/Eid-ul-Fitar 1",
"name": "Ramzan Id\\\/Eid-ul-Fitar",
"date": "1970-11-30"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"date": "1971-01-26"
},
{
"name": "Bakrid\/Eid ul-Adha 1",
"name": "Bakrid\/Eid ul-Adha",
"date": "1971-02-06"
},
{
"name": "Muharram\/Ashura 1",
"name": "Muharram\/Ashura",
"date": "1971-03-08"
},
{
Expand All @@ -28,7 +28,7 @@
"date": "1971-04-09"
},
{
"name": "Milad un-Nabi\/Id-e-Milad 1",
"name": "Milad un-Nabi\/Id-e-Milad",
"date": "1971-05-09"
},
{
Expand Down Expand Up @@ -68,7 +68,7 @@
"date": "1971-11-02"
},
{
"name": "Ramzan Id\\\/Eid-ul-Fitar 1",
"name": "Ramzan Id\\\/Eid-ul-Fitar",
"date": "1971-11-20"
},
{
Expand Down
Loading

0 comments on commit 4ad9945

Please sign in to comment.