From 9deb4fc47184169d150856d2a20fe332f31b8e1a Mon Sep 17 00:00:00 2001 From: AnuzPandey Date: Sat, 18 Nov 2023 23:59:00 +0545 Subject: [PATCH 1/5] :zap: features: add format and locale option to english date method. - Refactor to DRY code. --- src/Traits/EnglishDateTrait.php | 40 +++++------ src/Traits/HelperTrait.php | 73 ++++++++++++++++++- src/Traits/NepaliDateTrait.php | 123 +++++++------------------------- 3 files changed, 117 insertions(+), 119 deletions(-) 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..ef780e9 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,73 @@ public function convertEnToNpWord($word): array|string return str_replace($en_word, $np_word, $word); } - private function invalidDateFormatException(): void + + 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 { - 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->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'], + ]; + + $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, $formatData)) { + // Append the formatted value to the result string + $formattedString .= $formatData[$char]; + } else { + // If it's not a valid format character, append it as is + $formattedString .= $char; + } + } + + return $formattedString; } } 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) { - 'आइतबार' => 'आइत', - 'सोमबार' => 'सोम', - 'मङ्गलबार' => 'मङ्गल', - 'बुधबार' => 'बुध', - 'बिहिबार' => 'बिहि', - 'शुक्रबार' => 'शुक्र', - 'शनिबार' => 'शनि', - }; - } - } From 9122bf03c313a66a362425b5f7f8cf9fd85418fe Mon Sep 17 00:00:00 2001 From: AnuzPandey Date: Sun, 19 Nov 2023 00:09:56 +0545 Subject: [PATCH 2/5] :zap: features: add ordinalSuffix to English Date Value. --- src/Traits/HelperTrait.php | 56 ++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/src/Traits/HelperTrait.php b/src/Traits/HelperTrait.php index ef780e9..b257e51 100644 --- a/src/Traits/HelperTrait.php +++ b/src/Traits/HelperTrait.php @@ -83,6 +83,44 @@ public function convertEnToNpWord($word): array|string } + 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') { + $formattedString .= $formatData[$char]; + } else { + $formattedString .= $char === 'S' + ? $this->getOrdinalSuffix((int) $formatData['j']) + : $formatData[$char]; + } + } else { + $formattedString .= $char; + } + } + return $formattedString; + } + + + public function getOrdinalSuffix(int $number): string + { + if ($number % 100 >= 11 && $number % 100 <= 13) { + return $number . 'th'; + } + + return match ($number % 10) { + 1 => $number . 'st', + 2 => $number . 'nd', + 3 => $number . 'rd', + default => $number . 'th', + }; + } + + private function getNepaliLocaleFormattingCharacters(NepaliDateArrayData $nepaliDateArray): array { return [ @@ -133,22 +171,6 @@ private function formatDateString(string $format, string $locale, $dateArray): s 'D' => $formattedArray['D'], ]; - $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, $formatData)) { - // Append the formatted value to the result string - $formattedString .= $formatData[$char]; - } else { - // If it's not a valid format character, append it as is - $formattedString .= $char; - } - } - - return $formattedString; + return $this->getFormattedString($format, $formatData, $locale); } } From c5598e6b311495e00e6d667f4f4a74c02d0421c8 Mon Sep 17 00:00:00 2001 From: AnuzPandey Date: Sun, 19 Nov 2023 00:17:07 +0545 Subject: [PATCH 3/5] :zap: features: fix ordinalSuffix to English Date Value. --- src/Traits/HelperTrait.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Traits/HelperTrait.php b/src/Traits/HelperTrait.php index b257e51..300960c 100644 --- a/src/Traits/HelperTrait.php +++ b/src/Traits/HelperTrait.php @@ -94,9 +94,11 @@ public function getFormattedString(string $format, array $formatData, string $lo if ($locale === 'np') { $formattedString .= $formatData[$char]; } else { - $formattedString .= $char === 'S' - ? $this->getOrdinalSuffix((int) $formatData['j']) - : $formatData[$char]; + if ($char === 'S') { + $formattedString .= $this->getOrdinalSuffix((int) $formatData['j']); + } else { + $formattedString .= $formatData[$char]; + } } } else { $formattedString .= $char; @@ -169,6 +171,7 @@ private function formatDateString(string $format, string $locale, $dateArray): s 'j' => $formattedArray['j'], 'l' => $formattedArray['l'], 'D' => $formattedArray['D'], + 'S' => $formattedArray['j'], ]; return $this->getFormattedString($format, $formatData, $locale); From 71b76eed024878f571c9810aba95992bfc0d93a5 Mon Sep 17 00:00:00 2001 From: AnuzPandey Date: Sun, 19 Nov 2023 00:19:20 +0545 Subject: [PATCH 4/5] :zap: features: fix ordinalSuffix to English Date Value. --- src/Traits/HelperTrait.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Traits/HelperTrait.php b/src/Traits/HelperTrait.php index 300960c..8ed2e5e 100644 --- a/src/Traits/HelperTrait.php +++ b/src/Traits/HelperTrait.php @@ -111,14 +111,14 @@ public function getFormattedString(string $format, array $formatData, string $lo public function getOrdinalSuffix(int $number): string { if ($number % 100 >= 11 && $number % 100 <= 13) { - return $number . 'th'; + return 'th'; } return match ($number % 10) { - 1 => $number . 'st', - 2 => $number . 'nd', - 3 => $number . 'rd', - default => $number . 'th', + 1 => 'st', + 2 => 'nd', + 3 => 'rd', + default => 'th', }; } From 4ac6cb54aeeafcaa9f97481bcc658d1c776aeaae Mon Sep 17 00:00:00 2001 From: AnuzPandey Date: Sun, 19 Nov 2023 00:23:01 +0545 Subject: [PATCH 5/5] :zap: features: fix ordinalSuffix to English Date Value in np locale. --- src/Traits/HelperTrait.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Traits/HelperTrait.php b/src/Traits/HelperTrait.php index 8ed2e5e..fe9a96e 100644 --- a/src/Traits/HelperTrait.php +++ b/src/Traits/HelperTrait.php @@ -92,6 +92,7 @@ public function getFormattedString(string $format, array $formatData, string $lo if (array_key_exists($char, $formatData)) { if ($locale === 'np') { + if ($char === 'S') continue; $formattedString .= $formatData[$char]; } else { if ($char === 'S') {