Skip to content

Commit

Permalink
Merge pull request #5 from anuzpandey/dev
Browse files Browse the repository at this point in the history
⚡ features: add format and locale option to english date method
  • Loading branch information
anuzpandey authored Nov 18, 2023
2 parents 3854e11 + 4ac6cb5 commit b358f37
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 119 deletions.
40 changes: 19 additions & 21 deletions src/Traits/EnglishDateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,27 @@ 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);
}

$totalNepaliDays = $this->calculateTotalNepaliDays();

$this->performCalculationBasedonNepaliDays($totalNepaliDays);

return $this->englishYear.'-'.$this->englishMonth.'-'.$this->englishDay;
return $this->englishYear . '-' . $this->englishMonth . '-' . $this->englishDay;
}


public function toEnglishDateArray(): NepaliDateArrayData
{
$this->toEnglishDate();
Expand All @@ -94,6 +100,7 @@ public function toEnglishDateArray(): NepaliDateArrayData
]);
}


public function isInNepaliDateRange(Carbon $date): string|bool
{
if ($date->year < 2000 || $date->year > 2089) {
Expand All @@ -111,6 +118,7 @@ public function isInNepaliDateRange(Carbon $date): string|bool
return true;
}


public function calculateTotalNepaliDays()
{
$totalNepaliDays = 0;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}
99 changes: 97 additions & 2 deletions src/Traits/HelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
}
}
123 changes: 27 additions & 96 deletions src/Traits/NepaliDateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
'आइतबार' => 'आइत',
'सोमबार' => 'सोम',
'मङ्गलबार' => 'मङ्गल',
'बुधबार' => 'बुध',
'बिहिबार' => 'बिहि',
'शुक्रबार' => 'शुक्र',
'शनिबार' => 'शनि',
};
}

}

0 comments on commit b358f37

Please sign in to comment.