Skip to content

Commit 9ed42b5

Browse files
author
Amandio Magalhaes
committed
Returning Carbon instances when using Model Casting
1 parent e202cd3 commit 9ed42b5

File tree

4 files changed

+100
-16
lines changed

4 files changed

+100
-16
lines changed

README.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# Laravel Timezone
22

3-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone)
4-
[![Total Downloads](https://img.shields.io/packagist/dt/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone)
5-
[![Licence](https://img.shields.io/packagist/l/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone)
6-
[![Quality Score](https://img.shields.io/scrutinizer/g/jamesmills/laravel-timezone.svg?style=flat-square)](https://scrutinizer-ci.com/g/jamesmills/laravel-timezone)
7-
[![StyleCI](https://github.styleci.io/repos/142882574/shield?branch=master)](https://github.styleci.io/repos/142882574)
8-
[![Buy us a tree](https://img.shields.io/badge/treeware-%F0%9F%8C%B3-lightgreen?style=flat-square)](https://plant.treeware.earth/jamesmills/laravel-timezone)
9-
[![Treeware (Trees)](https://img.shields.io/treeware/trees/jamesmills/laravel-timezone?style=flat-square)](https://plant.treeware.earth/jamesmills/laravel-timezone)
3+
[![Packagist](https://img.shields.io/packagist/v/jamesmills/laravel-timezone.svg?style=for-the-badge)](https://packagist.org/packages/jamesmills/laravel-timezone)
4+
![Packagist](https://img.shields.io/packagist/dt/jamesmills/laravel-timezone.svg?style=for-the-badge)
5+
![Packagist](https://img.shields.io/packagist/l/jamesmills/laravel-timezone.svg?style=for-the-badge)
6+
[![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen?style=for-the-badge)](https://plant.treeware.earth/jamesmills/laravel-timezone)
7+
[![Treeware (Trees)](https://img.shields.io/treeware/trees/jamesmills/laravel-timezone?style=for-the-badge)](https://plant.treeware.earth/jamesmills/laravel-timezone)
108

119
An easy way to set a timezone for a user in your application and then show date/times to them in their local timezone.
1210

@@ -90,6 +88,31 @@ And with custom formatting
9088
// 2018-07-04 3:32 New York, America
9189
```
9290

91+
### Using models casting class
92+
93+
#### Basic usage
94+
95+
```
96+
<?php
97+
98+
namespace App;
99+
100+
use Illuminate\Database\Eloquent\Model;
101+
use JamesMills\LaravelTimezone\Casts\Timezone;
102+
103+
class Foo extends Model
104+
{
105+
/**
106+
* The attributes that should be cast to native types.
107+
*
108+
* @var array
109+
*/
110+
protected $casts = [
111+
'created_at' => Timezone::class,
112+
];
113+
}
114+
```
115+
93116
### Saving the users input to the database in UTC
94117

95118
This will take a date/time, set it to the users timezone then return it as UTC in a Carbon instance.

src/Casts/Timezone.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace JamesMills\LaravelTimezone\Casts;
4+
5+
use Carbon\Carbon;
6+
use Exception;
7+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
8+
use Illuminate\Database\Eloquent\Model;
9+
use JamesMills\LaravelTimezone\Facades\Timezone as TimezoneFacade;
10+
use JamesMills\LaravelTimezone\Traits\TimezoneTrait;
11+
12+
class Timezone implements CastsAttributes
13+
{
14+
use TimezoneTrait;
15+
16+
/**
17+
* Transform the attribute from the underlying model values.
18+
*
19+
* @param Model $model
20+
* @param string $key
21+
* @param mixed $value
22+
* @param array $attributes
23+
* @return Carbon
24+
* @throws Exception
25+
*/
26+
public function get($model, string $key, $value, array $attributes)
27+
{
28+
return Carbon::parse($value)
29+
->setTimezone($this->getUserTimezone());
30+
}
31+
32+
/**
33+
* Transform the attribute to its underlying model values.
34+
*
35+
* @param Model $model
36+
* @param string $key
37+
* @param mixed $value
38+
* @param array $attributes
39+
* @return Carbon
40+
*/
41+
public function set($model, string $key, $value, array $attributes)
42+
{
43+
return TimezoneFacade::convertFromLocal($value);
44+
}
45+
}

src/Timezone.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@
33
namespace JamesMills\LaravelTimezone;
44

55
use Carbon\Carbon;
6+
use JamesMills\LaravelTimezone\Traits\TimezoneTrait;
67

78
class Timezone
89
{
10+
use TimezoneTrait;
11+
912
/**
1013
* @param Carbon|null $date
1114
* @param null $format
1215
* @param bool $format_timezone
1316
* @return string
1417
*/
15-
public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false) : string
18+
public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false): string
1619
{
1720
if (is_null($date)) {
1821
return 'Empty';
1922
}
2023

21-
$timezone = (auth()->user()->timezone) ?? config('app.timezone');
22-
23-
$date->setTimezone($timezone);
24+
$date->setTimezone($this->getUserTimezone());
2425

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

3132
if ($format_timezone) {
32-
return $formatted_date_time . ' ' . $this->formatTimezone($date);
33+
return $formatted_date_time.' '.$this->formatTimezone($date);
3334
}
3435

3536
return $formatted_date_time;
@@ -39,22 +40,23 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone =
3940
* @param $date
4041
* @return Carbon
4142
*/
42-
public function convertFromLocal($date) : Carbon
43+
public function convertFromLocal($date): Carbon
4344
{
44-
return Carbon::parse($date, auth()->user()->timezone)->setTimezone('UTC');
45+
return Carbon::parse($date, auth()->user()->timezone)
46+
->setTimezone('UTC');
4547
}
4648

4749
/**
4850
* @param Carbon $date
4951
* @return string
5052
*/
51-
private function formatTimezone(Carbon $date) : string
53+
private function formatTimezone(Carbon $date): string
5254
{
5355
$timezone = $date->format('e');
5456
$parts = explode('/', $timezone);
5557

5658
if (count($parts) > 1) {
57-
return str_replace('_', ' ', $parts[1]) . ', ' . $parts[0];
59+
return str_replace('_', ' ', $parts[1]).', '.$parts[0];
5860
}
5961

6062
return str_replace('_', ' ', $parts[0]);

src/Traits/TimezoneTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace JamesMills\LaravelTimezone\Traits;
4+
5+
trait TimezoneTrait
6+
{
7+
/**
8+
* @return string
9+
*/
10+
protected function getUserTimezone(): string
11+
{
12+
return (auth()->user()->timezone) ?? config('app.timezone');
13+
}
14+
}

0 commit comments

Comments
 (0)