diff --git a/src/Traits/EnglishDateTrait.php b/src/Traits/EnglishDateTrait.php index 465ed92..82c505f 100644 --- a/src/Traits/EnglishDateTrait.php +++ b/src/Traits/EnglishDateTrait.php @@ -61,11 +61,16 @@ trait EnglishDateTrait 9 => '9', ]; - public function toEnglishDate(): string + + public function toEnglishDate(?string $format = NULL, ?string $locale = 'en'): string { + if ($format) { + return $this->toFormattedEnglishDate($format, $locale); + } + $checkIfIsInRange = $this->isInNepaliDateRange($this->date); - if (! $checkIfIsInRange) { + if (!$checkIfIsInRange) { throw new RuntimeException($checkIfIsInRange); } @@ -73,9 +78,10 @@ public function toEnglishDate(): string $this->performCalculationBasedonNepaliDays($totalNepaliDays); - return $this->englishYear.'-'.$this->englishMonth.'-'.$this->englishDay; + return $this->englishYear . '-' . $this->englishMonth . '-' . $this->englishDay; } + public function toEnglishDateArray(): NepaliDateArrayData { $this->toEnglishDate(); @@ -94,6 +100,7 @@ public function toEnglishDateArray(): NepaliDateArrayData ]); } + public function isInNepaliDateRange(Carbon $date): string|bool { if ($date->year < 2000 || $date->year > 2089) { @@ -111,6 +118,7 @@ public function isInNepaliDateRange(Carbon $date): string|bool return true; } + public function calculateTotalNepaliDays() { $totalNepaliDays = 0; @@ -134,7 +142,8 @@ public function calculateTotalNepaliDays() return $totalNepaliDays; } - public function performCalculationBasedOnNepaliDays(string|int $totalNepaliDays) + + public function performCalculationBasedOnNepaliDays(string|int $totalNepaliDays): void { $_day = 4 - 1; @@ -168,30 +177,19 @@ public function performCalculationBasedOnNepaliDays(string|int $totalNepaliDays) } $this->englishYear = $_year; - $this->englishMonth = $_month > 9 ? $_month : '0'.$_month; - $this->englishDay = $totalEnglishDays > 9 ? $totalEnglishDays : '0'.$totalEnglishDays; + $this->englishMonth = $_month > 9 ? $_month : '0' . $_month; + $this->englishDay = $totalEnglishDays > 9 ? $totalEnglishDays : '0' . $totalEnglishDays; $this->dayOfWeek = $_day; } + public function toFormattedEnglishDate( string $format = 'd F Y, l', string $locale = 'en' - ): string { + ): string + { $englishDateArray = $this->toEnglishDateArray(); - $formattedArray = ($locale === 'en') - ? $this->getEnglishLocaleFormattingCharacters($englishDateArray) - : $this->getNepaliLocaleFormattingCharacters($englishDateArray); - - return match ($format) { - 'd F Y, l' => "{$formattedArray['d']} {$formattedArray['F']} {$formattedArray['Y']}, {$formattedArray['l']}", - 'l, d F Y' => "{$formattedArray['l']}, {$formattedArray['d']} {$formattedArray['F']} {$formattedArray['Y']}", - 'd F Y' => "{$formattedArray['d']} {$formattedArray['F']} {$formattedArray['Y']}", - 'd-m-Y' => "{$formattedArray['d']}-{$formattedArray['m']}-{$formattedArray['Y']}", - 'Y-m-d' => "{$formattedArray['Y']}-{$formattedArray['m']}-{$formattedArray['d']}", - 'd/m/Y' => "{$formattedArray['d']}/{$formattedArray['m']}/{$formattedArray['Y']}", - 'Y/m/d' => "{$formattedArray['Y']}/{$formattedArray['m']}/{$formattedArray['d']}", - default => $this->invalidDateFormatException(), - }; + return $this->formatDateString($format, $locale, $englishDateArray); } } diff --git a/src/Traits/HelperTrait.php b/src/Traits/HelperTrait.php index 6e03f83..fe9a96e 100644 --- a/src/Traits/HelperTrait.php +++ b/src/Traits/HelperTrait.php @@ -2,6 +2,9 @@ namespace Anuzpandey\LaravelNepaliDate\Traits; +use Anuzpandey\LaravelNepaliDate\DataTransferObject\NepaliDateArrayData; +use Illuminate\Support\Str; + trait HelperTrait { public function convertEnToNpNumber($number): array|string @@ -35,6 +38,7 @@ public function convertEnToNpNumber($number): array|string return str_replace($en_number, $np_number, $number); } + public function convertEnToNpWord($word): array|string { $en_word = [ @@ -78,8 +82,99 @@ public function convertEnToNpWord($word): array|string return str_replace($en_word, $np_word, $word); } - private function invalidDateFormatException(): void + + public function getFormattedString(string $format, array $formatData, string $locale): string + { + $formattedString = ''; + + for ($i = 0, $iMax = strlen($format); $i < $iMax; $i++) { + $char = $format[$i]; + + if (array_key_exists($char, $formatData)) { + if ($locale === 'np') { + if ($char === 'S') continue; + $formattedString .= $formatData[$char]; + } else { + if ($char === 'S') { + $formattedString .= $this->getOrdinalSuffix((int) $formatData['j']); + } else { + $formattedString .= $formatData[$char]; + } + } + } else { + $formattedString .= $char; + } + } + return $formattedString; + } + + + public function getOrdinalSuffix(int $number): string + { + if ($number % 100 >= 11 && $number % 100 <= 13) { + return 'th'; + } + + return match ($number % 10) { + 1 => 'st', + 2 => 'nd', + 3 => 'rd', + default => 'th', + }; + } + + + private function getNepaliLocaleFormattingCharacters(NepaliDateArrayData $nepaliDateArray): array { - throw new \RuntimeException('Invalid date format provided. Valid formats are: "d F Y, l" - "l, d F Y" - "d F Y" - "d-m-Y" - "Y-m-d" - "d/m/Y" - "Y/m/d"'); + return [ + 'Y' => $nepaliDateArray->npYear, + 'y' => Str::substr($nepaliDateArray->npYear, 2, 2), + 'F' => $nepaliDateArray->npMonthName, + 'm' => $nepaliDateArray->npMonth, + 'n' => $nepaliDateArray->npMonth > 9 ? $nepaliDateArray->npMonth : Str::substr($nepaliDateArray->npMonth, 1, 1), + 'd' => $nepaliDateArray->npDay, + 'j' => $nepaliDateArray->npDay > 9 ? $nepaliDateArray->npDay : Str::substr($nepaliDateArray->npDay, 1, 1), + 'l' => $nepaliDateArray->npDayName, + 'D' => $this->getShortDayName($nepaliDateArray->npDayName), + ]; + } + + + private function getEnglishLocaleFormattingCharacters(NepaliDateArrayData $nepaliDateArray): array + { + return [ + 'Y' => $nepaliDateArray->year, + 'y' => Str::substr($nepaliDateArray->year, 2, 2), + 'F' => $nepaliDateArray->monthName, + 'm' => $nepaliDateArray->month, + 'n' => $nepaliDateArray->month > 9 ? $nepaliDateArray->month : Str::substr($nepaliDateArray->month, 1, 1), + 'd' => $nepaliDateArray->day, + 'j' => $nepaliDateArray->day > 9 ? $nepaliDateArray->day : Str::substr($nepaliDateArray->day, 1, 1), + 'l' => $nepaliDateArray->dayName, + 'D' => $this->getShortDayName($nepaliDateArray->dayName, 'en'), + ]; + } + + + private function formatDateString(string $format, string $locale, $dateArray): string + { + $formattedArray = ($locale === 'en') + ? $this->getEnglishLocaleFormattingCharacters($dateArray) + : $this->getNepaliLocaleFormattingCharacters($dateArray); + + $formatData = [ + 'Y' => $formattedArray['Y'], + 'y' => $formattedArray['y'], + 'F' => $formattedArray['F'], + 'm' => $formattedArray['m'], + 'n' => $formattedArray['n'], + 'd' => $formattedArray['d'], + 'j' => $formattedArray['j'], + 'l' => $formattedArray['l'], + 'D' => $formattedArray['D'], + 'S' => $formattedArray['j'], + ]; + + return $this->getFormattedString($format, $formatData, $locale); } } diff --git a/src/Traits/NepaliDateTrait.php b/src/Traits/NepaliDateTrait.php index ef60d24..54dfde3 100644 --- a/src/Traits/NepaliDateTrait.php +++ b/src/Traits/NepaliDateTrait.php @@ -94,23 +94,7 @@ public function toFormattedNepaliDate( { $nepaliDateArray = $this->toNepaliDateArray(); - $formattedArray = (Str::lower($locale) === 'np') - ? $this->getNepaliLocaleFormattingCharacters($nepaliDateArray) - : $this->getEnglishLocaleFormattingCharacters($nepaliDateArray); - - $formatData = [ - 'Y' => $formattedArray['Y'], - 'y' => $formattedArray['y'], - 'F' => $formattedArray['F'], - 'm' => $formattedArray['m'], - 'n' => $formattedArray['n'], - 'd' => $formattedArray['d'], - 'j' => $formattedArray['j'], - 'l' => $formattedArray['l'], - 'D' => $formattedArray['D'], - ]; - - return $this->formatDateString($format, $formatData); + return $this->formatDateString($format, $locale, $nepaliDateArray); } @@ -136,6 +120,32 @@ public function toNepaliDateArray(): NepaliDateArrayData } + public function getShortDayName(string $npDayName, string $locale = 'np'): string + { + if ($locale === 'en') { + return match ($npDayName) { + 'Sunday' => 'Sun', + 'Monday' => 'Mon', + 'Tuesday' => 'Tue', + 'Wednesday' => 'Wed', + 'Thursday' => 'Thu', + 'Friday' => 'Fri', + 'Saturday' => 'Sat', + }; + } + + return match ($npDayName) { + 'आइतबार' => 'आइत', + 'सोमबार' => 'सोम', + 'मङ्गलबार' => 'मङ्गल', + 'बुधबार' => 'बुध', + 'बिहिबार' => 'बिहि', + 'शुक्रबार' => 'शुक्र', + 'शनिबार' => 'शनि', + }; + } + + private function calculateTotalEnglishDays($year, $month, $day) { $totalEnglishDays = 0; @@ -220,38 +230,6 @@ private function formattedNepaliNumber($value): string } - private function getNepaliLocaleFormattingCharacters(NepaliDateArrayData $nepaliDateArray): array - { - return [ - 'Y' => $nepaliDateArray->npYear, - 'y' => Str::substr($nepaliDateArray->npYear, 2, 2), - 'F' => $nepaliDateArray->npMonthName, - 'm' => $nepaliDateArray->npMonth, - 'n' => $nepaliDateArray->npMonth > 9 ? $nepaliDateArray->npMonth : Str::substr($nepaliDateArray->npMonth, 1, 1), - 'd' => $nepaliDateArray->npDay, - 'j' => $nepaliDateArray->npDay > 9 ? $nepaliDateArray->npDay : Str::substr($nepaliDateArray->npDay, 1, 1), - 'l' => $nepaliDateArray->npDayName, - 'D' => $this->getShortDayName($nepaliDateArray->npDayName), - ]; - } - - - private function getEnglishLocaleFormattingCharacters(NepaliDateArrayData $nepaliDateArray): array - { - return [ - 'Y' => $nepaliDateArray->year, - 'y' => Str::substr($nepaliDateArray->year, 2, 2), - 'F' => $nepaliDateArray->monthName, - 'm' => $nepaliDateArray->month, - 'n' => $nepaliDateArray->month > 9 ? $nepaliDateArray->month : Str::substr($nepaliDateArray->month, 1, 1), - 'd' => $nepaliDateArray->day, - 'j' => $nepaliDateArray->day > 9 ? $nepaliDateArray->day : Str::substr($nepaliDateArray->day, 1, 1), - 'l' => $nepaliDateArray->dayName, - 'D' => $this->getShortDayName($nepaliDateArray->dayName, 'en'), - ]; - } - - private function isInEnglishDateRange(Carbon $date): string|bool { if ($date->year < 1944 || $date->year > 2033) { @@ -269,51 +247,4 @@ private function isInEnglishDateRange(Carbon $date): string|bool return true; } - - private function formatDateString(string $format, $datePartials): string - { - $formattedString = ''; - - // Loop through each format character - for ($i = 0, $iMax = strlen($format); $i < $iMax; $i++) { - $char = $format[$i]; - - // Check if the character is a valid format character - if (array_key_exists($char, $datePartials)) { - // Append the formatted value to the result string - $formattedString .= $datePartials[$char]; - } else { - // If it's not a valid format character, append it as is - $formattedString .= $char; - } - } - - return $formattedString; - } - - public function getShortDayName(string $npDayName, string $locale = 'np'): string - { - if ($locale === 'en') { - return match ($npDayName) { - 'Sunday' => 'Sun', - 'Monday' => 'Mon', - 'Tuesday' => 'Tue', - 'Wednesday' => 'Wed', - 'Thursday' => 'Thu', - 'Friday' => 'Fri', - 'Saturday' => 'Sat', - }; - } - - return match ($npDayName) { - 'आइतबार' => 'आइत', - 'सोमबार' => 'सोम', - 'मङ्गलबार' => 'मङ्गल', - 'बुधबार' => 'बुध', - 'बिहिबार' => 'बिहि', - 'शुक्रबार' => 'शुक्र', - 'शनिबार' => 'शनि', - }; - } - }