Skip to content

Commit

Permalink
♻️ chore: update EnglishDate feature to omit Carbon usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
anuzpandey committed May 12, 2024
1 parent 2a95241 commit 063b2e7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
17 changes: 8 additions & 9 deletions src/Traits/EnglishDateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Anuzpandey\LaravelNepaliDate\Traits;

use Anuzpandey\LaravelNepaliDate\DataTransferObject\NepaliDateArrayData;
use Carbon\Carbon;
use RuntimeException;

trait EnglishDateTrait
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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';
}

Expand All @@ -117,20 +116,20 @@ 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];
}
$k++;
}

// 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;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Traits/NepaliDateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 063b2e7

Please sign in to comment.