Skip to content

Commit

Permalink
Merge pull request #7 from anuzpandey/dev
Browse files Browse the repository at this point in the history
🔨 fix: memory exhaustion issue with recursive call.
  • Loading branch information
anuzpandey authored Nov 18, 2023
2 parents 6d4a9e2 + 650c24f commit edcacf8
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 63 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,47 @@ The following format specifiers are supported for formatting dates:
- `l` - Day in full name (Sunday-Saturday/आइतबार-शनिबार)
- `S` - Day in two letters (st, nd, rd, th)

## Extending Carbon with NepaliDateMixin
In order to use the `toNepaliDate` and `toEnglishDate` mixin on Carbon instances, you need to register the `NepaliDateMixin` mixin in your Laravel service provider.

You can do so by adding the following code to your `AppServiceProvider`
```php
<?php

use Anuzpandey\LaravelNepaliDate\NepaliDateMixin;
use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
// Register the NepaliDateMixin
Carbon::mixin(new NepaliDateMixin());
}
}
```

Or create your own service provider with `php artisan make:provider MacroableServiceProvider`,
register it in `config/app.php` and add the following code to the `boot` method.
```php
<?php

use Anuzpandey\LaravelNepaliDate\NepaliDateMixin;
use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;

class MacroableServiceProvider extends ServiceProvider
{
public function boot(): void
{
// Register the NepaliDateMixin
Carbon::mixin(new NepaliDateMixin());
}
}
```


## Testing

```bash
Expand Down
34 changes: 34 additions & 0 deletions src/Mixin/NepaliDateMixin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Anuzpandey\LaravelNepaliDate\Mixin;

use Anuzpandey\LaravelNepaliDate\LaravelNepaliDate;
use Carbon\Carbon;
use Closure;

/**
* @mixin Carbon
*/
class NepaliDateMixin
{

public function toNepaliDate(): Closure
{
return function (?string $format = 'Y-m-d', ?string $locale = 'np') {
$date = $this->toDateString();

return LaravelNepaliDate::from($date)->toNepaliDate($format, $locale);
};
}


public function toEnglishDate(): Closure
{
return function (?string $format = 'Y-m-d', ?string $locale = 'en') {
$date = $this->toDateString();

return LaravelNepaliDate::from($date)->toEnglishDate($format, $locale);
};
}

}
10 changes: 2 additions & 8 deletions src/Traits/EnglishDateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,8 @@ trait EnglishDateTrait
];


public function toEnglishDate(?string $format = NULL, ?string $locale = 'en'): string
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 = NULL, ?string $locale = 'en'): s

$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 = NULL, ?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');
17 changes: 1 addition & 16 deletions tests/ConvertToEnglishDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,11 @@
['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))
expect(LaravelNepaliDate::from($date)->toEnglishDate(format: $format, locale: $locale))
->toBe($expectedResult);
})->with([
['d F Y, l', 'np', '२२ अप्रिल १९९६, सोमबार'],
Expand Down
18 changes: 1 addition & 17 deletions tests/ConvertToNepaliDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,10 @@
['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))
expect(LaravelNepaliDate::from($date)->toNepaliDate(format: $format, locale: $locale))
->toBe($expectedResult);
})->with([
['d F Y, l', 'np', '१० वैशाख २०५३, सोमबार'],
Expand Down

0 comments on commit edcacf8

Please sign in to comment.