Skip to content

Commit d7f3069

Browse files
author
Amandio Magalhaes
committed
Database data source
1 parent e202cd3 commit d7f3069

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ Pull in the package using Composer
4040
composer require jamesmills/laravel-timezone
4141
```
4242

43+
Add TimezoneModelTrait into your resource Model
44+
45+
```
46+
<?php
47+
48+
namespace App;
49+
50+
class Foo
51+
{
52+
use TimezoneModelTrait;
53+
}
54+
```
55+
56+
## Database
57+
58+
By default, this package is shipped with database migration,
59+
which creates a new column `timezone` into the `users` table.
60+
61+
### Using Default Migrations
4362
Publish database migrations
4463

4564
```
@@ -52,6 +71,11 @@ Run the database migrations. This will add a `timezone` column to your `users` t
5271
php artisan migrate
5372
```
5473

74+
### Custom Data Source
75+
76+
If you use a different data source for your timezone,
77+
you can just override both methods in the `TimezoneModelTrait` on your resource model to your needs.
78+
5579
## Examples
5680

5781
### Showing date/time to the user in their timezone

src/Listeners/Auth/UpdateUsersTimezone.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
class UpdateUsersTimezone
1111
{
12-
1312
/**
1413
* Handle the event.
1514
*
@@ -50,9 +49,9 @@ public function handle($event)
5049
$ip = $this->getFromLookup();
5150
$geoip_info = geoip()->getLocation($ip);
5251

53-
if ($user->timezone != $geoip_info['timezone']) {
54-
if (config('timezone.overwrite') == true || $user->timezone == null) {
55-
$user->timezone = $geoip_info['timezone'];
52+
if ($user->getTimezone() != $geoip_info['timezone']) {
53+
if (config('timezone.overwrite') == true || $user->getTimezone() == null) {
54+
$user->setTimezone($geoip_info['timezone']);
5655
$user->save();
5756

5857
$this->notify($geoip_info);
@@ -69,7 +68,7 @@ private function notify(Location $geoip_info)
6968
return;
7069
}
7170

72-
$message = 'We have set your timezone to ' . $geoip_info['timezone'];
71+
$message = 'We have set your timezone to '.$geoip_info['timezone'];
7372

7473
if (config('timezone.flash') == 'laravel') {
7574
request()->session()->flash('success', $message);
@@ -103,8 +102,8 @@ private function notify(Location $geoip_info)
103102
}
104103

105104
/**
106-
* @return mixed
107-
*/
105+
* @return string|null
106+
*/
108107
private function getFromLookup()
109108
{
110109
$result = null;
@@ -134,7 +133,7 @@ private function lookup($type, $keys)
134133
$value = null;
135134

136135
foreach ($keys as $key) {
137-
if (!request()->$type->has($key)) {
136+
if (! request()->$type->has($key)) {
138137
continue;
139138
}
140139
$value = request()->$type->get($key);

src/Timezone.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ class Timezone
1212
* @param bool $format_timezone
1313
* @return string
1414
*/
15-
public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false) : string
15+
public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false): string
1616
{
1717
if (is_null($date)) {
1818
return 'Empty';
1919
}
2020

21-
$timezone = (auth()->user()->timezone) ?? config('app.timezone');
21+
$timezone = (auth()->user()->getTimezone()) ?? config('app.timezone');
2222

2323
$date->setTimezone($timezone);
2424

@@ -29,7 +29,7 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone =
2929
$formatted_date_time = $date->format($format);
3030

3131
if ($format_timezone) {
32-
return $formatted_date_time . ' ' . $this->formatTimezone($date);
32+
return $formatted_date_time.' '.$this->formatTimezone($date);
3333
}
3434

3535
return $formatted_date_time;
@@ -39,22 +39,22 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone =
3939
* @param $date
4040
* @return Carbon
4141
*/
42-
public function convertFromLocal($date) : Carbon
42+
public function convertFromLocal($date): Carbon
4343
{
44-
return Carbon::parse($date, auth()->user()->timezone)->setTimezone('UTC');
44+
return Carbon::parse($date, auth()->user()->getTimezone())->setTimezone('UTC');
4545
}
4646

4747
/**
4848
* @param Carbon $date
4949
* @return string
5050
*/
51-
private function formatTimezone(Carbon $date) : string
51+
private function formatTimezone(Carbon $date): string
5252
{
5353
$timezone = $date->format('e');
5454
$parts = explode('/', $timezone);
5555

5656
if (count($parts) > 1) {
57-
return str_replace('_', ' ', $parts[1]) . ', ' . $parts[0];
57+
return str_replace('_', ' ', $parts[1]).', '.$parts[0];
5858
}
5959

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

src/Traits/TimezoneModelTrait.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace JamesMills\LaravelTimezone\Traits;
4+
5+
trait TimezoneModelTrait
6+
{
7+
/**
8+
* @return mixed
9+
*/
10+
public function getTimezone()
11+
{
12+
return $this->timezone;
13+
}
14+
15+
/**
16+
* @param string $timezone
17+
* @return $this
18+
*/
19+
public function setTimezone(string $timezone)
20+
{
21+
$this->timezone = $timezone;
22+
23+
return $this;
24+
}
25+
}

0 commit comments

Comments
 (0)