Skip to content

Commit

Permalink
⚡ features: add carbon extension feature with mixin.
Browse files Browse the repository at this point in the history
  • Loading branch information
anuzpandey committed Nov 18, 2023
1 parent 9e1014a commit 2400978
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 26 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
24 changes: 0 additions & 24 deletions src/Mixin/CarbonMixin.php

This file was deleted.

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);
};
}

}
2 changes: 1 addition & 1 deletion src/Traits/EnglishDateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ 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);
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/NepaliDateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ trait NepaliDateTrait
];


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

0 comments on commit 2400978

Please sign in to comment.