Skip to content

Commit

Permalink
Merge pull request #14 from anuzpandey/updates/remove-carbon
Browse files Browse the repository at this point in the history
Updates: Remove Carbon Dependency from Package.
  • Loading branch information
anuzpandey authored May 12, 2024
2 parents 5ef2b8d + ea01e3d commit 607e3b9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
17 changes: 8 additions & 9 deletions src/LaravelNepaliDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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((int) $year, (int) $month, (int) $day);
}
}
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
17 changes: 8 additions & 9 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 Expand Up @@ -159,13 +158,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);
}
Expand Down Expand Up @@ -250,17 +249,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';
}

Expand Down
3 changes: 3 additions & 0 deletions tests/ConvertToEnglishDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 607e3b9

Please sign in to comment.