From 2248e25e84f3c615d0381f1fcc170f48f7d28a76 Mon Sep 17 00:00:00 2001 From: AnuzPandey Date: Sun, 12 May 2024 15:25:56 +0545 Subject: [PATCH 1/4] :recycle: chore: remove carbon instance support from main class. --- src/LaravelNepaliDate.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/LaravelNepaliDate.php b/src/LaravelNepaliDate.php index 78f225f..549547a 100755 --- a/src/LaravelNepaliDate.php +++ b/src/LaravelNepaliDate.php @@ -8,7 +8,7 @@ use Anuzpandey\LaravelNepaliDate\Traits\HelperTrait; use Anuzpandey\LaravelNepaliDate\Traits\IsLeapYearTrait; use Anuzpandey\LaravelNepaliDate\Traits\NepaliDateTrait; -use Carbon\Carbon; +use Illuminate\Support\Str; class LaravelNepaliDate { @@ -20,17 +20,16 @@ class LaravelNepaliDate use NepaliDateTrait; public function __construct( - public string|Carbon $date, - ) - { + public int|string $year, + public int|string $month, + public int|string $day, + ) { } - public static function from(string|Carbon $date): LaravelNepaliDate + public static function from(string $date): LaravelNepaliDate { - $parsedDate = ($date instanceof Carbon) - ? $date - : Carbon::parse($date); + [$year, $month, $day] = Str::of($date)->explode('-')->toArray(); - return new static($parsedDate); + return new static($year, $month, $day); } } From 2a95241b064a1aa01cf21fd1d782dcc45c3a58a9 Mon Sep 17 00:00:00 2001 From: AnuzPandey Date: Sun, 12 May 2024 15:49:26 +0545 Subject: [PATCH 2/4] :recycle: chore: fix toNepaliDate feature to omit Carbon usage. --- src/LaravelNepaliDate.php | 2 +- src/Traits/NepaliDateTrait.php | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/LaravelNepaliDate.php b/src/LaravelNepaliDate.php index 549547a..9597998 100755 --- a/src/LaravelNepaliDate.php +++ b/src/LaravelNepaliDate.php @@ -30,6 +30,6 @@ public static function from(string $date): LaravelNepaliDate { [$year, $month, $day] = Str::of($date)->explode('-')->toArray(); - return new static($year, $month, $day); + return new static((int) $year, (int) $month, (int) $day); } } diff --git a/src/Traits/NepaliDateTrait.php b/src/Traits/NepaliDateTrait.php index ec5bf53..95f1a80 100644 --- a/src/Traits/NepaliDateTrait.php +++ b/src/Traits/NepaliDateTrait.php @@ -159,13 +159,13 @@ public function getShortDayName(string $npDayName, string $locale = 'np'): strin public function performCalculationOnEnglishDate(): void { - $checkIfIsInRange = $this->isInEnglishDateRange($this->date); + $checkIfIsInRange = $this->isInEnglishDateRange($this->year, $this->month, $this->day); if (! $checkIfIsInRange) { throw new RuntimeException($checkIfIsInRange); } - $totalEnglishDays = $this->calculateTotalEnglishDays($this->date->year, $this->date->month, $this->date->day); + $totalEnglishDays = $this->calculateTotalEnglishDays($this->year, $this->month, $this->day); $this->performCalculationBasedOn($totalEnglishDays); } @@ -250,17 +250,17 @@ private function formattedNepaliNumber($value): string return implode('', $numbers); } - private function isInEnglishDateRange(Carbon $date): string|bool + private function isInEnglishDateRange(int $year, int $month, int $day): string|bool { - if ($date->year < 1944 || $date->year > 2033) { + if ($year < 1944 || $year > 2033) { return 'Date is out of range. Please provide date between 1944-01-01 to 2033-12-31'; } - if ($date->month < 1 || $date->month > 12) { + if ($month < 1 || $month > 12) { return 'Month is out of range. Please provide month between 1-12'; } - if ($date->day < 1 || $date->day > 31) { + if ($day < 1 || $day > 31) { return 'Day is out of range. Please provide day between 1-31'; } From 063b2e7a0cf3850889b84a9c58aa43100c02036d Mon Sep 17 00:00:00 2001 From: AnuzPandey Date: Sun, 12 May 2024 16:15:16 +0545 Subject: [PATCH 3/4] :recycle: chore: update EnglishDate feature to omit Carbon usage. --- src/Traits/EnglishDateTrait.php | 17 ++++++++--------- src/Traits/NepaliDateTrait.php | 5 ++--- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Traits/EnglishDateTrait.php b/src/Traits/EnglishDateTrait.php index a8e046b..5d7a487 100644 --- a/src/Traits/EnglishDateTrait.php +++ b/src/Traits/EnglishDateTrait.php @@ -3,7 +3,6 @@ namespace Anuzpandey\LaravelNepaliDate\Traits; use Anuzpandey\LaravelNepaliDate\DataTransferObject\NepaliDateArrayData; -use Carbon\Carbon; use RuntimeException; trait EnglishDateTrait @@ -63,7 +62,7 @@ trait EnglishDateTrait public function toEnglishDate(?string $format = null, ?string $locale = null): string { - $checkIfIsInRange = $this->isInNepaliDateRange($this->date); + $checkIfIsInRange = $this->isInNepaliDateRange($this->year, $this->month, $this->day); if (! $checkIfIsInRange) { throw new RuntimeException($checkIfIsInRange); @@ -95,17 +94,17 @@ public function toEnglishDateArray(): NepaliDateArrayData ]); } - public function isInNepaliDateRange(Carbon $date): string|bool + public function isInNepaliDateRange(int $year, int $month, int $day): string|bool { - if ($date->year < 2000 || $date->year > 2089) { + if ($year < 2000 || $year > 2089) { return 'Date is out of range. Please provide date between 2000 to 2089'; } - if ($date->month < 1 || $date->month > 12) { + if ($month < 1 || $month > 12) { return 'Month is out of range. Please provide month between 1-12'; } - if ($date->day < 1 || $date->day > 32) { + if ($day < 1 || $day > 32) { return 'Day is out of range. Please provide day between 1-32'; } @@ -117,7 +116,7 @@ public function calculateTotalNepaliDays() $totalNepaliDays = 0; $k = 0; - for ($i = 0; $i < ($this->date->year - $this->nepaliYear); $i++) { + for ($i = 0; $i < ($this->year - $this->nepaliYear); $i++) { for ($j = 1; $j <= 12; $j++) { $totalNepaliDays += $this->calendarData[$k][$j]; } @@ -125,12 +124,12 @@ public function calculateTotalNepaliDays() } // Count Total Days in terms of month - for ($j = 1; $j < $this->date->month; $j++) { + for ($j = 1; $j < $this->month; $j++) { $totalNepaliDays += $this->calendarData[$k][$j]; } // Count Total Days in Terms of days - $totalNepaliDays += $this->date->day; + $totalNepaliDays += $this->day; return $totalNepaliDays; } diff --git a/src/Traits/NepaliDateTrait.php b/src/Traits/NepaliDateTrait.php index 95f1a80..e3dccea 100644 --- a/src/Traits/NepaliDateTrait.php +++ b/src/Traits/NepaliDateTrait.php @@ -5,7 +5,6 @@ use Anuzpandey\LaravelNepaliDate\Constants\NepaliDate; use Anuzpandey\LaravelNepaliDate\DataTransferObject\NepaliDateArrayData; use Anuzpandey\LaravelNepaliDate\Enums\NepaliMonth; -use Carbon\Carbon; use RuntimeException; trait NepaliDateTrait @@ -115,8 +114,8 @@ public function toFormattedNepaliDate(string $format, string $locale): string public function toNepaliDateArray(): NepaliDateArrayData { - $nepaliMonth = $this->nepaliMonth > 9 ? $this->nepaliMonth : '0' . $this->nepaliMonth; - $nepaliDay = $this->nepaliDay > 9 ? $this->nepaliDay : '0' . $this->nepaliDay; + $nepaliMonth = $this->nepaliMonth > 9 ? $this->nepaliMonth : '0'.$this->nepaliMonth; + $nepaliDay = $this->nepaliDay > 9 ? $this->nepaliDay : '0'.$this->nepaliDay; return NepaliDateArrayData::from([ 'year' => $this->nepaliYear, From ea01e3dce765ac3b46e7c150e7b47f7bbc0f5f65 Mon Sep 17 00:00:00 2001 From: AnuzPandey Date: Sun, 12 May 2024 16:16:24 +0545 Subject: [PATCH 4/4] :white_check_mark: test: add tests to check if the new update fixes the issue. --- tests/ConvertToEnglishDateTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/ConvertToEnglishDateTest.php b/tests/ConvertToEnglishDateTest.php index e1a248b..45c32b6 100644 --- a/tests/ConvertToEnglishDateTest.php +++ b/tests/ConvertToEnglishDateTest.php @@ -11,6 +11,9 @@ ['2053-01-10', '1996-04-22'], ['2029-04-04', '1972-07-19'], ['2022-12-20', '1966-04-02'], + ['2081-01-30', '2024-05-12'], + ['2081-02-32', '2024-06-14'], + ['2081-02-32', '2024-06-14'], ]); it('can convert to nepali formatted result', function (string $format, string $locale, string $expectedResult) {