add syntax sugar for Carbon conversions

This commit is contained in:
Mahdi Ziraki committed 2022-02-21 00:15:17 +03:30
1 parent 3e1ef52534
commit 264b4488a6
5 files changed
+39

No files matched your search

+8
View File
@@ -118,6 +118,12 @@ return new Verta($dt); // 1395-12-09 15:05:56
<pre> <pre>
$c = \Carbon::now(); $c = \Carbon::now();
return verta($c); // 1395-12-09 15:05:56 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();
</pre> </pre>
<p>To use the jalali datetime as a string, use the following command</p> <p>To use the jalali datetime as a string, use the following command</p>
<pre> <pre>
@@ -165,6 +171,8 @@ $dt = verta()->datetime();
<p>Verta to Carbon, use the following command</p> <p>Verta to Carbon, use the following command</p>
<pre> <pre>
$c = Carbon::instance($v->datetime()); $c = Carbon::instance($v->datetime());
// or
$c = $v->toCarbon();
</pre> </pre>
<h3 id="property">Property</h3> <h3 id="property">Property</h3>
<p>To get property, use the following command</p> <p>To get property, use the following command</p>
+8
View File
@@ -123,6 +123,12 @@ return new Verta($dt); // 1395-12-09 15:05:56
<pre> <pre>
$c = \Carbon::now(); $c = \Carbon::now();
return verta($c); // 1395-12-09 15:05:56 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();
</pre> </pre>
<p>برای استفاده از تاریخ شمسی به صورت رشته، از دستور زیر استفاده کنید</p> <p>برای استفاده از تاریخ شمسی به صورت رشته، از دستور زیر استفاده کنید</p>
<pre> <pre>
@@ -170,6 +176,8 @@ $dt = verta()->datetime();
<p>برای تبدیل تاریخ از ورتا به کربن، از دستور زیر استفاده کنید</p> <p>برای تبدیل تاریخ از ورتا به کربن، از دستور زیر استفاده کنید</p>
<pre> <pre>
$c = Carbon::instance($v->datetime()); $c = Carbon::instance($v->datetime());
// or
$c = $v->toCarbon();
</pre> </pre>
<h3 id="property">خصوصیات</h3> <h3 id="property">خصوصیات</h3>
<p>برای دریافت مقادیر خصوصیات، از دستورات زیر استفاده کنید</p> <p>برای دریافت مقادیر خصوصیات، از دستورات زیر استفاده کنید</p>
+5
View File
@@ -3,6 +3,7 @@
namespace Hekmatinasser\Verta\Laravel; namespace Hekmatinasser\Verta\Laravel;
use Hekmatinasser\Verta\Verta; use Hekmatinasser\Verta\Verta;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
@@ -87,6 +88,10 @@ class VertaServiceProvider extends ServiceProvider
$this->app->bind('verta', function ($app) { $this->app->bind('verta', function ($app) {
return new Verta(); return new Verta();
}); });
Carbon::macro('toJalali', function ($timezone = null) {
return new Verta($this, $timezone);
});
} }
/** /**
+11
View File
@@ -5,6 +5,7 @@ namespace Hekmatinasser\Verta\Traits;
use DateTime; use DateTime;
use DateTimeZone; use DateTimeZone;
use Exception; use Exception;
use Illuminate\Support\Carbon;
use InvalidArgumentException; use InvalidArgumentException;
trait Creator trait Creator
@@ -128,6 +129,16 @@ trait Creator
return new DateTime(date('Y-m-d H:i:s', $this->getTimestamp()), $this->getTimezone()); 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 * Create a Verta instance from a DateTime one
* *
+7
View File
@@ -74,6 +74,13 @@ class CreatorTest extends TestCase
$this->assertEquals('2019-01-01 10:20:11', $datetime); $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() public function testParse()
{ {
$datetime = Verta::parse('1397 دی 11 10:20:11')->formatGregorian('Y-m-d H:i:s'); $datetime = Verta::parse('1397 دی 11 10:20:11')->formatGregorian('Y-m-d H:i:s');