Added date factory

This commit is contained in:
Graham Campbell 2015-11-07 13:15:28 +00:00
parent 1a23d84488
commit 592b62b399
2 changed files with 94 additions and 1 deletions

77
app/Dates/DateFactory.php Normal file
View File

@ -0,0 +1,77 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Date;
use Jenssegers\Date\Date
class DateFactory
{
/**
* The application timezone.
*
* @var string
*/
protected $appTimezone;
/**
* The cachet timezone.
*
* @var string
*/
protected $cachetTimezone;
/**
* Create a new date factory instance.
*
* @param string $appTimezone
* @param string $cachetTimezone
*
* @return void
*/
public function __construct($appTimezone, $cachetTimezone)
{
$this->appTimezone = $appTimezone;
$this->cachetTimezone = $cachetTimezone;
}
/**
* Create a Carbon instance from a specific format.
*
* @param string $format
* @param string $time
*
* @throws \InvalidArgumentException
*
* @return \Carbon\Carbon
*/
public function create($format, $time)
{
return Date::createFromFormat($format, $time, $this->cachetTimezone)->setTimezone($this->appTimezone);
}
/**
* Create a Carbon instance from a specific format.
*
* We're also going to make sure the timezone information is correct.
*
* @param string $format
* @param string $time
*
* @throws \InvalidArgumentException
*
* @return \Carbon\Carbon
*/
public function createNormalized($format, $time)
{
return $this->createFromFormat($format, $time)->setTimezone($this->appTimezone);
}
}

View File

@ -11,6 +11,7 @@
namespace CachetHQ\Cachet\Providers; namespace CachetHQ\Cachet\Providers;
use CachetHQ\Cachet\Dates\DateFactory;
use Illuminate\Bus\Dispatcher; use Illuminate\Bus\Dispatcher;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -40,6 +41,21 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function register() public function register()
{ {
// $this->registerDateFactory();
}
/**
* Register the date factory.
*
* @return void
*/
protected function registerDateFactory()
{
$this->app->singleton(DateFactory::class, function ($app) {
$appTimezone = $app->config->get('app.timezone');
$cacheTimezone = $app->config->get('cachet.timezone');
return new DateFactory($appTimezone, $cacheTimezone);
});
} }
} }