From 264b4488a6d07e946260c6d3eacd4b5bbd652fda Mon Sep 17 00:00:00 2001 From: Mahdi Ziraki Date: Mon, 21 Feb 2022 00:15:17 +0330 Subject: [PATCH 1/6] 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'); From 313159a0fcddccbc5774fc4449832f32829ab7d4 Mon Sep 17 00:00:00 2001 From: Mahdi Ziraki Date: Mon, 21 Feb 2022 00:20:27 +0330 Subject: [PATCH 2/6] fix format method signature --- src/Traits/Formatting.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/Formatting.php b/src/Traits/Formatting.php index 584f190..33a79bd 100644 --- a/src/Traits/Formatting.php +++ b/src/Traits/Formatting.php @@ -325,7 +325,7 @@ trait Formatting * @param string $format * @return string */ - public function format($format) + public function format(string $format): string { return $this->date($this->strftime($format)); } From 84b7bf87a11414fb0eb9d83a33eafb2fd6f67c70 Mon Sep 17 00:00:00 2001 From: Mahdi Ziraki Date: Mon, 21 Feb 2022 00:28:35 +0330 Subject: [PATCH 3/6] fix timezone initialization from DateTime or Carbon object --- src/Traits/Creator.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Traits/Creator.php b/src/Traits/Creator.php index e1119f1..b4a7068 100644 --- a/src/Traits/Creator.php +++ b/src/Traits/Creator.php @@ -31,7 +31,11 @@ trait Creator } try { - parent::__construct($dt, static::createTimeZone($timezone)); + $tz = static::createTimeZone($timezone); + parent::__construct($dt, $tz); + if ($datetime instanceof DateTime) { + parent::setTimezone($tz); + } self::loadMessages(); } catch (Exception $exception) { throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime)); From 4db4b98065bacaa599e872569134cad931093109 Mon Sep 17 00:00:00 2001 From: Mahdi Ziraki Date: Wed, 23 Feb 2022 21:10:32 +0330 Subject: [PATCH 4/6] add test for timezone in instance from DateTime object temporary fix for php 8.1 warning --- src/Traits/Creator.php | 11 +++++++---- src/Traits/Formatting.php | 4 +++- tests/CreatorTest.php | 13 ++++++++++++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Traits/Creator.php b/src/Traits/Creator.php index b4a7068..8340bb5 100644 --- a/src/Traits/Creator.php +++ b/src/Traits/Creator.php @@ -31,10 +31,11 @@ trait Creator } try { - $tz = static::createTimeZone($timezone); - parent::__construct($dt, $tz); if ($datetime instanceof DateTime) { - parent::setTimezone($tz); + parent::__construct($dt); + parent::setTimezone($datetime->getTimezone()); + } else { + parent::__construct($dt, static::createTimeZone($timezone)); } self::loadMessages(); } catch (Exception $exception) { @@ -130,7 +131,9 @@ trait Creator */ public function datetime() { - return new DateTime(date('Y-m-d H:i:s', $this->getTimestamp()), $this->getTimezone()); + $dt = new DateTime(date('Y-m-d H:i:s', $this->getTimestamp())); + $dt->setTimezone($this->getTimezone()); + return $dt; } /** diff --git a/src/Traits/Formatting.php b/src/Traits/Formatting.php index 33a79bd..40718a7 100644 --- a/src/Traits/Formatting.php +++ b/src/Traits/Formatting.php @@ -4,6 +4,7 @@ namespace Hekmatinasser\Verta\Traits; use Hekmatinasser\Notowo\Notowo; use Hekmatinasser\Verta\Verta; +use ReturnTypeWillChange; trait Formatting { @@ -325,7 +326,8 @@ trait Formatting * @param string $format * @return string */ - public function format(string $format): string + #[ReturnTypeWillChange] + public function format($format) { return $this->date($this->strftime($format)); } diff --git a/tests/CreatorTest.php b/tests/CreatorTest.php index c2de3c3..34e5853 100644 --- a/tests/CreatorTest.php +++ b/tests/CreatorTest.php @@ -3,8 +3,10 @@ namespace Hekmatinasser\Verta\Tests; use DateTime; +use DateTimeZone; use Hekmatinasser\Verta\Facades\Verta as VertaFacade; use Hekmatinasser\Verta\Verta; +use Illuminate\Support\Facades\Date; use PHPUnit\Framework\TestCase; class CreatorTest extends TestCase @@ -55,7 +57,6 @@ class CreatorTest extends TestCase $this->assertEquals('1397-10-11 10:20:11', $datetime); - $datetime = (string) Verta::instance('2019-01-01 10:20:11'); $this->assertEquals('1397-10-11 10:20:11', $datetime); @@ -67,6 +68,16 @@ class CreatorTest extends TestCase $this->assertEquals('1397-10-11 10:20:11', $datetime); } + public function testInstanceFromDateTimeWithTimezone() + { + $now = new DateTime('now', new DateTimeZone('Asia/Tehran')); + + $datetime = new Verta($now); + + $this->assertEquals($now->getTimezone()->getName(), $datetime->getTimezone()->getName()); + $this->assertEquals($now->format('Y-m-d H:i:s'), $datetime->datetime()->format('Y-m-d H:i:s')); + } + public function testDatetime() { $datetime = verta('2019-01-01 10:20:11')->DateTime()->format('Y-m-d H:i:s'); From 5c961e62526575cd88c2c11c1b251c090eae6826 Mon Sep 17 00:00:00 2001 From: Mahdi Ziraki Date: Wed, 23 Feb 2022 21:19:46 +0330 Subject: [PATCH 5/6] revert back method name to prevent breaking change --- docs/en/index.html | 2 +- docs/index.html | 2 +- src/Traits/Accessor.php | 6 ++++-- src/Traits/Boundaries.php | 2 +- tests/AccessTest.php | 4 ++-- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/docs/en/index.html b/docs/en/index.html index ddbef7b..d939e17 100644 --- a/docs/en/index.html +++ b/docs/en/index.html @@ -220,7 +220,7 @@ $v = $v->timezone('Asia/Baku');
 $v = $v->setDateTime(1395, 4, 25, 16, 50, 42);
 $v = $v->setDateTime(1395, 4, 25, 16, 50, 42, 1569856);
-$v = $v->setDateJalali(1395, 4, 25);
+$v = $v->setDate(1395, 4, 25);
 $v = $v->setTimeString('12:25:48');
 

Format

diff --git a/docs/index.html b/docs/index.html index c4429c1..20c2614 100644 --- a/docs/index.html +++ b/docs/index.html @@ -225,7 +225,7 @@ $v = $v->timezone('Asia/Baku');
 $v = $v->setDateTime(1395, 4, 25, 16, 50, 42);
 $v = $v->setDateTime(1395, 4, 25, 16, 50, 42, 1569856);
-$v = $v->setDateJalali(1395, 4, 25);
+$v = $v->setDate(1395, 4, 25);
 $v = $v->setTimeString('12:25:48');
 

قالب نمایش

diff --git a/src/Traits/Accessor.php b/src/Traits/Accessor.php index 454c8d8..583c38f 100644 --- a/src/Traits/Accessor.php +++ b/src/Traits/Accessor.php @@ -5,6 +5,7 @@ namespace Hekmatinasser\Verta\Traits; use DateTimeZone; use Hekmatinasser\Verta\Verta; use InvalidArgumentException; +use ReturnTypeWillChange; trait Accessor { @@ -229,7 +230,7 @@ trait Accessor */ public function setDateTime($year, $month, $day, $hour, $minute, $second = 0, $microseconds = 0) { - return $this->setDateJalali($year, $month, $day)->setTime($hour, $minute, $second, $microseconds); + return $this->setDate($year, $month, $day)->setTime($hour, $minute, $second, $microseconds); } /** @@ -242,7 +243,8 @@ trait Accessor * * @return Verta */ - public function setDateJalali($year, $month, $day) + #[ReturnTypeWillChange] + public function setDate($year, $month, $day) { if (static::isValidDate($year, $month, $day)) { list($year, $month, $day) = self::getGregorian($year, $month, $day); diff --git a/src/Traits/Boundaries.php b/src/Traits/Boundaries.php index 4f3d402..7805207 100644 --- a/src/Traits/Boundaries.php +++ b/src/Traits/Boundaries.php @@ -133,7 +133,7 @@ trait Boundaries { $month = $this->quarter * static::MONTHS_PER_QUARTER; - return $this->setDateJalali($this->year, $month, 1)->endMonth(); + return $this->setDate($this->year, $month, 1)->endMonth(); } /** diff --git a/tests/AccessTest.php b/tests/AccessTest.php index 3e5ac36..43e4205 100644 --- a/tests/AccessTest.php +++ b/tests/AccessTest.php @@ -97,9 +97,9 @@ class AccessTest extends TestCase $this->assertEquals(11, $datetime->second); } - public function testSetDateJalali() + public function testSetDate() { - $datetime = verta()->setDateJalali(1397, 10, 11); + $datetime = verta()->setDate(1397, 10, 11); $this->assertEquals(1397, $datetime->year); $this->assertEquals(10, $datetime->month); From 06b0e4f1228d0250e3ec9c6d1904391c0575d183 Mon Sep 17 00:00:00 2001 From: Mahdi Ziraki Date: Wed, 23 Feb 2022 22:14:02 +0330 Subject: [PATCH 6/6] cleanup --- tests/CreatorTest.php | 2 +- tests/bootstrap.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 tests/bootstrap.php diff --git a/tests/CreatorTest.php b/tests/CreatorTest.php index 34e5853..6ce6a53 100644 --- a/tests/CreatorTest.php +++ b/tests/CreatorTest.php @@ -80,7 +80,7 @@ class CreatorTest extends TestCase public function testDatetime() { - $datetime = verta('2019-01-01 10:20:11')->DateTime()->format('Y-m-d H:i:s'); + $datetime = verta('2019-01-01 10:20:11')->datetime()->format('Y-m-d H:i:s'); $this->assertEquals('2019-01-01 10:20:11', $datetime); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index a814366..0000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1 +0,0 @@ -