From 264b4488a6d07e946260c6d3eacd4b5bbd652fda Mon Sep 17 00:00:00 2001 From: Mahdi Ziraki Date: Mon, 21 Feb 2022 00:15:17 +0330 Subject: [PATCH] add syntax sugar for Carbon conversions --- docs/en/index.html | 8 ++++++++ docs/index.html | 8 ++++++++ src/Laravel/VertaServiceProvider.php | 5 +++++ src/Traits/Creator.php | 11 +++++++++++ tests/CreatorTest.php | 7 +++++++ 5 files changed, 39 insertions(+) diff --git a/docs/en/index.html b/docs/en/index.html index 6753d1a..ddbef7b 100644 --- a/docs/en/index.html +++ b/docs/en/index.html @@ -118,6 +118,12 @@ return new Verta($dt); // 1395-12-09 15:05:56
 $c = \Carbon::now();
 return verta($c); // 1395-12-09 15:05:56
+// or
+\Illuminate\Support\Carbon::now()->toJalali(); // 1395-12-09 15:05:56
+// or with laravel helper
+now()->toJalali(); // 1395-12-09 15:05:56
+// or in models
+return $model->created_at->toJalali();
 

To use the jalali datetime as a string, use the following command

@@ -165,6 +171,8 @@ $dt = verta()->datetime();
                 

Verta to Carbon, use the following command

 $c = Carbon::instance($v->datetime());
+// or
+$c = $v->toCarbon();
 

Property

To get property, use the following command

diff --git a/docs/index.html b/docs/index.html index 7ca3b70..c4429c1 100644 --- a/docs/index.html +++ b/docs/index.html @@ -123,6 +123,12 @@ return new Verta($dt); // 1395-12-09 15:05:56
 $c = \Carbon::now();
 return verta($c); // 1395-12-09 15:05:56
+// or
+\Illuminate\Support\Carbon::now()->toJalali(); // 1395-12-09 15:05:56
+// or with laravel helper
+now()->toJalali(); // 1395-12-09 15:05:56
+// or in models
+return $model->created_at->toJalali();
 

برای استفاده از تاریخ شمسی به صورت رشته، از دستور زیر استفاده کنید

@@ -170,6 +176,8 @@ $dt = verta()->datetime();
 

برای تبدیل تاریخ از ورتا به کربن، از دستور زیر استفاده کنید

 $c = Carbon::instance($v->datetime());
+// or
+$c = $v->toCarbon();
 

خصوصیات

برای دریافت مقادیر خصوصیات، از دستورات زیر استفاده کنید

diff --git a/src/Laravel/VertaServiceProvider.php b/src/Laravel/VertaServiceProvider.php index cf1bc00..a38dfe7 100644 --- a/src/Laravel/VertaServiceProvider.php +++ b/src/Laravel/VertaServiceProvider.php @@ -3,6 +3,7 @@ namespace Hekmatinasser\Verta\Laravel; use Hekmatinasser\Verta\Verta; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Validator; use Illuminate\Support\ServiceProvider; @@ -87,6 +88,10 @@ class VertaServiceProvider extends ServiceProvider $this->app->bind('verta', function ($app) { return new Verta(); }); + + Carbon::macro('toJalali', function ($timezone = null) { + return new Verta($this, $timezone); + }); } /** diff --git a/src/Traits/Creator.php b/src/Traits/Creator.php index 2543a6a..e1119f1 100644 --- a/src/Traits/Creator.php +++ b/src/Traits/Creator.php @@ -5,6 +5,7 @@ namespace Hekmatinasser\Verta\Traits; use DateTime; use DateTimeZone; use Exception; +use Illuminate\Support\Carbon; use InvalidArgumentException; trait Creator @@ -128,6 +129,16 @@ trait Creator return new DateTime(date('Y-m-d H:i:s', $this->getTimestamp()), $this->getTimezone()); } + /** + * Create a Carbon instance from Verta + * + * @return Carbon + */ + public function toCarbon() + { + return Carbon::instance($this->datetime()); + } + /** * Create a Verta instance from a DateTime one * diff --git a/tests/CreatorTest.php b/tests/CreatorTest.php index e91666f..c2de3c3 100644 --- a/tests/CreatorTest.php +++ b/tests/CreatorTest.php @@ -74,6 +74,13 @@ class CreatorTest extends TestCase $this->assertEquals('2019-01-01 10:20:11', $datetime); } + public function testToCarbon() + { + $datetime = verta('2019-01-01 10:20:11')->toCarbon()->format('Y-m-d H:i:s'); + + $this->assertEquals('2019-01-01 10:20:11', $datetime); + } + public function testParse() { $datetime = Verta::parse('1397 دی 11 10:20:11')->formatGregorian('Y-m-d H:i:s');