Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning Carbon instances when using Model Casting #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ And with custom formatting
// 2018-07-04 3:32 New York, America
```

### Using models casting class

#### Basic usage

```
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use JamesMills\LaravelTimezone\Casts\Timezone;

class Foo extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'created_at' => Timezone::class,
];
}
```

### Saving the users input to the database in UTC

This will take a date/time, set it to the users timezone then return it as UTC in a Carbon instance.
Expand Down
45 changes: 45 additions & 0 deletions src/Casts/Timezone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace JamesMills\LaravelTimezone\Casts;

use Carbon\Carbon;
use Exception;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use JamesMills\LaravelTimezone\Facades\Timezone as TimezoneFacade;
use JamesMills\LaravelTimezone\Traits\TimezoneTrait;

class Timezone implements CastsAttributes
{
use TimezoneTrait;

/**
* Transform the attribute from the underlying model values.
*
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return Carbon
* @throws Exception
*/
public function get($model, string $key, $value, array $attributes)
{
return Carbon::parse($value)
->setTimezone($this->getUserTimezone());
}

/**
* Transform the attribute to its underlying model values.
*
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return Carbon
*/
public function set($model, string $key, $value, array $attributes)
{
return TimezoneFacade::convertFromLocal($value);
}
}
20 changes: 11 additions & 9 deletions src/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
namespace JamesMills\LaravelTimezone;

use Carbon\Carbon;
use JamesMills\LaravelTimezone\Traits\TimezoneTrait;

class Timezone
{
use TimezoneTrait;

/**
* @param Carbon|null $date
* @param null $format
* @param bool $format_timezone
* @return string
*/
public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false) : string
public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false): string
{
if (is_null($date)) {
return 'Empty';
}

$timezone = (auth()->user()->timezone) ?? config('app.timezone');

$date->setTimezone($timezone);
$date->setTimezone($this->getUserTimezone());

if (is_null($format)) {
return $date->format(config('timezone.format'));
Expand All @@ -29,7 +30,7 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone =
$formatted_date_time = $date->format($format);

if ($format_timezone) {
return $formatted_date_time . ' ' . $this->formatTimezone($date);
return $formatted_date_time.' '.$this->formatTimezone($date);
}

return $formatted_date_time;
Expand All @@ -39,22 +40,23 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone =
* @param $date
* @return Carbon
*/
public function convertFromLocal($date) : Carbon
public function convertFromLocal($date): Carbon
{
return Carbon::parse($date, auth()->user()->timezone)->setTimezone('UTC');
return Carbon::parse($date, auth()->user()->timezone)
->setTimezone('UTC');
}

/**
* @param Carbon $date
* @return string
*/
private function formatTimezone(Carbon $date) : string
private function formatTimezone(Carbon $date): string
{
$timezone = $date->format('e');
$parts = explode('/', $timezone);

if (count($parts) > 1) {
return str_replace('_', ' ', $parts[1]) . ', ' . $parts[0];
return str_replace('_', ' ', $parts[1]).', '.$parts[0];
}

return str_replace('_', ' ', $parts[0]);
Expand Down
14 changes: 14 additions & 0 deletions src/Traits/TimezoneTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace JamesMills\LaravelTimezone\Traits;

trait TimezoneTrait
{
/**
* @return string
*/
protected function getUserTimezone(): string
{
return (auth()->user()->timezone) ?? config('app.timezone');
}
}