Skip to content

Commit

Permalink
🔨 fix: memory exhaustion issue with recursive call.
Browse files Browse the repository at this point in the history
  • Loading branch information
anuzpandey committed Nov 18, 2023
1 parent 593deab commit 650c24f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 115 deletions.
8 changes: 1 addition & 7 deletions src/Traits/EnglishDateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ trait EnglishDateTrait

public function toEnglishDate(string $format = 'Y-m-d', string $locale = 'en'): string
{
if ($format) {
return $this->toFormattedEnglishDate($format, $locale);
}

$checkIfIsInRange = $this->isInNepaliDateRange($this->date);

if (!$checkIfIsInRange) {
Expand All @@ -78,14 +74,12 @@ public function toEnglishDate(string $format = 'Y-m-d', string $locale = 'en'):

$this->performCalculationBasedonNepaliDays($totalNepaliDays);

return $this->englishYear . '-' . $this->englishMonth . '-' . $this->englishDay;
return $this->toFormattedEnglishDate($format, $locale);
}


public function toEnglishDateArray(): NepaliDateArrayData
{
$this->toEnglishDate();

return NepaliDateArrayData::from([
'year' => $this->englishYear,
'month' => $this->englishMonth,
Expand Down
39 changes: 17 additions & 22 deletions src/Traits/NepaliDateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Anuzpandey\LaravelNepaliDate\DataTransferObject\NepaliDateArrayData;
use Carbon\Carbon;
use Illuminate\Support\Str;
use RuntimeException;

trait NepaliDateTrait
Expand Down Expand Up @@ -63,27 +62,11 @@ trait NepaliDateTrait
];


public function toNepaliDate(string $format = 'Y-m-d', string $locale = 'np'): string
public function toNepaliDate(string $format = 'Y-m-d', string $locale = 'en'): string
{
if ($format) {
return $this->toFormattedNepaliDate($format, $locale);
}

$checkIfIsInRange = $this->isInEnglishDateRange($this->date);

if (!$checkIfIsInRange) {
throw new RuntimeException($checkIfIsInRange);
}
$this->performCalculationOnEnglishDate();

$totalEnglishDays = $this->calculateTotalEnglishDays($this->date->year, $this->date->month, $this->date->day);

$this->performCalculationBasedOn($totalEnglishDays);

$year = $this->nepaliYear;
$month = $this->nepaliMonth < 10 ? '0' . $this->nepaliMonth : $this->nepaliMonth;
$day = $this->nepaliDay < 10 ? '0' . $this->nepaliDay : $this->nepaliDay;

return $year . '-' . $month . '-' . $day;
return $this->toFormattedNepaliDate($format, $locale);
}


Expand All @@ -100,8 +83,6 @@ public function toFormattedNepaliDate(

public function toNepaliDateArray(): NepaliDateArrayData
{
$this->toNepaliDate();

$nepaliMonth = $this->nepaliMonth > 9 ? $this->nepaliMonth : '0' . $this->nepaliMonth;
$nepaliDay = $this->nepaliDay > 9 ? $this->nepaliDay : '0' . $this->nepaliDay;

Expand Down Expand Up @@ -146,6 +127,20 @@ public function getShortDayName(string $npDayName, string $locale = 'np'): strin
}


public function performCalculationOnEnglishDate(): void
{
$checkIfIsInRange = $this->isInEnglishDateRange($this->date);

if (!$checkIfIsInRange) {
throw new RuntimeException($checkIfIsInRange);
}

$totalEnglishDays = $this->calculateTotalEnglishDays($this->date->year, $this->date->month, $this->date->day);

$this->performCalculationBasedOn($totalEnglishDays);
}


private function calculateTotalEnglishDays($year, $month, $day)
{
$totalEnglishDays = 0;
Expand Down
8 changes: 8 additions & 0 deletions tests/ArchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@
it('will not use debugging functions')
->expect(['dd', 'dump', 'ray'])
->each->not->toBeUsed();

it('ensures traits are suffixed with Trait')
->expect('Anuzpandey\LaravelNepaliDate\Traits')
->toHaveSuffix('Trait');

it('ensures mixins are suffixed with Mixin')
->expect('Anuzpandey\LaravelNepaliDate\Mixin')
->toHaveSuffix('Mixin');
71 changes: 28 additions & 43 deletions tests/ConvertToEnglishDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,31 @@

use Anuzpandey\LaravelNepaliDate\LaravelNepaliDate;

//it('can convert to basic nepali date', function (string $date, string $expectedResult) {
// $nepaliDate = LaravelNepaliDate::from($date)->toEnglishDate();
//
// expect($nepaliDate)
// ->toBe($expectedResult);
//})->with([
// ['2053-01-10', '1996-04-22'],
// ['2029-04-04', '1972-07-19'],
// ['2022-12-20', '1966-04-02'],
//]);
//
//it('can convert to english date array', function () {
// $date = '2053-01-10';
//
// $englishDateArray = LaravelNepaliDate::from($date)->toEnglishDateArray();
//
// expect($englishDateArray->toArray())
// ->toBeArray()
// ->toMatchArray([
// 'year' => '1996',
// 'month' => '04',
// 'day' => '22',
// 'npYear' => '१९९६',
// 'npDayName' => 'सोमबार',
// ]);
//});
//
//it('can convert to nepali formatted result', function (string $format, string $locale, string $expectedResult) {
// $date = '2053-01-10';
//
// expect(LaravelNepaliDate::from($date)->toFormattedEnglishDate(format: $format, locale: $locale))
// ->toBe($expectedResult);
//})->with([
// ['d F Y, l', 'np', '२२ अप्रिल १९९६, सोमबार'],
// ['d F Y, l', 'en', '22 April 1996, Monday'],
// ['Y-m-d', 'np', '१९९६-०४-२२'],
// ['Y-m-d', 'en', '1996-04-22'],
// ['l, d F Y', 'np', 'सोमबार, २२ अप्रिल १९९६'],
// ['l, d F Y', 'en', 'Monday, 22 April 1996'],
// ['d F Y', 'np', '२२ अप्रिल १९९६'],
// ['d F Y', 'en', '22 April 1996'],
// ['Y/m/d', 'np', '१९९६/०४/२२'],
//]);
it('can convert to basic nepali date', function (string $date, string $expectedResult) {
$nepaliDate = LaravelNepaliDate::from($date)->toEnglishDate();

expect($nepaliDate)
->toBe($expectedResult);
})->with([
['2053-01-10', '1996-04-22'],
['2029-04-04', '1972-07-19'],
['2022-12-20', '1966-04-02'],
]);


it('can convert to nepali formatted result', function (string $format, string $locale, string $expectedResult) {
$date = '2053-01-10';

expect(LaravelNepaliDate::from($date)->toEnglishDate(format: $format, locale: $locale))
->toBe($expectedResult);
})->with([
['d F Y, l', 'np', '२२ अप्रिल १९९६, सोमबार'],
['d F Y, l', 'en', '22 April 1996, Monday'],
['Y-m-d', 'np', '१९९६-०४-२२'],
['Y-m-d', 'en', '1996-04-22'],
['l, d F Y', 'np', 'सोमबार, २२ अप्रिल १९९६'],
['l, d F Y', 'en', 'Monday, 22 April 1996'],
['d F Y', 'np', '२२ अप्रिल १९९६'],
['d F Y', 'en', '22 April 1996'],
['Y/m/d', 'np', '१९९६/०४/२२'],
]);
70 changes: 27 additions & 43 deletions tests/ConvertToNepaliDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,30 @@

use Anuzpandey\LaravelNepaliDate\LaravelNepaliDate;

//it('can convert to basic nepali date', function (string $date, string $expectedResult) {
// $nepaliDate = LaravelNepaliDate::from($date)->toNepaliDate();
//
// expect($nepaliDate)
// ->toBe($expectedResult);
//})->with([
// ['1996-04-22', '2053-01-10'],
// ['1972-07-19', '2029-04-04'],
// ['1966-04-02', '2022-12-20'],
//]);
//
//it('can convert to nepali date array', function () {
// $date = '1996-04-22';
//
// $nepaliDateArray = LaravelNepaliDate::from($date)->toNepaliDateArray();
//
// expect($nepaliDateArray->toArray())
// ->toBeArray()
// ->toMatchArray([
// 'year' => '2053',
// 'month' => '01',
// 'day' => '10',
// 'npYear' => '२०५३',
// 'npDayName' => 'सोमबार',
// ]);
//});
//
//it('can convert to nepali formatted result', function (string $format, string $locale, string $expectedResult) {
// $date = '1996-04-22';
//
// expect(LaravelNepaliDate::from($date)->toFormattedNepaliDate(format: $format, locale: $locale))
// ->toBe($expectedResult);
//})->with([
// ['d F Y, l', 'np', '१० वैशाख २०५३, सोमबार'],
// ['d F Y, l', 'en', '10 Baisakh 2053, Monday'],
// ['Y-m-d', 'np', '२०५३-०१-१०'],
// ['Y-m-d', 'en', '2053-01-10'],
// ['l, d F Y', 'np', 'सोमबार, १० वैशाख २०५३'],
// ['l, d F Y', 'en', 'Monday, 10 Baisakh 2053'],
// ['d F Y', 'np', '१० वैशाख २०५३'],
// ['d F Y', 'en', '10 Baisakh 2053'],
// ['Y/m/d', 'np', '२०५३/०१/१०'],
//]);
it('can convert to basic nepali date', function (string $date, string $expectedResult) {
$nepaliDate = LaravelNepaliDate::from($date)->toNepaliDate();

expect($nepaliDate)
->toBe($expectedResult);
})->with([
['1996-04-22', '2053-01-10'],
['1972-07-19', '2029-04-04'],
['1966-04-02', '2022-12-20'],
]);

it('can convert to nepali formatted result', function (string $format, string $locale, string $expectedResult) {
$date = '1996-04-22';

expect(LaravelNepaliDate::from($date)->toNepaliDate(format: $format, locale: $locale))
->toBe($expectedResult);
})->with([
['d F Y, l', 'np', '१० वैशाख २०५३, सोमबार'],
['d F Y, l', 'en', '10 Baisakh 2053, Monday'],
['Y-m-d', 'np', '२०५३-०१-१०'],
['Y-m-d', 'en', '2053-01-10'],
['l, d F Y', 'np', 'सोमबार, १० वैशाख २०५३'],
['l, d F Y', 'en', 'Monday, 10 Baisakh 2053'],
['d F Y', 'np', '१० वैशाख २०५३'],
['d F Y', 'en', '10 Baisakh 2053'],
['Y/m/d', 'np', '२०५३/०१/१०'],
]);

0 comments on commit 650c24f

Please sign in to comment.