diff --git a/src/Helper/helper.php b/src/Helper/helper.php index 1987d3e..2281b44 100644 --- a/src/Helper/helper.php +++ b/src/Helper/helper.php @@ -6,12 +6,13 @@ if (! function_exists('toNepaliDate')) { /** * The function converts a given date to the Nepali date format - * @param string|Carbon $date
The date parameter is a string that represents the date in the English calendar format.
- * @param string|null $formatThe format parameter is used to specify the desired format of the Nepali date.
- * @param string $localeThe "locale" parameter is used to specify the language and region. Supported languages are en and np
+ * + * @param string|Carbon $dateThe date parameter is a string that represents the date in the English calendar format.
+ * @param string|null $formatThe format parameter is used to specify the desired format of the Nepali date.
+ * @param string $localeThe "locale" parameter is used to specify the language and region. Supported languages are en and np
* @return stringNepali date converted from the given English Date.
*/ - function toNepaliDate(string|Carbon $date, string $format = null, string $locale = null): string + function toNepaliDate(string|Carbon $date, ?string $format = null, ?string $locale = null): string { return LaravelNepaliDate::from($date) ->toNepaliDate( @@ -24,12 +25,13 @@ function toNepaliDate(string|Carbon $date, string $format = null, string $locale if (! function_exists('toEnglishDate')) { /** * The function converts a given date to the English date format - * @param $dateThe date parameter is a string that represents the date in the Nepali calendar format.
- * @param string $formatThe format parameter is used to specify the desired format of the English date.
- * @param string $localeThe "locale" parameter is used to specify the language and region. Supported languages are en and np
+ * + * @param $dateThe date parameter is a string that represents the date in the Nepali calendar format.
+ * @param string $formatThe format parameter is used to specify the desired format of the English date.
+ * @param string $localeThe "locale" parameter is used to specify the language and region. Supported languages are en and np
* @return stringEnglish date converted from the given Nepali Date.
*/ - function toEnglishDate(string|Carbon $date, string $format = null, string $locale = null): string + function toEnglishDate(string|Carbon $date, ?string $format = null, ?string $locale = null): string { return LaravelNepaliDate::from($date) ->toEnglishDate( diff --git a/src/LaravelNepaliDate.php b/src/LaravelNepaliDate.php index c4caff0..ddceff8 100755 --- a/src/LaravelNepaliDate.php +++ b/src/LaravelNepaliDate.php @@ -21,11 +21,9 @@ class LaravelNepaliDate public function __construct( public string|Carbon $date, - ) - { + ) { } - public static function from(string|Carbon $date): LaravelNepaliDate { $parsedDate = ($date instanceof Carbon) diff --git a/src/Mixin/NepaliDateMixin.php b/src/Mixin/NepaliDateMixin.php index 34691ff..8d48b04 100644 --- a/src/Mixin/NepaliDateMixin.php +++ b/src/Mixin/NepaliDateMixin.php @@ -11,7 +11,6 @@ */ class NepaliDateMixin { - public function toNepaliDate(): Closure { return function (?string $format = 'Y-m-d', ?string $locale = 'np') { @@ -21,7 +20,6 @@ public function toNepaliDate(): Closure }; } - public function toEnglishDate(): Closure { return function (?string $format = 'Y-m-d', ?string $locale = 'en') { @@ -30,5 +28,4 @@ public function toEnglishDate(): Closure return LaravelNepaliDate::from($date)->toEnglishDate($format, $locale); }; } - } diff --git a/src/Traits/EnglishDateTrait.php b/src/Traits/EnglishDateTrait.php index 09db771..a8e046b 100644 --- a/src/Traits/EnglishDateTrait.php +++ b/src/Traits/EnglishDateTrait.php @@ -61,7 +61,7 @@ trait EnglishDateTrait 9 => '9', ]; - public function toEnglishDate(string $format = null, string $locale = null): string + public function toEnglishDate(?string $format = null, ?string $locale = null): string { $checkIfIsInRange = $this->isInNepaliDateRange($this->date); @@ -169,8 +169,8 @@ 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; } diff --git a/src/Traits/HelperTrait.php b/src/Traits/HelperTrait.php index fe9a96e..79d067e 100644 --- a/src/Traits/HelperTrait.php +++ b/src/Traits/HelperTrait.php @@ -38,7 +38,6 @@ public function convertEnToNpNumber($number): array|string return str_replace($en_number, $np_number, $number); } - public function convertEnToNpWord($word): array|string { $en_word = [ @@ -82,7 +81,6 @@ public function convertEnToNpWord($word): array|string return str_replace($en_word, $np_word, $word); } - public function getFormattedString(string $format, array $formatData, string $locale): string { $formattedString = ''; @@ -92,7 +90,9 @@ public function getFormattedString(string $format, array $formatData, string $lo if (array_key_exists($char, $formatData)) { if ($locale === 'np') { - if ($char === 'S') continue; + if ($char === 'S') { + continue; + } $formattedString .= $formatData[$char]; } else { if ($char === 'S') { @@ -105,10 +105,10 @@ public function getFormattedString(string $format, array $formatData, string $lo $formattedString .= $char; } } + return $formattedString; } - public function getOrdinalSuffix(int $number): string { if ($number % 100 >= 11 && $number % 100 <= 13) { @@ -123,7 +123,6 @@ public function getOrdinalSuffix(int $number): string }; } - private function getNepaliLocaleFormattingCharacters(NepaliDateArrayData $nepaliDateArray): array { return [ @@ -139,7 +138,6 @@ private function getNepaliLocaleFormattingCharacters(NepaliDateArrayData $nepali ]; } - private function getEnglishLocaleFormattingCharacters(NepaliDateArrayData $nepaliDateArray): array { return [ @@ -155,7 +153,6 @@ private function getEnglishLocaleFormattingCharacters(NepaliDateArrayData $nepal ]; } - private function formatDateString(string $format, string $locale, $dateArray): string { $formattedArray = ($locale === 'en') diff --git a/src/Traits/NepaliDateTrait.php b/src/Traits/NepaliDateTrait.php index 4a55697..10efc9b 100644 --- a/src/Traits/NepaliDateTrait.php +++ b/src/Traits/NepaliDateTrait.php @@ -61,7 +61,7 @@ trait NepaliDateTrait 9 => '९', ]; - public function toNepaliDate(string $format = null, string $locale = null): string + public function toNepaliDate(?string $format = null, ?string $locale = null): string { $this->performCalculationOnEnglishDate(); @@ -80,8 +80,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, @@ -231,5 +231,4 @@ private function isInEnglishDateRange(Carbon $date): string|bool return true; } - } diff --git a/tests/ConvertToEnglishDateTest.php b/tests/ConvertToEnglishDateTest.php index cecdfc9..e1a248b 100644 --- a/tests/ConvertToEnglishDateTest.php +++ b/tests/ConvertToEnglishDateTest.php @@ -13,7 +13,6 @@ ['2022-12-20', '1966-04-02'], ]); - it('can convert to nepali formatted result', function (string $format, string $locale, string $expectedResult) { $date = '2053-01-10'; @@ -40,9 +39,8 @@ ['2022-12-20', '1966-04-02'], ]); - it('can convert to nepali formatted result to english date with helper function', function (string $format, string $locale, string $expectedResult) { - expect(toEnglishDate("2053-01-10", $format, $locale)) + expect(toEnglishDate('2053-01-10', $format, $locale)) ->toBe($expectedResult); })->with([ ['d F Y, l', 'np', '२२ अप्रिल १९९६, सोमबार'], diff --git a/tests/ConvertToNepaliDateTest.php b/tests/ConvertToNepaliDateTest.php index 8e197b5..0348fa5 100644 --- a/tests/ConvertToNepaliDateTest.php +++ b/tests/ConvertToNepaliDateTest.php @@ -30,7 +30,6 @@ ['Y/m/d', 'np', '२०५३/०१/१०'], ]); - it('can convert to basic nepali date with helper function toNepaliDate', function (string $date, string $expectedResult) { expect(toNepaliDate($date)) ->toBe($expectedResult); @@ -41,7 +40,7 @@ ]); it('can convert to nepali formatted result with helper function toNepaliDate', function (string $format, string $locale, string $expectedResult) { - expect(toNepaliDate("1996-04-22", $format, $locale)) + expect(toNepaliDate('1996-04-22', $format, $locale)) ->toBe($expectedResult); })->with([ ['d F Y, l', 'np', '१० वैशाख २०५३, सोमबार'],